Eduardo Lorenzo Posted May 25, 2007 Posted May 25, 2007 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! Quote
David Anton Posted May 25, 2007 Posted May 25, 2007 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) { } Quote
Eduardo Lorenzo Posted May 25, 2007 Author Posted May 25, 2007 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 Quote
David Anton Posted May 25, 2007 Posted May 25, 2007 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. Quote
Eduardo Lorenzo Posted May 25, 2007 Author Posted May 25, 2007 thank you sooooooo much! I placed it in the Page_init and it works like a charm! Quote
MrPaul Posted May 25, 2007 Posted May 25, 2007 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: ThirdObject.SomeEvent += new System.EventHandler(Test); //Can be rewritten as: ThirdObject.SomeEvent += Test; Good luck :) Quote Never trouble another for what you can do for yourself.
Leaders snarfblam Posted May 25, 2007 Leaders Posted May 25, 2007 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#. Quote [sIGPIC]e[/sIGPIC]
David Anton Posted May 25, 2007 Posted May 25, 2007 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. Quote
Eduardo Lorenzo Posted May 27, 2007 Author Posted May 27, 2007 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#. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.