Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Centralising event handling in a Windows Form application

Centralising event handling in a Windows Form application

Scheduled Pinned Locked Moved C#
winformsdesigndata-structuresregexarchitecture
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GazzaJ
    wrote on last edited by
    #1

    I am trying to devise a system for centrally handling events generated by controls in my application and passing these events to other controls who may be interested in them. For example say I have an application where a control can add an employee to the system. The control does the necessary and then raises an event which is then passed to two other controls who may have to process this event. I would like to define the event in one place and for the controls who want notification of the event to simply register this fact and then receive the events. This is what I have so far but perhaps there is a better way, or maybe there is a nice design pattern which someone can point me to. I have a common class called EventHandlerCommon which defines the EventArgs for the event and defines a public delegate: public delegate void EmployeeAddedEventHandler(object sender, EmployeeAddedEventArgs e) The class then has a static method: public static void RegisterEmployeeAddedEventHandler(EmployeeAddedEventHandler sourceMethod) which adds the the delegate to an array of methods to call. This class also has another static method: public static void ProcessEmployeeAddedEvent(object sender, EmployeeAddedEventArgs e) which can be called when the event is raised and will pass it to all the delegates in the array. My main application has two user controls. Each has a constructor which registers their interest in the employee added event by creating a delegate and passing it to the static class described above: EmployeeAddedEventHandler del = new EmployeeAddedEventHandler(MyLocalEmployeeAddedEventHandler); MyEventHandler.RegisterEmployeeAddedEventHandler(del); The first user control actually generates the event so has: public event EmployeeAddedEventHandler EmployeeAdded; and raises this event when a button is pressed. The main form for the application has an event handler to handle this event: this.userControl1.EmployeeAdded += new EventHandlerCommon.EmployeeAddedEventHandler(this.userControl1_EmployeeAdded); This calls the static method to pass this event to all the controls interested: private void userControl11_EmployeeAdded(object sender, EmployeeAddedEventArgs e) { MyEventHandler.ProcessEmployeeAddedEvent(sender, e); } This seems to work ok. Each of the user controls registers their interest in the event and is passed the event when it is raised. Perhaps this is n

    J L 2 Replies Last reply
    0
    • G GazzaJ

      I am trying to devise a system for centrally handling events generated by controls in my application and passing these events to other controls who may be interested in them. For example say I have an application where a control can add an employee to the system. The control does the necessary and then raises an event which is then passed to two other controls who may have to process this event. I would like to define the event in one place and for the controls who want notification of the event to simply register this fact and then receive the events. This is what I have so far but perhaps there is a better way, or maybe there is a nice design pattern which someone can point me to. I have a common class called EventHandlerCommon which defines the EventArgs for the event and defines a public delegate: public delegate void EmployeeAddedEventHandler(object sender, EmployeeAddedEventArgs e) The class then has a static method: public static void RegisterEmployeeAddedEventHandler(EmployeeAddedEventHandler sourceMethod) which adds the the delegate to an array of methods to call. This class also has another static method: public static void ProcessEmployeeAddedEvent(object sender, EmployeeAddedEventArgs e) which can be called when the event is raised and will pass it to all the delegates in the array. My main application has two user controls. Each has a constructor which registers their interest in the employee added event by creating a delegate and passing it to the static class described above: EmployeeAddedEventHandler del = new EmployeeAddedEventHandler(MyLocalEmployeeAddedEventHandler); MyEventHandler.RegisterEmployeeAddedEventHandler(del); The first user control actually generates the event so has: public event EmployeeAddedEventHandler EmployeeAdded; and raises this event when a button is pressed. The main form for the application has an event handler to handle this event: this.userControl1.EmployeeAdded += new EventHandlerCommon.EmployeeAddedEventHandler(this.userControl1_EmployeeAdded); This calls the static method to pass this event to all the controls interested: private void userControl11_EmployeeAdded(object sender, EmployeeAddedEventArgs e) { MyEventHandler.ProcessEmployeeAddedEvent(sender, e); } This seems to work ok. Each of the user controls registers their interest in the event and is passed the event when it is raised. Perhaps this is n

      J Offline
      J Offline
      Jun Du
      wrote on last edited by
      #2

      For event/notification handling, there are .NET event model and Observer/Subject design pattern. How is yours different than these two?

      Best, Jun

      G 1 Reply Last reply
      0
      • G GazzaJ

        I am trying to devise a system for centrally handling events generated by controls in my application and passing these events to other controls who may be interested in them. For example say I have an application where a control can add an employee to the system. The control does the necessary and then raises an event which is then passed to two other controls who may have to process this event. I would like to define the event in one place and for the controls who want notification of the event to simply register this fact and then receive the events. This is what I have so far but perhaps there is a better way, or maybe there is a nice design pattern which someone can point me to. I have a common class called EventHandlerCommon which defines the EventArgs for the event and defines a public delegate: public delegate void EmployeeAddedEventHandler(object sender, EmployeeAddedEventArgs e) The class then has a static method: public static void RegisterEmployeeAddedEventHandler(EmployeeAddedEventHandler sourceMethod) which adds the the delegate to an array of methods to call. This class also has another static method: public static void ProcessEmployeeAddedEvent(object sender, EmployeeAddedEventArgs e) which can be called when the event is raised and will pass it to all the delegates in the array. My main application has two user controls. Each has a constructor which registers their interest in the employee added event by creating a delegate and passing it to the static class described above: EmployeeAddedEventHandler del = new EmployeeAddedEventHandler(MyLocalEmployeeAddedEventHandler); MyEventHandler.RegisterEmployeeAddedEventHandler(del); The first user control actually generates the event so has: public event EmployeeAddedEventHandler EmployeeAdded; and raises this event when a button is pressed. The main form for the application has an event handler to handle this event: this.userControl1.EmployeeAdded += new EventHandlerCommon.EmployeeAddedEventHandler(this.userControl1_EmployeeAdded); This calls the static method to pass this event to all the controls interested: private void userControl11_EmployeeAdded(object sender, EmployeeAddedEventArgs e) { MyEventHandler.ProcessEmployeeAddedEvent(sender, e); } This seems to work ok. Each of the user controls registers their interest in the event and is passed the event when it is raised. Perhaps this is n

        L Offline
        L Offline
        lmoelleb
        wrote on last edited by
        #3

        Take a look at the smart client software factory (Google it, the first search page should contain various resources regarding this). No need to reinvent the wheel.

        G 1 Reply Last reply
        0
        • J Jun Du

          For event/notification handling, there are .NET event model and Observer/Subject design pattern. How is yours different than these two?

          Best, Jun

          G Offline
          G Offline
          GazzaJ
          wrote on last edited by
          #4

          I am effectively using the .NET event model. My question seems to be how to handle controls that are programatically created. Say the main hub of my application deals with the events that controls can generate and passes these to the other controls to process. If the main hub programatically creates a user control, how does it know what events this can fire and hook up an event handler to these?

          J 1 Reply Last reply
          0
          • L lmoelleb

            Take a look at the smart client software factory (Google it, the first search page should contain various resources regarding this). No need to reinvent the wheel.

            G Offline
            G Offline
            GazzaJ
            wrote on last edited by
            #5

            This looks good although I am still on .NET 1.1 at the moment.

            1 Reply Last reply
            0
            • G GazzaJ

              I am effectively using the .NET event model. My question seems to be how to handle controls that are programatically created. Say the main hub of my application deals with the events that controls can generate and passes these to the other controls to process. If the main hub programatically creates a user control, how does it know what events this can fire and hook up an event handler to these?

              J Offline
              J Offline
              Jun Du
              wrote on last edited by
              #6

              GazzaJ wrote:

              controls can generate and passes these to the other controls to process

              Hope this link can help.

              GazzaJ wrote:

              If the main hub programatically creates a user control, how does it know what events this can fire and hook up an event handler to these?

              I believe that you need to raise and consume events in your custom control. This [link](programatically creates a user control) might be able to help.

              Best, Jun

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups