I have a USerControll in which i have a textbox. I use the usercontrol in my form, I want to do something when somebody presses enter on the textbox. how can I do it?
if you tell me how to call an event manually, I can call usercontrol.keydown in textbox.keydown.
I have a USerControll in which i have a textbox. I use the usercontrol
Share
First, events can only be raised from code within the control that declares the event. So, your user control has to declare the custom event KeyDown in order to raise it. You cannot, for instance, raise KeyDown on a TextBox contained by your user control. However, you can declare your own KeyDown, and attach a handler to the TextBox’s KeyDown that will raise your own KeyDown.
Given this restriction, raising an event is easy:
Raising an event looks much like a method, because in essence that’s what you’re doing; you’re calling one or more method delegates that are assigned to the MultiCast delegate behind the scenes of your event. Think of it as assigning a method to an ordinary named delegate (like if you’d omitted the “event” keyword from the definition) and calling it from inside your code. the only difference between a true event and that is that an event can have more than one handler delegate attached to it, and will invoke all of them when raised.