C#: change button name and click function name

26,841

Solution 1

You can change the name of the method by simply typing the new name in place of the old one when editing the code. Just like changing any text.

Then you can validate that the control still uses that event by either:

  1. Go to the Form designer and select the control. Check its properties/events and make sure the name still lines up.
  2. Go to the code generated by the form designer and find where it assigns the method to the event, and change it if it needs to be changed.

Either one of these should also update the other one automatically, though I imagine it's generally preferred to use the first approach for consistency with the rest of the form design process.

Note that the method name can be anything you like, there's no steadfast rule that says it must be controlName_eventName(object sender, EvenArgs e). For example, if you have multiple controls which share an event handler then you don't have to decide which control gets to be the one in the method name. You can give it any common name, such as saveFormChanges(object sender, EvenArgs e) and still assign it as the handler for that event.

Solution 2

Right click on the function name and select Rename. The name will be highlighted (it's green on my system, see picture below). Edit the name and then hit the Apply button. The button's click event will correctly point to the newly renamed method.

enter image description here

enter image description here

Solution 3

go to your designer Right Click -> Properties (F4) -> Events -> ClickEvent -> Type in the name what so ever you want and click this will create Click event with the name you entered

Solution 4

You need to change the name of Event from the property window.enter image description here

Solution 5

change button1_Click method name in code to new name and then press ctrl+. (period) and chose Rename. Alternatively, click on light bulb and select Rename.

Share:
26,841

Related videos on Youtube

SHAHS
Author by

SHAHS

Updated on July 13, 2020

Comments

  • SHAHS
    SHAHS almost 4 years

    I am starting with C# programming, I have a scenario, where I create a windows form application. I have created a button inside the form and double click it to generate its associated click function. The visual studio generates the function with default name.

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

    Now when I rename the button in properties, the function name remains same, making it difficult to relate button and function, I have an old code where a lot of such cases are present. So, I cannot delete and recreate the buttons and other things.

    How can I rename the functions when I rename the button?

    • Mighty Badaboom
      Mighty Badaboom almost 7 years
      Have you tryed clicking in the function name and pressing f2 ;)
    • someguy76
      someguy76 almost 7 years
      private void yournewbuttonname_Click(object sender, EvenArgs e) manually change it to that?