Where to register for C # events?

I am currently transitioning from VB to C # and I have some problems registering my interest in the event.

When using VB, this was just a case of specifying a method Handlesand an event, often this was generated using a list of object events. Although I can easily use Class.event += delegatein C #, I'm not sure where the best place to put code for this is.

I best place it inside InitializeComponent()according to the generated code (say, if you select an event in the designer), or I should put it inside the constructor for better readability / maintenance. If inside the constructor should be before or after the call InitializeComponent()?

+5
source share
8 answers

When you do WinForm development (judging by the mentioned InitializeComponent () function), you usually assign a handler using Visual Studio. You look at the properties of your control, click on the lightning bolt to get a list of all the events, find your event and double-click it (to create a new handler) or select an existing handler from the list. Visual Studio will add the wiring of this to the generated code, so you don't have to worry about it.

+5
source

Init() , Form_Load. , , . InitializeComponent(), , - , .

+3

Visual Studio , InitializeComponent , -

public Form1(){
   InitializeComponent();
   WireUpEvents();
}

public void WireUpEvents(){
   this.fooEvent += new EventHandler(foo_handler);
   .... etc ....
}

, Form Dispose...

public void UnWireEvents(){
   this.fooEvent -= new EventHandler(foo_handler);
   .... etc ....
}

, Visual Studio InitializeComponent(), form.design.cs, , .

+2

, , .

InitializeComponent, , ( , Forms/UserControls/etc.). .

, ( - =), , . .

+1

InitializeComponent(). , , , , , .

Form Load .

+1

InitializeComponent(), , , . , , . , .

, InitializeComponent , , ( Dispose()).

0

2 . , , , , , . , Finalizer/Destructor.

0

InitializeComponent, /, , , .

, / , , - .

0
source

All Articles