Event handling

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • VC

    Event handling

    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.
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Event handling

    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 Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "VC" <usa_2001us@yah oo.com> wrote in message
    news:be347d2c.0 311111133.1ac43 888@posting.goo gle.com...[color=blue]
    > 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.[/color]


    Comment

    • VC

      #3
      Re: Event handling

      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(AC TRL) 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 InkCtrlUpdatedE vent(object o, string str);

      public class ACTRL: System.Windows. Forms.UserContr ol
      {
      :
      :
      public event InkCtrlUpdatedE vent InkCtrlCellUpda ted;
      :
      :
      private void fireEvent(strin g str)
      {
      InkCtrlCellUpda ted(this,str);
      }
      }

      Again, thanks for the help.




      usa_2001us@yaho o.com (VC) wrote in message news:<be347d2c. 0311111133.1ac4 3888@posting.go ogle.com>...[color=blue]
      > 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.[/color]

      Comment

      • Scott C. Wagner

        #4
        Re: Event handling

        Try putting changing your fireEvent method to have the following code:

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

        - Scott Wagner

        VC wrote:[color=blue]
        > 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(AC TRL) 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 InkCtrlUpdatedE vent(object o, string str);
        >
        > public class ACTRL: System.Windows. Forms.UserContr ol
        > {
        > :
        > :
        > public event InkCtrlUpdatedE vent InkCtrlCellUpda ted;
        > :
        > :
        > private void fireEvent(strin g str)
        > {
        > InkCtrlCellUpda ted(this,str);
        > }
        > }
        >
        > Again, thanks for the help.
        >
        >
        >
        >
        > usa_2001us@yaho o.com (VC) wrote in message news:<be347d2c. 0311111133.1ac4 3888@posting.go ogle.com>...
        >[color=green]
        >>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.[/color][/color]

        Comment

        Working...