Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the correct way to change name of form element in C# Visual Studio?

I'm using Visual Studio, I have generated a form & added a button but unfortunately I have named btn_test1 instead of Btn_test1 (small vs large beginning letter) & double clicked the button to generate the click function and visual Studio happily generated the: btn_test1_Click() function & then complained that the function didn't start with a capital letter.

I thought: "Ok, no problem", went back & renamed the button to Btn_test1 & changed the click function to Btn_test1_Click() and now instead Visual Studio gives me an error that btn_test1_Click() is missing.

My question is: What is the proper procedure I should have used in this case?

If you don't want to write a long answer but have a link to some documentation it's fine too.

like image 889
Sebastian Norr Avatar asked Dec 06 '25 05:12

Sebastian Norr


2 Answers

Okay, there is no need for a capital letter at the beginning of a name property, I'm also using VS 2017. I'll give some screenshots how to rename a function.

Step 1: Click on the events of the button in the properties panel as shown in the following image enter image description here

Step 2: Find click event, and Delete the text

enter image description here

enter image description here

Step 3: Go to Properties in the properties panel

enter image description here

Step 4: Select the Name property of the button.

enter image description here

Step 5: Rename it as you like,

enter image description here

Step 6: Double click the button on the designer, it will automatically create a function with the new name.

As for the naming convention, use as I mentioned in the image.

for example, a button to send something can be named as btnSend. btn -> stands for button. Send -> represents the name.


Note:

After you have successfully renamed and generated the new click event code. The old click event code will just be there. If you need the content of the old event copy it to the new click event code.

Also, if you still stuck with some error states that the old function is missing. Read the Designer.cs file carefully and delete all lines associated with the old function name.

like image 55
Christlin Panneer Avatar answered Dec 08 '25 19:12

Christlin Panneer


Form1.cs [Design] crashes when you try to delete the event that is bound with button.

While making such changes -

  1. Go to Button properties and remove the function name from click event.
  2. Change the Button name
  3. Again go to click event and do double click it (VS will generate click event for you)
  4. Now you can delete the previous click event from your code without making your Form1.cs(design) crash.
like image 37
Shilpa Avatar answered Dec 08 '25 18:12

Shilpa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!