display characters remaining in label

Treeki

New member
Joined
Mar 8, 2016
Messages
1
Programming Experience
Beginner
Let me start by saying I'm new to programming, and so far I've only spent a few months learning Java, and even less time learning visual basic well over a year ago - that's it. Now I'm stuck in a team having to do a group project and the group lead decided to code this in c#, which I've never used, and we're coding it in visual studio - which I have almost no experience with. I've searched all over google with little luck, so I'm posting on here to see if anyone can help. There's a form with a rich textbox on it. I've added a label and I want it to display the number of characters remaining that the user can type into the box. This is what I've managed to piece together:

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

There are no errors, but nothing happens. The label is empty. I don't understand.
 
Is that code even being executed? Are you calling that method directly or is it supposed to be an event handler? The method signature indicates the latter but, by the name, I'm guessing that you have written that method yourself, maybe by pasting it in, rather than having the IDE generate it. If that is the case then there is no connection between the event and the method, so the method will not be executed when the event is raised.

Generally speaking, you should let the IDE generate event handlers for you. That can be done in a number of ways. In the designer, you can double-click a control or component and a handler for the default event will be generated. If you want to handle an event other than the default, select the control or component, open the Properties window, click the Events button at the top and then double-click the desired event.

If you already have a method written then you can use the drop-down for the desired event in the Properties to select that method. Note that only methods with appropriate signatures will be displayed.

You can also register an event handler is code, but that would usually only be done with objects that you create in code rather than in the designer. The IDE can help you out there too.
 
By the way, if you want to confirm whether or not the code is being executed then place a breakpoint on it using the F9 key. Execution in the debugger will then break when the breakpoint is hit. If the debugger never breaks then you know the code is never executed.
 
Back
Top Bottom