button click event question

T

Tony

I put three buttone on my form.
Double clicked the first button and got the below code

private void button1_Click(object sender, System.EventArgs e)
{
}

I put some stuff in it and then it occured to me that I left the
button's name etc default..

Went to the putton's properties and changed the name of the button to
"btnAdd"

Double clicked the button again expecting it to create

private void btnAdd_Click(object sender, System.EventArgs e)
{
}

But intead, it put me back into the same Button1 click event code..

I figured VS must be confused and changed the event to
private void btnAdd_Click(object sender, System.EventArgs e)
{
//some stuff here
}

And tried a debug compile... It complained that there wasn't a click
event for button1

I'm sure I've done this in VB6 with no problems..

What am I not seeing here?

Thanks :)

Tony!
 
C

Chris R. Timmons

I put three buttone on my form.
Double clicked the first button and got the below code

private void button1_Click(object sender, System.EventArgs e)
{
}

I put some stuff in it and then it occured to me that I left the
button's name etc default..

Went to the putton's properties and changed the name of the
button to "btnAdd"

Double clicked the button again expecting it to create

private void btnAdd_Click(object sender, System.EventArgs e)
{
}

But intead, it put me back into the same Button1 click event
code..

I figured VS must be confused and changed the event to
private void btnAdd_Click(object sender, System.EventArgs e)
{
//some stuff here
}

And tried a debug compile... It complained that there wasn't a
click event for button1

I'm sure I've done this in VB6 with no problems..

What am I not seeing here?

Tony,

VS.Net isn't smart enough to do that automatically. The code that's
causing the problem is in the code that was generated by VS.Net. You
can safely do a manual replacement by searching your source code for
button1_Click and replacing it with btnAdd_Click.
 
S

Simon Smith

I put three buttone on my form.

<snip stuff about renaming a button not renaming it's click event>

As Tony says, VS.NET won't rename the events for you. Unlike VB6, the
click event is not linked to the button by the method name. In the generated
code it assigns the method to the button's click. Using the Events pane
of the properties you can assign any method with the right signature to
the button's click event and presumably VS.NET won't rename the method
because other controls might be sharing the method and so renaming it would
break them. Also in the events pane you can type in any name you want for
the click event and if it doesn't exist it will create it with the name
you've specified and the correct signature - a bit more flexible than VB6.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top