Reputation: 39
Day 1 coder here:
private void button1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
string firstName;
}
In Visual C# Express, I changed my button name (through properties) from button1
to btnString
, but as you can see it is not adapting in the code.
Did I do something wrong ?
Upvotes: 1
Views: 3101
Reputation: 17512
When you change the name of a control, the designer will search your code and replace instances where you have used that variable name, but it will not change the name of event handlers.
Why? What if instead of the "default name" (for a button click it would be <buttonName>_Click
) you had your own custom name, like MyCoolEventHandler_Click
? The designer would not know how to rename that. The same thing applies if you have coincidentally used a variable name in a totally unrelated function. Would you want it changing the name on you?
You have to do these changes manually. My best advice is to name the controls before you create event handlers. But you can always go into the Properties panel and change the links.
Upvotes: 1
Reputation: 432
private void button1_Click(object sender, EventArgs e)
{
}
This is the event handler, and by changing the control's name it won't automatically change the event handler name (because it was created prior to changing the control's name), you can manually do it though. You can also change it through the properties window, change the tab to "events" instead of the default selected "properties" tab
Upvotes: 2
Reputation: 1802
Changing the button name seemingly hasn't updated the Click event handler.
Either go to the button properties event tab, and update the button1_Click
to btnString_Click
and the same to the event function, or delete the button1_Click
function and double click the button again, so visual studio will generate the correct handler name.
Upvotes: 1