C # event handlers - syntax / pattern

I am new to C #, so I apologize if this is a question about noob. I am trying to get clarity around the syntax or pattern for handling events in C #.

So, I have a Form Form1 object and a Button button1 in the form. I handle the Click event with a method similar to this in Form1.cs:

 private void button1_Click(object sender, EventArgs e) { Debug.WriteLine("click!"); } 

which works great. Now in another Form2 form, I have TreeView treeView1 and I want to handle the BeforeExpand event. Therefore, I assumed that this would be:

 private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e) { Debug.WriteLine("Hello!"); } 

which actually doesn't work: this method is never called when I extend node. But a few SO answers imply that this is the way to do this, for example this one .

In any case, I found an alternative approach that works for me. In the form constructor, bind the event handler as follows:

 treeView1.BeforeExpand += new TreeViewCancelEventHandler(anyMethodNameYouLike); 

So what is the difference between these two approaches? Why is _event syntax not working for tree? Is there a difference between the types of events?

thanks

+6
source share
8 answers

I assume that you double-clicked the button in the designer of Visual Studio. The button1_Click handler button1_Click added automatically, just as you manually created the BeforeExpand handler.

Check the file Form1.Designer.cs, you will find a line something like this:

 this.button1.Click += new System.EventHandler(this.button1_Click); 
+5
source

You need both things:
1) a method that can handle the type of event in question. For TreeViewCancelEventHandler ( MSDN ) to delegate the correct method signature:

 public void MyMethodNameGoesHere(Object sender,TreeViewCancelEventArgs e) { // do some impressive stuff here } 

2) you need to register for the event:

 treeView1.BeforeExpand += new TreeViewCancelEventHandler(MyMethodNameGoesHere); 

You can also use only the method name:

 treeView1.BeforeExpand += MyMethodNameGoesHere; 

And as a final alternative, you can use this โ€œbuilt-inโ€ syntax for small functions:

 treeView1.BeforeExpand += (sender, e) => { // do a bit of magic here }; 

What is perhaps nice to know is that the registration of the handlers does not add up (sorry for the poor wording, suggestions for improvement are very welcome!). If you do the following, you will not receive further events after the last line:

 treeView1.BeforeExpand += MyMethodNameGoesHere; treeView1.BeforeExpand += MyMethodNameGoesHere; treeView1.BeforeExpand += MyMethodNameGoesHere; treeView1.BeforeExpand -= MyMethodNameGoesHere; // note the MINUS sign 
+5
source

What you have is right. You need to add a handler to the event, as you did in the second case.

There should be a line like this:

 button1.Click += button1_Click; 

(possibly with a new EventHandler() wrapper) somewhere in your Form1 code, most likely in a .designer.cs file.

+4
source

The first syntax, which is not syntax at all, is simply a naming convention for event handlers. What you do with the second syntax is to create a delegate for the event handler and add it to this event.

If you check out Form1 and click on the โ€œSelectโ€ button and look at its event properties, the event will most likely connect via the constructor. You can do the same with TreeView in your form through the constructor.

+1
source

The C # event processing system does not work in any naming convention (I think you can believe that it is?). A method called treeView1_BeforeExpand will not be called in the treeview1 BeforeExpand event unless you tell it to call this method on this particular event.

The code below says: "When the BeforeExpand event is fired, call the anyMethodNameYouLike method.

 treeView1.BeforeExpand += new TreeViewCancelEventHandler(anyMethodNameYouLike); 

You need to write your anyMethodNameYouLike method.

+1
source

In WinForms, you can bind event handlers from the designer using the "Properties" toolbar and clicking on the lightning button at the top. The events tab opens. Simply double-click the event name to create a handler in the code, or select an existing right-signed method from the drop-down list. This actually generates the "+ =" code for you in the .designer file.

The name Control_Event is automatically generated. However, you can use any name for your handlers and even register more than one.

+1
source

This is the right way to sign up for an event. The designer automatically adds this line to the .designer.cs file when you subscribe to this event through it.

In any case, this line of code is written somewhere. The difference is that one event is signed through the designer, and the other you sign manually.

0
source

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


All Articles