Visual C # GUI Designer - Recommended Way to Remove Generated Event Handler

I am new to the Visual C # designer, so this is a general and fairly simple question about how to work with the designer.

For example, if we add a label to the form and then double-click it in the Visual C # designer (I use Microsoft Visual C # 2008 Express Edition), the following will happen:

  • The designer generates code in Form1.Designer.cs (suppose the default names are for simplicity) to add a label,
  • then, with a double click, add the label of the event handler label1_Click to the label in Form1.Designer.cs using the following code

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

and adds an event handler method to Form1.cs

private void label1_Click(object sender, EventArgs e)
{

}

If you remove the label now, the code Form1.Designer.cs will be deleted, but the label1_Click method will remain within Form1.cs, even if it is not used by anything else. But if I use reset in properties -> Events for the Click event from the constructor, even the label1_Click method in Form1.cs will be deleted.

1.) Isn't this a contradictory behavior?

2.) What is the recommended way to remove such a generated event handler?

3.) What is the best “mental approach” / best practice for using a designer?

I would approach it with the help of mental separation in that Form1.cs is 100% responsible for me, and on the other hand, I don’t touch the code in Form1.Designer.cs at all. Does this make sense or not? Since sometimes the designer removes sth. from Form1.cs I'm not sure about that.

+3
2

1) , . . 2) MUCH MORE SIMPLE: = > , . , . 3) , - : 3a) WPF ( WPF , ); 3b) Visual Studio (WPF Windows.Forms); (2) ; 3b) :

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

v.2.0:

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

: ; , , ...

+2

, , Visual Studio.

:

  • A click.
  • A click B.
  • A
    • , B
    • , B .

( ), designer.cs.

+1

Source: https://habr.com/ru/post/1739234/


All Articles