1

Okay, so here's my problem: Whenever I create a button, textbox, listbox, then double-click to view the source code:

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

After this has been auto-created by Visual Studio, it won't allow me to change it later.

So when I go into properties and want to change "quitToolStripMenuItem" (in the Name property field) to "mnuQuit," it will show up in the properties window properly, and will change the name (for all intents and purposes), but when I double-click to view source - it still shows the 'quitToolStrip..." name.

If I rename it to

private void mnuQuit_Click(object sender, EventArgs e)

It will throw a big hissy fit, and then my form design will be gone and a (basically) 404 message will appear instead of the form.

How can I do it without deleting the item and then recreating?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Newbie
  • 179
  • 2
  • 11
  • When you rename there is a smart tag that pops up and let's you rename all references, you want to make sure to use that. – Ron Beyer Jul 26 '15 at 15:39
  • Nope, it doesn't work unfortunately. 'The designer cannot process unknown name 'Name' at line 107. The code within the method 'InitializeComponent' is generated by the designer and should not be manually modified. Please remove any changes and try opening the designer again.' -- is the error message and '404' message received any time I change it. – Newbie Jul 26 '15 at 15:45
  • The way you have phrased the question, it sounds like you are double-clicking on a control as a way of switching from the design view to the source code view! I expect I have simply misunderstood what you are doing, but I thought I should just check. – IglooGreg Jul 26 '15 at 21:19

3 Answers3

3

If you want to rename the event handler method, go to the designer, select the object (the menu item in your case), and in the properties window click the events button (it looks like a lightning bolt), and rename the event handler method from there.

In C#, the event handler is linked to the object that raise the event by a delegate, so the name of the method does not matter. You can have a button called Jack and event handler called Jill_Click that will actually handle the resize event of Jack. If you open the designer code you will see something like:

this.Jack.Resize += new System.EventHandler(this.Jill_Click);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
  • Oh my, Thank you so much. This isn't the first time I ran into this issue. I just never asked because I hadn't ever put as much effort into setting the others up as I had this one and would just recreate them. LOL. Thank you!!!!!! – Newbie Jul 26 '15 at 15:49
1

Click on the Events (lightning) icon at the top of the Properties window and delete the text in the Click field.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hoang Anh
  • 11
  • 1
  • If the original handler (function) is renamed first (in the .cs file), then this is a good way to ***avoid*** having to ***manually*** correct the name (unlike [Zohar Peled's answer](https://stackoverflow.com/questions/31638776/how-do-i-rename-a-form-objectbutton-textbox-etc-after-visual-c-sharp-has-au/31638874#31638874)) - just double click on *"Click"*. (If it is not renamed then "`_1`" will be appended to the handler (function) name.) – Peter Mortensen Feb 27 '19 at 09:13
0

Just refactor the method name? This will update it on the .designer.cs, in your designer and of course in your code.

Select the method name, right click, RefactorRename.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
CularBytes
  • 9,924
  • 8
  • 76
  • 101