Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call an event manually in C#?

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.

like image 229
Saeed Avatar asked Oct 01 '10 16:10

Saeed


Video Answer


3 Answers

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:

public delegate void MyEventHandler(object sender, MyEventArgs e)

public event MyEventHandler MyEvent;

public void RaisesMyEvent()
{
   ...

   if(MyEvent != null) //required in C# to ensure a handler is attached
      MyEvent(this, new MyEventArgs(/*any info you want handlers to have*/));
}

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.

like image 93
KeithS Avatar answered Dec 01 '22 01:12

KeithS


I was looking for an answer to this issue for me,

just do this

examble:

//this is the call to trigger the event:

 **lst_ListaDirectorios_SelectedIndexChanged(this, new EventArgs());**

//do that if you have the method signature in the same class as I do. (something like this below)
private void lst_ListaDirectorios_SelectedIndexChanged(object sender, EventArgs e)
        {
          //do something
         }

I hope this was useful for you.

like image 34
HoNgOuRu Avatar answered Dec 01 '22 01:12

HoNgOuRu


Typically, the event invokation is wrapped in a method named something like "On[EventName]" which validates that the delgate has one or more targets (event is not null), and then invokes it with the sender and any applicable arguments...so something like this is the typical pattern:

public event EventHandler SomethingHappened;
protected void OnSomethingHappend(EventArgs e)
{
    if (SomethingHappened != null)
        SomethingHappened(this, e);
}

Anything that needs to raise that event invokes that method (assuming its accessible).

If you simply want to pass the event along, then as a UserControl, you can probably just invoke the base "On[Event]" method, which is likely exposed. You can wire up the event handlers, too, to directly pass the event from a child control as the event of the parent control...so that txtFoo.KeyPress simply invokes the OnKeyPress method of the parent control.

like image 23
Steven Avatar answered Dec 01 '22 01:12

Steven