Click here to Skip to main content
15,899,474 members
Home / Discussions / C#
   

C#

 
AnswerRe: ico Pin
Nader Elshehabi12-Sep-06 8:47
Nader Elshehabi12-Sep-06 8:47 
GeneralRe: ico Pin
Judah Gabriel Himango12-Sep-06 9:02
sponsorJudah Gabriel Himango12-Sep-06 9:02 
AnswerRe: ico Pin
Judah Gabriel Himango12-Sep-06 9:01
sponsorJudah Gabriel Himango12-Sep-06 9:01 
JokeRe: ico Pin
Nader Elshehabi12-Sep-06 10:16
Nader Elshehabi12-Sep-06 10:16 
GeneralRe: ico Pin
TAREQ F ABUZUHRI12-Sep-06 11:37
TAREQ F ABUZUHRI12-Sep-06 11:37 
QuestionRe: ico Pin
The Only Nock7-Aug-08 16:36
The Only Nock7-Aug-08 16:36 
QuestionHow to Import an Excel file using ODBC connection Pin
JCMori12-Sep-06 7:14
JCMori12-Sep-06 7:14 
QuestionCentralising event handling in a Windows Form application Pin
GazzaJ12-Sep-06 7:00
GazzaJ12-Sep-06 7:00 
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); <br />
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) <br />
{ <br />
        MyEventHandler.ProcessEmployeeAddedEvent(sender, e); <br />
} 


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 not the best way though. My concern is the connection between the main form of the application and its link to userControl1. What if userControl1 was programatically created during run time, how would the main form know it could raise an the employee added event.

Any advice in this area would be gratefully appreciated.
AnswerRe: Centralising event handling in a Windows Form application Pin
Jun Du12-Sep-06 15:53
Jun Du12-Sep-06 15:53 
GeneralRe: Centralising event handling in a Windows Form application Pin
GazzaJ18-Sep-06 4:45
GazzaJ18-Sep-06 4:45 
GeneralRe: Centralising event handling in a Windows Form application Pin
Jun Du18-Sep-06 6:13
Jun Du18-Sep-06 6:13 
AnswerRe: Centralising event handling in a Windows Form application Pin
lmoelleb12-Sep-06 21:21
lmoelleb12-Sep-06 21:21 
GeneralRe: Centralising event handling in a Windows Form application Pin
GazzaJ18-Sep-06 4:46
GazzaJ18-Sep-06 4:46 
QuestionHow to get Image from other Image quickly Pin
Libor Tinka12-Sep-06 6:23
Libor Tinka12-Sep-06 6:23 
AnswerRe: How to get Image from other Image quickly Pin
Jun Du12-Sep-06 7:12
Jun Du12-Sep-06 7:12 
GeneralRe: How to get Image from other Image quickly Pin
Libor Tinka12-Sep-06 8:58
Libor Tinka12-Sep-06 8:58 
AnswerRe: How to get Image from other Image quickly Pin
Ranjan Banerji12-Sep-06 17:08
Ranjan Banerji12-Sep-06 17:08 
QuestionCompiler Error Numbers Pin
Brady Kelly12-Sep-06 5:39
Brady Kelly12-Sep-06 5:39 
AnswerRe: Compiler Error Numbers Pin
User 665812-Sep-06 5:47
User 665812-Sep-06 5:47 
AnswerRe: Compiler Error Numbers Pin
Rick Crone14-Sep-06 8:16
Rick Crone14-Sep-06 8:16 
QuestionDatabase Access in UITypeEditors Pin
nlgarvey12-Sep-06 5:20
nlgarvey12-Sep-06 5:20 
QuestionCompiler Warnings Pin
gantww12-Sep-06 5:06
gantww12-Sep-06 5:06 
AnswerRe: Compiler Warnings Pin
User 665812-Sep-06 6:00
User 665812-Sep-06 6:00 
QuestionHow to get a return value from a winservice at cmd batch: net start ... Pin
MDT Daniel12-Sep-06 5:05
MDT Daniel12-Sep-06 5:05 
QuestionHow to create my own setup dialog form for Setup Pin
Nicklez12-Sep-06 4:32
Nicklez12-Sep-06 4:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.