Event Handler

Eduardo Lorenzo

Regular
Joined
Jun 27, 2006
Messages
87
I am a newbie to C# and I am sure this is a very basic question for you guys.

In vb, we had a "handles" for the code block right?

What is the version for this in C#?

Thing is, in VB I had the habit of adding another control's event to a common control. And I can't seem to be able to do it in C#.

Thanks in advance for helping out a newbie!
 
e.g.,
Public Sub Test(ByVal sender As Object, ByVal e As EventArgs) Handles SomeObject.SomeEvent
End Sub

would be:
public void InitializeComponent()
{
//INSTANT C# NOTE: Converted event handlers:
SomeObject.SomeEvent += new System.EventHandler(Test);

}
public void Test(object sender, EventArgs e)
{
}
 
Thanks so much.

you wouldn't happen to know how to convert this:

Public Sub Test(ByVal sender As Object, ByVal e As EventArgs) Handles SomeObject.SomeEvent, SecondObject.SomeEvent, ThirdObject.SomeEvent
End Sub
 
The actual output from our Instant C# VB to C# converter is:

//TODO: INSTANT C# TODO TASK: Insert the following converted event handlers at the end of the 'InitializeComponent' method for forms, 'Page_Init' for web pages, or into a constructor for other classes:
SomeObject.SomeEvent += new System.EventHandler(Test);
SecondObject.SomeEvent += new System.EventHandler(Test);
ThirdObject.SomeEvent += new System.EventHandler(Test);

public void Test(object sender, EventArgs e)
{
}

As stated, the event wireups should be added somewhere like InitializeComponent or some other method that executes on start up of the form.
 
Code in InitializeComponent

As stated, the event wireups should be added somewhere like InitializeComponent or some other method that executes on start up of the form.

For forms, I would suggest placing any custom event linking code in the constructor, after the call to InitializeComponent, since the comments generated by the forms designer clearly indicate that the InitializeComponent method should not be manually edited using the code editor.

Also, C# 2.0 supports a slightly more concise method of specifying a delegate:

C#:
ThirdObject.SomeEvent += new System.EventHandler(Test);
//Can be rewritten as:
ThirdObject.SomeEvent += Test;

Good luck :)
 
Re: Code in InitializeComponent

Eduardo, I'm glad the problem seems to be resolved, but I have a recommendation. Investigate the AddHandler keyword in VB and see how it relates to the Handles clause. C#'s use of += is the same as VB's use of AddHandler, which can produce the same effect as Handles. Being familiar with the relationship between these things will give you a thorough understanding of how events work in C#.
 
Re: Code in InitializeComponent

For forms, I would suggest placing any custom event linking code in the constructor, after the call to InitializeComponent, since the comments generated by the forms designer clearly indicate that the InitializeComponent method should not be manually edited using the code editor.

Yes, but this is where event wireups normally belong. In addition, our testing showed that code inserted into the InitializeComponent method that normally 'belongs' in that method (such as event wireups) will not be trashed by the designer.
 
Re: Code in InitializeComponent

Eduardo, I'm glad the problem seems to be resolved, but I have a recommendation. Investigate the AddHandler keyword in VB and see how it relates to the Handles clause. C#'s use of += is the same as VB's use of AddHandler, which can produce the same effect as Handles. Being familiar with the relationship between these things will give you a thorough understanding of how events work in C#.

Thanks marble_eater for the advice. I do have plans of understanding C# as deep as possible but am afraid I was thrown into the C# cauldron quite unceremoniously. I was added to this project without formal core concept training in C#.
 
Back
Top