Events

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

    #1

    Events

    Can events be used to notify an object that something has happened and it
    needs to take some action?

    Most of the documentation on events makes them sound like they work the
    other way around, that is, an object raises an event to inform other objects
    that something happened. I need the ability for many objects to notify a
    single object that some event has occurred. e.g. similar to LostFocus.

    In C++, I would have used SendMessage() to inform the object that it needs
    to take some action but I can't find anything in C# to accomplish the same
    thing.

    Thanks,
    Tom


  • mia lanui

    #2
    Re: Events

    Well, the object you want notified by the other objects could have a public
    Notify() method. You could implement it asynchronously, if you need it to
    return immediately. Notify() could also take a parameter of this, indicating
    which caller called it...


    "Tom Bean" <tbean@t-a-c.com> wrote in message
    news:#v6Y3DGYDH A.1204@TK2MSFTN GP12.phx.gbl...[color=blue]
    > Can events be used to notify an object that something has happened and it
    > needs to take some action?
    >
    > Most of the documentation on events makes them sound like they work the
    > other way around, that is, an object raises an event to inform other[/color]
    objects[color=blue]
    > that something happened. I need the ability for many objects to notify a
    > single object that some event has occurred. e.g. similar to LostFocus.
    >
    > In C++, I would have used SendMessage() to inform the object that it needs
    > to take some action but I can't find anything in C# to accomplish the same
    > thing.
    >
    > Thanks,
    > Tom
    >
    >[/color]


    Comment

    • Chris Capel

      #3
      Re: Events

      If the event you need isn't already provided by the control (something like
      Leave) I don't see how you could avoid writing code in each of these other
      controls to call the notify event, or function, or whatever you use to
      implement it. So you're saying that you have to subclass all these controls
      because the events already given don't do what you need? I would agree with
      the last two posters. Either make each of your controls call a public method
      on the notifying control, with some sort of identification of the sender, or
      make a new event delegate and add that event to each of the notifying
      controls, let the class receiving the notification subscribe to each of
      those events, and then add logic in each of the controls with the events to
      fire the event when appropriate.

      Chris

      "Tom Bean" <tbean@t-a-c.com> wrote in message
      news:#1rAxcJYDH A.1056@TK2MSFTN GP10.phx.gbl...[color=blue]
      > Andrew,
      >
      > That would mean that all kinds of objects, like textboxes, comboboxes,
      > listboxes...wou ld have to have the same notify event. That's a lot of
      > duplicate code in the other objects to just be able to notify a single
      > object that something has happened,
      >
      > Tom
      >
      > "andrew lowe" <andrew [dot] lowe [at] geac [.] com> wrote in message
      > news:erxF%23xIY DHA.2476@tk2msf tngp13.phx.gbl. ..[color=green]
      > >
      > > "Tom Bean" <tbean@t-a-c.com> wrote in message
      > > news:%23v6Y3DGY DHA.1204@TK2MSF TNGP12.phx.gbl. ..[color=darkred]
      > > > Can events be used to notify an object that something has happened and[/color][/color]
      > it[color=green][color=darkred]
      > > > needs to take some action?
      > > >
      > > > Most of the documentation on events makes them sound like they work[/color][/color][/color]
      the[color=blue][color=green][color=darkred]
      > > > other way around, that is, an object raises an event to inform other[/color]
      > > objects[color=darkred]
      > > > that something happened. I need the ability for many objects to[/color][/color][/color]
      notify[color=blue]
      > a[color=green][color=darkred]
      > > > single object that some event has occurred. e.g. similar to LostFocus.[/color]
      > >
      > > Tom,
      > >
      > > Whats wrong with having the single object subscribe to the each of the[/color]
      > other[color=green]
      > > objects "notify" event(s) ?
      > >
      > > andrew
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Tom Bean

        #4
        Re: Events

        Chris,

        I did as you and the others recommended and implemented a public method that
        the other controls can call to notify the control about the event. After
        thinking about it, I suppose that's not any different than using
        SendMessage() in C++ which in the final analysis is just a call to a public
        method.

        Thanks,
        Tom

        "Chris Capel" <chris@ibanktec h.net.zerospam> wrote in message
        news:O37e%23COY DHA.1900@TK2MSF TNGP10.phx.gbl. ..[color=blue]
        > If the event you need isn't already provided by the control (something[/color]
        like[color=blue]
        > Leave) I don't see how you could avoid writing code in each of these other
        > controls to call the notify event, or function, or whatever you use to
        > implement it. So you're saying that you have to subclass all these[/color]
        controls[color=blue]
        > because the events already given don't do what you need? I would agree[/color]
        with[color=blue]
        > the last two posters. Either make each of your controls call a public[/color]
        method[color=blue]
        > on the notifying control, with some sort of identification of the sender,[/color]
        or[color=blue]
        > make a new event delegate and add that event to each of the notifying
        > controls, let the class receiving the notification subscribe to each of
        > those events, and then add logic in each of the controls with the events[/color]
        to[color=blue]
        > fire the event when appropriate.
        >
        > Chris
        >
        > "Tom Bean" <tbean@t-a-c.com> wrote in message
        > news:#1rAxcJYDH A.1056@TK2MSFTN GP10.phx.gbl...[color=green]
        > > Andrew,
        > >
        > > That would mean that all kinds of objects, like textboxes, comboboxes,
        > > listboxes...wou ld have to have the same notify event. That's a lot of
        > > duplicate code in the other objects to just be able to notify a single
        > > object that something has happened,
        > >
        > > Tom
        > >
        > > "andrew lowe" <andrew [dot] lowe [at] geac [.] com> wrote in message
        > > news:erxF%23xIY DHA.2476@tk2msf tngp13.phx.gbl. ..[color=darkred]
        > > >
        > > > "Tom Bean" <tbean@t-a-c.com> wrote in message
        > > > news:%23v6Y3DGY DHA.1204@TK2MSF TNGP12.phx.gbl. ..
        > > > > Can events be used to notify an object that something has happened[/color][/color][/color]
        and[color=blue][color=green]
        > > it[color=darkred]
        > > > > needs to take some action?
        > > > >
        > > > > Most of the documentation on events makes them sound like they work[/color][/color]
        > the[color=green][color=darkred]
        > > > > other way around, that is, an object raises an event to inform other
        > > > objects
        > > > > that something happened. I need the ability for many objects to[/color][/color]
        > notify[color=green]
        > > a[color=darkred]
        > > > > single object that some event has occurred. e.g. similar to[/color][/color][/color]
        LostFocus.[color=blue][color=green][color=darkred]
        > > >
        > > > Tom,
        > > >
        > > > Whats wrong with having the single object subscribe to the each of the[/color]
        > > other[color=darkred]
        > > > objects "notify" event(s) ?
        > > >
        > > > andrew
        > > >
        > > >[/color]
        > >
        > >[/color]
        >
        >[/color]


        Comment

        Working...