0

Yes, I have already researched this question. I've found this: How to display remaining textbox characters in a label in C#? and many others just like it. That's how I managed to get this following code pieced together:

protected void rtdDisclaimer(object sender, EventArgs e)
{
    lblCharCount.Text = "Characters Remaining:" + (700 - rtbDisclaimer.Text.Length).ToString(); // char count limit set to 700
}

I've never coded in c# before, but am working on a group project and that's the language the group lead chose. I'm new to programming and have minimal experience with java. This program is being done in visual studio. I'm trying to make the label show the number of characters remaining depending upon what's typed in the richtextbox. There are no errors, but the label isn't displaying anything at all.

  • I double clicked on the richtextbox on the form and it created the header. The variable inside is spelled slightly differently rtbDisclaimer because that's how someone else in the group typed it. The variable has a b while the actual richtextbox itself has a d. No errors with that, just a mistype on someones part. –  Mar 08 '16 at 23:57

3 Answers3

1

You must associate the method to the textchanged event of the control.

protected void rtbDisclaimerTextChanged(object sender, EventArgs e)
{
     lblCharCount.Text = "Characters Remaining:" + (700 - rtbDisclaimer.Text.Length).ToString(); // char count limit set to 700
}

On the constructor, after InitializeComponent(), yo must add this line:

rtbDisclaimer.TextChanged += new EventHandler(rtbDisclaimerTextChanged);
Richard
  • 568
  • 1
  • 6
  • 23
0

Your code will work, but you need to attach the method to the correct event on your RichTextBox, so it will invoke that code when the event fires.

Add this to the constructor of your Form:

rtbDisclaimer.TextChanged += rtdDisclaimer;
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
  • Error CS1061 'FrmMain' does not contain a definition for 'tabDisclaimer_Click' and no extension method 'tabDisclaimer_Click' accepting a first argument of type 'FrmMain' could be found (are you missing a using directive or an assembly reference?) The textbox is on the Disclaimer tab, but what would that have to do with the textbox itself? –  Mar 09 '16 at 00:22
-1

The function you mentioned in your question seems a server side and you can call it on a an event like a button's click. If you want to display characters length while typing then use JavaScript/jQuery

like

$('textarea').keyup(updateCount);
$('textarea').keydown(updateCount);

function updateCount() {
    var cs = $(this).val().length;
    $('#characters').text(cs);
}

Check this too. JsFiddle example (by Dreami)

Hepe this helps!

Sami
  • 3,686
  • 4
  • 17
  • 28
  • $('#characters') is ID of the div/span to display characters count. – Sami Mar 09 '16 at 00:02
  • I'm new to programming, so this may seem like a stupid question, but can you mix code like that? This small thing is in a bigger chunk of code in visual studio thats all written in C#. –  Mar 09 '16 at 00:09