Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Events
Message
From
14/11/2004 12:42:20
 
General information
Forum:
ASP.NET
Category:
Forms
Title:
Re: Events
Miscellaneous
Thread ID:
00961252
Message ID:
00961270
Views:
6
Well, off the top of my head, you could probably handle this with Interfaces. IOW, have an interface defined that implements the custom Event. Have your form implement that Interface. Then, in the KeyPress event of your TextBox class, you could check whether the parent implements that Interface and subscribe to the custom event if it does. In order to find the parent, you will probably have to recursively iterate through this.Parent until you've found the Interface implentation or it's null. If you're not familiar with interfaces at all, you'll probably need me to explain this a little further with some code, but if you can follow what I'm saying, then go for it! <g>

~~Bonnie



>Thanks Bonnie!
>
>Does this meen i have to put all this code in the form everytime i add a object to it ? I would like to code this in a subclass of a textbox and everytime i would add this textbox to a form it would susbcribe to the event of the form without adding code to the form... Is this possible ?
>
>
>>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