Click here to Skip to main content
16,003,474 members
Home / Discussions / C#
   

C#

 
GeneralProblem in Mobile Internet Toolkit Beta 2 Pin
9-Jan-02 2:30
suss9-Jan-02 2:30 
GeneralProblem in Calling Unmanaged code Pin
8-Jan-02 18:16
suss8-Jan-02 18:16 
Generalclient code in C# and server code in C# and support COM+ Pin
7-Jan-02 23:50
suss7-Jan-02 23:50 
General"event bubbling" for winform control Pin
kasturirawat7-Jan-02 12:43
kasturirawat7-Jan-02 12:43 
GeneralRe: "event bubbling" for winform control Pin
Senkwe Chanda8-Jan-02 21:35
Senkwe Chanda8-Jan-02 21:35 
GeneralRe: "event bubbling" for winform control Pin
kasturirawat9-Jan-02 6:05
kasturirawat9-Jan-02 6:05 
GeneralRe: "event bubbling" for winform control Pin
Senkwe Chanda9-Jan-02 20:59
Senkwe Chanda9-Jan-02 20:59 
GeneralRe: "event bubbling" for winform control Pin
Senkwe Chanda11-Jan-02 3:26
Senkwe Chanda11-Jan-02 3:26 
Here's a sample EventsManger class. All it does is check to see if there are any listeners for registered events. As you can see it's very simple.

public class EventsManager
{
	public delegate void EventHandler(Object sender);
	public event EventHandler EventsHolder;

        protected virtual void OnEventOccurred()
	{
		if(EventsHolder != null) //meaning there is a listener for the event somewhere
			EventsHolder(this);
	}		

	public void EventWasFired()
	{
		OnEventOccurred();
	}
}

I prefer to have my entry point in a class called driver (I'm used to C++) so I declare the following...
public class driver	
{
	//declare an events manager variable, make it <code>public</code> <code>static</code> so you can query it from all classes in your
	//namespace
	public static EventsManager em;
	
	static void Main() 
	{
	    em = new EventsManager();
	    Application.Run(new Form1());
	}
}

To test it, I created a user control with a button on it. i.e. a composite control. Now, I want that when I press the button, the user control back color turns red, ie, the button_click event gets bubbled up to the parent control so to speak. To make things easy, you can make your user control take an EventsManager as a constructor paramater.Then register a listener like so...
public UserControl1(EventsManager em)
{			
    InitializeComponent();
    em.EventsHolder += new EventsManager.EventHandler(ButtonWasPressed);			
}

private void ButtonWasPressed(Object sender)
{
    this.BackColor = Color.Red;
}

You can then fire the event from the button's OnClick event like so
private void button1_Click(object sender, System.EventArgs e)
{
    driver.em.EventWasFired();
}

You'll notice that I don't use EventArgs in my delegate declaration. I left that out because it's of no real use here. But it's advisable fro you to do this. (I typically derive a class from EventArgs and stuff it with mty own event specific code)

Well it's ugly but it works for me, hope it helps

Just another wannabe code junky
GeneralProxy Settings in C# Pin
Ray Hayes5-Jan-02 4:38
Ray Hayes5-Jan-02 4:38 
GeneralASP.NET Pin
Matt.W.3-Jan-02 11:59
Matt.W.3-Jan-02 11:59 
GeneralCacheItemRemovedCallback Problems Pin
Tim Bajz3-Jan-02 3:13
Tim Bajz3-Jan-02 3:13 
GeneralConsole app question Pin
Krista Crawley-Archer3-Jan-02 2:37
Krista Crawley-Archer3-Jan-02 2:37 
GeneralRe: Console app question Pin
James T. Johnson4-Jan-02 11:46
James T. Johnson4-Jan-02 11:46 
GeneralRe: Console app question Pin
Krista Crawley-Archer4-Jan-02 11:50
Krista Crawley-Archer4-Jan-02 11:50 
GeneralRe: Console app question Pin
James T. Johnson4-Jan-02 13:13
James T. Johnson4-Jan-02 13:13 
GeneralRe: Console app question Pin
Valer BOCAN24-Jan-02 21:17
Valer BOCAN24-Jan-02 21:17 
Generalcasting (headaches) Pin
Senkwe Chanda3-Jan-02 0:22
Senkwe Chanda3-Jan-02 0:22 
GeneralRe: casting (headaches) Pin
James T. Johnson3-Jan-02 0:42
James T. Johnson3-Jan-02 0:42 
GeneralRe: casting (headaches) Pin
Senkwe Chanda3-Jan-02 0:53
Senkwe Chanda3-Jan-02 0:53 
GeneralRe: casting (headaches) Pin
James T. Johnson3-Jan-02 1:07
James T. Johnson3-Jan-02 1:07 
GeneralInstr Pin
2-Jan-02 14:04
suss2-Jan-02 14:04 
GeneralRe: Instr Pin
James T. Johnson2-Jan-02 18:14
James T. Johnson2-Jan-02 18:14 
Generalobtaining drive information Pin
hotlemonade2-Jan-02 13:47
hotlemonade2-Jan-02 13:47 
GeneralWin32 APi with C# Pin
kasturirawat31-Dec-01 13:54
kasturirawat31-Dec-01 13:54 
GeneralRe: Win32 APi with C# Pin
James T. Johnson31-Dec-01 19:59
James T. Johnson31-Dec-01 19:59 

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.