generating events in user defined controls

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Liz - Newbie

    generating events in user defined controls

    I need to generate an event in a user defined control
    when I have changed a variable, so that I can pick it up
    in my main form and take it from there.

    Can someone explain the SIMPLEST way of doing this.

    Liz (newbie to OO)
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: generating events in user defined controls

    Liz,

    Assuming that your variable is exposed as a property, you can do the
    following:

    // Assume the property is named MyProperty. Define your event like this:
    public event EventHandler MyPropertyChang ed;

    This will define your event as type EventHandler. Then, when the value
    changes, you can make the following call:

    // Check to see if there are any events to fire.
    EventHandler pobjHandlers = MyPropertyChang ed;

    // If there are handlers, then fire the event.
    if (pobjHandlers != null)
    // Fire the event.
    pobjHandlers(th is, EventArgs.Empty );

    Then, your form would pick up the event like any other event. Also, the
    name of the event and the type are important here. The reason I suggested
    this is because naming the event like this makes your class fit more into
    the data binding model (the data grid and property grid will pick up on the
    property change, for example).

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m


    "Liz - Newbie" <ecollins@cornw all.gov.uk> wrote in message
    news:080301c3a7 8d$e78fbc90$a60 1280a@phx.gbl.. .[color=blue]
    > I need to generate an event in a user defined control
    > when I have changed a variable, so that I can pick it up
    > in my main form and take it from there.
    >
    > Can someone explain the SIMPLEST way of doing this.
    >
    > Liz (newbie to OO)[/color]


    Comment

    • Liz

      #3
      Re: generating events in user defined controls

      Bless you - it worked! I'm not certain quite how it all
      works but it worked. I can now use this as a basis for
      passing events back to my form. Thanks a million

      Liz


      [color=blue]
      >-----Original Message-----
      >Liz,
      >
      > Assuming that your variable is exposed as a[/color]
      property, you can do the[color=blue]
      >following:
      >
      >// Assume the property is named MyProperty. Define your[/color]
      event like this:[color=blue]
      >public event EventHandler MyPropertyChang ed;
      >
      > This will define your event as type EventHandler.[/color]
      Then, when the value[color=blue]
      >changes, you can make the following call:
      >
      >// Check to see if there are any events to fire.
      >EventHandler pobjHandlers = MyPropertyChang ed;
      >
      >// If there are handlers, then fire the event.
      >if (pobjHandlers != null)
      > // Fire the event.
      > pobjHandlers(th is, EventArgs.Empty );
      >
      > Then, your form would pick up the event like any[/color]
      other event. Also, the[color=blue]
      >name of the event and the type are important here. The[/color]
      reason I suggested[color=blue]
      >this is because naming the event like this makes your[/color]
      class fit more into[color=blue]
      >the data binding model (the data grid and property grid[/color]
      will pick up on the[color=blue]
      >property change, for example).
      >
      > Hope this helps.
      >
      >
      >--
      > - Nicholas Paldino [.NET/C# MVP]
      > - mvp@spam.guard. caspershouse.co m
      >
      >
      >"Liz - Newbie" <ecollins@cornw all.gov.uk> wrote in[/color]
      message[color=blue]
      >news:080301c3a 78d$e78fbc90$a6 01280a@phx.gbl. ..[color=green]
      >> I need to generate an event in a user defined control
      >> when I have changed a variable, so that I can pick it[/color][/color]
      up[color=blue][color=green]
      >> in my main form and take it from there.
      >>
      >> Can someone explain the SIMPLEST way of doing this.
      >>
      >> Liz (newbie to OO)[/color]
      >
      >
      >.
      >[/color]

      Comment

      Working...