0

I have a basic understanding issue that is there any difference between : @adding a click event to form_load method of windows forms like

viewToolStripMenuItem.Clicked += viewToolStripMenuItem_Click;

and @adding a click event to form1.designer.cs as:

this.viewToolStripMenuItem = new System.Windows.Froms.ToolStripMenuItem();
this.viewToolStripMenuItem.Name = "viewToolStripMenuItem";
this.viewToolStripMenuItem.Size = new System.Drawing.Size(44,30);
this.viewToolStripMenuItem.Text = "View";
this.viewToolStripMenuItem.Click += new System.EventHandler(this.viewToolStripMenuItem_Click)

Sorry for question repeat in advance.

mesutali
  • 1,137
  • 1
  • 9
  • 9
  • Putting code like this in the Load event handler earns [-100 elegance points](http://stackoverflow.com/questions/2521322/what-setup-code-should-go-in-form-constructors-versus-form-load-event/2522999#2522999). And [a headache](http://stackoverflow.com/a/4934010/17034). – Hans Passant Sep 02 '14 at 08:08
  • @HansPassant thank you for short but excellent answer. thanks for also combining two important issues for me – mesutali Sep 02 '14 at 14:18

1 Answers1

1

Never alter .designer.cs code because it has been generated by the tooling (Visual Studio), which means that in some cases your code might get wiped out!

If I'm not wrong, the code from the form1.designer.cs should be called by InitializeComponent. Check that this is called in the constructor of the form class: add your event handlers just after InitializeComponent call.

As @Sayse has pointed out, my warning mightn't be useful if you're adding these event handlers using Visual Studio designer (i.e. configuring control's properties in the designer view). If you're in this case, you can either leave your code as is.

Matías Fidemraizer
  • 59,064
  • 16
  • 107
  • 181
  • Totally agree, but if the OP was to add the code to the designer.cs by using the designer, then the two code snippets provided would essentially do the same thing (and safely added!) – Sayse Sep 02 '14 at 07:54
  • 1
    @Sayse You're absolutely right about this, I'm going to add your point in my answer. thanks. – Matías Fidemraizer Sep 02 '14 at 07:57