views:

420

answers:

2

Hi,
I'm new to the Visual C# designer so these are general and pretty basic question on how to work with the designer.

When we for instance add a label to a form and then double-click on it in the Visual C# designer (I'm using Microsoft Visual C# 2008 Express Edition), the following things happen:

  1. The designer generates code within Form1.Designer.cs (assume default names for simplicity) to add the label,
  2. then with the double-click it will add the event handler label1_Click to the label within Form1.Designer.cs, using the following code

    this.label1.Click += new System.EventHandler(this.label1_Click);

and it adds the event handler method to Form1.cs

private void label1_Click(object sender, EventArgs e)
{

}

If I now remove the label only the code within Form1.Designer.cs will be removed but the label1_Click method will stay within Form1.cs even if it isn't used by anything else. But if I'm using reset within Properties->Events for the Click-event from within the designer even the label1_Click method in Form1.cs will be removed.

1.) Isn't that a little inconsistent behavior?

2.) What is the recommended way of removing such generated event handler-code?

3.) What is the best "mental approach"/best practice for using the designer?

I would approach it by mental separation in the way that Form1.cs is 100% my responsibility and that on the other hand I'm not touching the code in Form1.Designer.cs at all. Does that make sense or not? Since sometimes the designer removes sth. from Form1.cs I'm not sure about this.

+1  A: 

There probably is a element of safety built into Visual Studio.

For example:

  • Add a button A and a click event.
  • Reference the button A click event from another button B.
  • Remove button A
    • if the code were to go then button B would break
    • if the code remains then button B continues to work.

I generally comment out any code (event handlers) that break in the designer.cs file.

John M
+1  A: 

1) Yes, it is inconsistent. A little. 2) I used MUCH MORE SIMPLE approach: simple wipe out all your handle code and try to compile => compiler will show you where to wipe out an event assignment. Despite of scary look, it is really safe. 3) Here is my best practices which I recommend and kind of enforce in my software department: 3a) Switch to WPF (ask for best WPF practices separately; there are a lot of other problems); 3b) NEVER ever allow Visual Studio to auto-generate event code (WPF or Windows.Forms); in case of accident use (2) as soon as possible; 3b) For event assignment use anonymous lambda:

ByButton.Click += (source, evArg) => { SomeHandler(...); };

for v.2.0:

ByButton.Click += delegate(object source, EventArgs evArgs) { SomeHandler(...); };

There are many benefits: your handlers are not bound to using specific method profile; you can put whole code inside anonymous handler is it is short enough, in lambda form you may never need to know the type of Event Arguments...

RocketSurgeon