Results 1 to 8 of 8

Thread: [RESOLVED] Why button name and button_click name does not match?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2017
    Location
    Washington DC
    Posts
    154

    Resolved [RESOLVED] Why button name and button_click name does not match?

    I have a button called btnConnect, but I do not see anything called btnConnect_Click

    If there are too many buttons, I don't know which one is which one.

    Why?

    Thanks.


    Name:  Connect1.jpg
Views: 173
Size:  10.2 KB



    Name:  Connect2.jpg
Views: 169
Size:  17.6 KB

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

    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.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    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.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Apr 2017
    Location
    Washington DC
    Posts
    154

    Re: Why button name and button_click name does not match?

    Quote Originally Posted by passel View Post
    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.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Apr 2017
    Location
    Washington DC
    Posts
    154

    Re: Why button name and button_click name does not match?

    Quote Originally Posted by dee-u View Post
    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.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Apr 2017
    Location
    Washington DC
    Posts
    154

    Re: Why button name and button_click name does not match?

    Quote Originally Posted by dee-u View Post
    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.

  7. #7
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

    Re: Why button name and button_click name does not match?

    Quote Originally Posted by VAian View Post
    Thanks, where is Reset menu for button?

    Thanks.
    Name:  Resetbutton.jpg
Views: 107
Size:  21.2 KB
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  8. #8
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Why button name and button_click name does not match?

    Quote Originally Posted by VAian View Post
    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
  •  



Click Here to Expand Forum to Full Width