Event handling

  • Thread starter Thread starter VC
  • Start date Start date
V

VC

I created an usercontrol ACTRL. and I fired an event from the ACTRL.
in my form application, do all ACTRL controls have to do "+=" to sign
up the event? my problem is if the ACTRL control in the form app
doesn't sign up the event, the application will throw exception
"Object reference not set to an instance of an object" during the run
time.
 
VC,

If you want to hook up a method on ACTRL to an event on the form, then
yes, you have to sign up for the event using the += operator. Or, if you
want other methods to be hooked up to an event that ACTRL fires, then you
need to hook those up using the same operator.

Hope this helps.
 
Nicholas,

Thank you very much for your feedback. I have read a lot of messages
you posted in Google Group, they are very helpful.

In my project(C#,.NET). some of usercontrols(ACTRL) need hook up to
the event that ACTRL fires. and some of them don't need. right now the
situation is that all of them have to be signed up to the event by
"+=". otherwise they throw "Object reference not set to an instance of
an object" exception.
what I want to do is if you need receive the event from the ACTRL,
sign up "+=" to event. if not, don't sign up. please let me know
anything wrong in my code.


Here is my code look like:

public delegate void InkCtrlUpdatedEvent(object o, string str);

public class ACTRL: System.Windows.Forms.UserControl
{
:
:
public event InkCtrlUpdatedEvent InkCtrlCellUpdated;
:
:
private void fireEvent(string str)
{
InkCtrlCellUpdated(this,str);
}
}

Again, thanks for the help.
 
Try putting changing your fireEvent method to have the following code:

if(null != InkCtrlCellUpdated)
{
InkCtrlCellUpdated(this,str);
}

- Scott Wagner
 
Back
Top