-
Feb 19th, 2019, 03:19 PM
#1
Thread Starter
Addicted Member
-
Feb 19th, 2019, 04:21 PM
#2
Re: Why button name and button_click name does not match?
It could be that you created the button1_Click event when your button's name was "button1" then you renamed it to btnConnect. You could reset it by right clicking the Click property then click the Reset menu, then double click it to generate a new Click button having your button's name. Or you could just modify your click event and name it to "btnConnect_Click" then select that event.
-
Feb 19th, 2019, 04:55 PM
#3
Re: Why button name and button_click name does not match?
You can have multiple buttons go to the same event handler if you wanted, which can be handy in many cases. So, the event handler's name doesn't have to match the button name. A button event sub for your button isn't created automatically, you have to cause it to be generated, perhaps by double clicking on the control, or the event in the event list.
-
Feb 19th, 2019, 06:05 PM
#4
Thread Starter
Addicted Member
Re: Why button name and button_click name does not match?
Originally Posted by passel
You can have multiple buttons go to the same event handler if you wanted,
What do you mean? Could you please give me a detailed example (when to use this kind of case)?
Thanks.
-
Feb 19th, 2019, 06:07 PM
#5
Thread Starter
Addicted Member
Re: Why button name and button_click name does not match?
Originally Posted by dee-u
It could be that you created the button1_Click event when your button's name was "button1" then you renamed it to btnConnect. You could reset it by right clicking the Click property then click the Reset menu, then double click it to generate a new Click button having your button's name. Or you could just modify your click event and name it to "btnConnect_Click" then select that event.
Thanks, I will take a look at it.
-
Feb 19th, 2019, 07:47 PM
#6
Thread Starter
Addicted Member
Re: Why button name and button_click name does not match?
Originally Posted by dee-u
It could be that you created the button1_Click event when your button's name was "button1" then you renamed it to btnConnect. You could reset it by right clicking the Click property then click the Reset menu, then double click it to generate a new Click button having your button's name. Or you could just modify your click event and name it to "btnConnect_Click" then select that event.
Thanks, where is Reset menu for button?
Thanks.
-
Feb 19th, 2019, 08:27 PM
#7
Re: Why button name and button_click name does not match?
Originally Posted by VAian
Thanks, where is Reset menu for button?
Thanks.
-
Feb 19th, 2019, 11:38 PM
#8
Re: Why button name and button_click name does not match?
Originally Posted by VAian
What do you mean? Could you please give me a detailed example (when to use this kind of case)?
Thanks.
There could be any number of cases where the code is largely redundant for each control so rather than have separate event handlers for each button and have the same code in each or have them call a common function, you just assign the same handler for each control.
A simple example. I want to create 10 buttons with the text "0" to "9", one digit in each button.
When I press the button, I want to append the digit associated with the button to a textbox.
So, I can create a button, change its text to "0", and resize it to be a small square button. I can copy that button and paste it nine times and rearrange them to create a desired keypad layout.
There are several ways you can do the next step, but I usually select one of the buttons and double click on it to create the default click event handler for it. I then change the name of the sub to something more general since it will handle multiple buttons clicks.
For instance, assume I double click on button1 and get a function named button1_Click with the appropriate parameters created, and it is tied to the click event of that button.
I then edit the code to change the name of the function to buttonNum_Click.
I then "rope" the 10 buttons (select them by dragging the mouse over them) and in the Properties window, with events listed, I select the buttonNum_Click function for the Click event, which chooses that event handler function for all ten buttons.
I then add the code I want to be processed when any of the ten buttons are clicked.
In this example, the code could look like this.
Code:
private void buttonNum_Click(object sender, EventArgs e)
{
textBox1.AppendText(((Button)sender).Text);
}
I cast the sender object to a Button instance and append its Text to the TextBox's text.
Try it, and you'll see you can click on any of the ten buttons and the text will be appended to the text box, so all ten button's click event is handled by one handler with one line of code in it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|