Where to register for C# events?
I am currently transitioning from VB to C# and am having some issues with regards to registering my interest in an event.
When using VB it was simply a case of specifying that a method Handles
a开发者_StackOverflow中文版nd event, often this was generated by using the object events list. While I can easily use the Class.event += delegate
in C# I am unsure where the best place is to place the code to do this.
Am I best placing it inside of the InitializeComponent()
as per the generated code (say if you select the event in the from designer) or should I place it inside the constructor for better readability/maintenance. If inside the constructor, should it be before or after the call to InitializeComponent()
?
When you are doing WinForm development (judging from InitializeComponent() function mentioned), usually you assign the handler using Visual Studio. You look up the properties of your control, click on the lightning icon to get the list of all events, find your event, and either double click on it (to create a new handler), or select existing handler from the list. Visual Studio will add the wiring of this in the generated code, so you don't have to worry about it.
I always create a private method called Init()
and place it there, and then call that method from the constructor or the Form_Load event handler. It's semantically better, IMO, than doing it within the constructor proper. And you don't want to place it within InitializeComponent()
, because next time you change something in your designer it's likely to delete any manually-added code there.
Sometimes Visual Studio's designer can mess up the code, so adding the event handlers within InitializeComponent
can create a headache, it would be better to do it something like this
public Form1(){ InitializeComponent(); WireUpEvents(); } public void WireUpEvents(){ this.fooEvent += new EventHandler(foo_handler); .... etc .... }
And make sure that you remove the event handlers in the Form's Dispose function also...
public void UnWireEvents(){ this.fooEvent -= new EventHandler(foo_handler); .... etc .... }
As you design the form, Visual Studio will change the code within the InitializeComponent()
method located in form.design.cs
, so it is imperative that you do not manually edit this code..
It depends, but most of the time, yes.
Use InitializeComponent when you want the event to be hooked for the entire duration of the Form (I'm assuming you're talking about Forms/UserControls/etc.). In other cases, you'll want finer grained control of when the Event is handled.
Keep in mind that you'll want to unhook all of these events (using the -= syntax) when you're Disposing the Form, or no longer want to handle the event. Keeping the event handler delegates attached is one of the most common managed memory leaks around.
Do not manually add code to the InitializeComponent()
method. This method is code generated, so as soon as you change your form, any logic that you've added manually to this method will be wiped out.
I usually add a method to handle the Form's Load
event and put my event registrations there.
If you have the InitializeComponent()
method you're using the designer so you can bind events directly in the designer if you like. To do this, click the lightning bolt icon in the properties window and you'll see a list of all the events for the selected object. You can just type the name of the event in there and it'll create the code for you.
If you're not a fan of the designer, bind them after your InitializeComponent
call and make sure you detach them when you're done (in Dispose()
).
2 ways of doing this. You can either create you own method which you call in your Constructor which in turn creates the Event Handler, or you can just place them in your Constructor. Probably a good idea to remove the Event Handlers in your Finalizer/Destructor code.
I would place it after InitializeComponent, since you might be registering events against a child control/object, like a button, and you will want to be sure the object has been created already.
There will be cases where you wire up to events dynamically/conditionally in other places, such as in response to some other event.
精彩评论