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
source share