Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Events
Message
From
14/11/2004 11:32:23
 
General information
Forum:
ASP.NET
Category:
Forms
Title:
Re: Events
Miscellaneous
Thread ID:
00961252
Message ID:
00961259
Views:
5
Stephane,

I have a simple example that I typically use for explaining this, but I've modified it a bit to fit what you're asking about:

Here is the Custom Event on your form, you need the following things:
// First you must make a public EventHandler:
public event EventHandler MyEvent;

// Then you need this code to raise the MyEvent event:
protected virtual void OnMyEvent(EventArgs e)
{
	if (MyEvent != null)
		MyEvent(this, e);
}
Then, your TextBoxes would need to have the usual delegates and EventHandlers for the KeyPress event:
this.MyTextBox.KeyPress += new KeyPressEventHandler(this.MyTextBox_KeyPressHandler);

protected virtual void KeyPressHandler(object sender, KeyPressEventArgs e)
{
    // Here is where you need to fire your Custom event:
    this.OnMyEvent(new EventArgs());
}

// And all other controls that need to subscribe to the event would have
// the usual delegates and EventHandlers:
this.MyLabel.MyEvent += new System.EventHandler(this.MyLabel_MyEventHandler);

private void MyLabel_MyEventHandler(object sender, System.EventArgs e)
{
	// whatever your code needs to be
}
Hope this helps.

~~Bonnie



>Hi all, i've been browsing documentation for a few days now and i'm still unclear about all this events and delegates thingny. What i'd like to see is a simple form example doing the following:
>
>a form with 1 label and 2 textbox. when the keypress event of one the textbox is fired, all other object are notified by suscribing to a custom event of the form. The code for the event handling would be in the derived class of the textbox and the label.
>
>Thanks.
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform