Question Click event name for button in code, not changing to one set in button properties

Valhalla

Member
Joined
Jun 16, 2013
Messages
10
Programming Experience
10+
In Visual Studio, C# .net, when I add controls to a form and set their name property, the click event code for the control in the code window, still has the default name like button1 rather than the name I set in the controls properties. It doesn't happen all the time but once is too often. Does anyone know why this is happening?

Thanks!

Valhalla
 
It's happening because that's how it's supposed to work. You seem to think that the behaviour is inconsistent but it's not. The name of an event handler will NEVER change when you change the Name of a control. What you should be doing is changing the Name IMMEDIATELY after adding the control to the form, BEFORE adding any event handlers. That way, when you do add an event handler, it will contain the proper name of the control. If the IDE was to automatically change the name of the event handler just because you change the Name of a control then what about situations where the method isn't named after that control, e.g. you are using the same method to handle events for multiple controls?

That said, there's no issue changing the name of a method after it's been written, whether you wrote it yourself or the IDE wrote it for you. The thing to remember is that you need to change any references to that method elsewhere in the code too, which includes where it's registered as an event handler. When you change an existing identifier, the IDE will put a little red bar under the end of the name. If you mouse over that bar it should expand into an icon with a drop-down arrow. Clicking on that drop-down arrow should give you an option to rename all references.
 
"The name of an event handler will NEVER change when you change the Name of a control. What you should be doing is changing the Name IMMEDIATELY after adding the control to the form, BEFORE adding any event handlers. That way, when you do add an event handler, it will contain the proper name of the control."


Thanks for the response! I used to program professionally, the last language being VB.net. I'm getting back into after a number of years away so I'm rusty. I don't remember seeing this behavior with VB.net. I'm not creating my own methods or event handlers. This issue is just with controls from the tool box, specifically button controls. I was experimenting today with this issue by varying the way I put a button on a form and opening the code window at different times, before I renamed a control and after. It does, sometimes, change the default click event name to the name I gave it. Other times it shows the generic/default name. I did rename the button immediately after putting it on the form and it still is unpredictable. Sometimes when I put the button on the form and double click it for the code window, it doesn't show any reference anywhere to that button with any name. At one point I put a button on the form, renamed it and saw:
private void button1_Click(......etc.). I added another button, renamed it uniquely, and the code window showed:
private void button1_Click(......etc.), exactly the same name. I put different code in each one that referenced the control by the name I gave it. When I ran it, it was working and distinguished between the two identically named click events. Not that it is random, but it seems like it is hit or miss when it renames the click event and when it doesn't. I understand what you said about being by design, but should it really be this unpredictable? I don't remember seeing this in the past with VB.net. I am assuming this would be more .net ide behavior as opposed to C# or VB specific.

The problem is after putting 10 buttons on the form and then going to the code window, if it is unpredictably changing some of the names and not others, how does one know which private void button1_Click(......etc.) is referencing which button I put on the form. I know this did not happen with VB.net. By the way, this is an older version of .net and C#. I believe it is .net framework 1.1 and MS development environment 2003 version 7.1.3088.

Thanks!
 
If what you're saying is true then something is broken on your machine. I'd start by reinstalling VS.

By the way, if you're using VS.NET 2003 and .NET 1.1 then why does your profile say .NET 3.5?
 
Thanks! As I remember there was no option for 1.1. I'll go back and look and see if I can change that. I'll also look into upgrading. I'm getting back into it and really wanted to get up to speed with C# first....
 
Thanks! As I remember there was no option for 1.1. I'll go back and look and see if I can change that. I'll also look into upgrading. I'm getting back into it and really wanted to get up to speed with C# first....

The latest version is VS 2012, which is four versions later than yours. VS Express is free and very functional. It is missing some of the features of the paid-for editon that you must be using but you're unlikely to be using many, if any, of those anyway and you'll get so much more as a result. Some of the new stuff you can do without for a while but some you certainly shouldn't do without, e.g. generics. I recommend upgrading to VS 2012 Express for Windows desktop immediately. Note that it supports both VB and C#.
 
Thanks! I will probably do that rather than the expense of the full version upgrade now. I changed my profile to .net 1.1. Apparently I didn't see that when registering.

Also... I found that if I add each button, name it and then double click on the button to launch the code window, everything is named properly. It would appear that the launching of the code window by double clicking on the button is what actually processes the button and it's events etc. including any name change. If the code window is opened by double clicking on the control before renamed, then it creates the events with the name at the time, namely the default name. I opened the code window by double clicking on the form and it did not have the events created yet for the new buttons. So the trick is not to double click on the control until after you have renamed it. :)

Also....Thanks for being so quick to respond with this issue!

Valhalla
 
So the trick is not to double click on the control until after you have renamed it. :)
Yes, and that's exactly what I said back in post #2. When you double-click on a control or component in the designer, the IDE will navigate to the handler for the default event of that control or component, creating it if it doesn't already exist. If the event handler is created, it will be named "<componentName>_<EventName>". That's how it is for both VB and C# and in every version of VS from 2002 to 2012. Changing the name of a control or component has never and will never change the names of existing event handlers. Given that changing the name of a control or component to something meaningful should pretty much always be the first thing you do after adding it, that should rarely be a problem. In those cases where you do need to change the name of something after creating event handlers, you can then change the name of the event handlers and their references as I described in post #2.
 
Yes you did and thanks. It was still seeming unpredictable at that time. It makes sense now though and I do remember having to go back and rename control event handlers at times when something was renamed after the fact. I guess the relearning process is worse than the original learning process. :)

Thanks again for your help!

Valhalla
 
Back
Top Bottom