How to create ( not handle) a mouse/keyboard event ?

B

Boki

Hi All,

We can process the mouse/keyboard events by handling, however, how to
create a event on a target form ( ex: webbrowser ) ?

Thanks!

Best regards,
Boki.
 
A

Alberto Poblacion

Boki said:
We can process the mouse/keyboard events by handling, however, how to
create a event on a target form ( ex: webbrowser ) ?

You create an event inside any class, which includes a form if that is
what you wish. Any consumer referencing your class can then handle the
events that are fired by your class.

Creating the event in C# is a two-step process: You create a delegate
with the type of the event and then you create the event itself:

public delegate void MyEventHandler(object sender, EventArgs e);
public event MyEventHandler MyEvent;

Now you have to decide under which conditions you want to raise that
event. When those conditions are met in your class you raise the event by
calling it:

if (mycondition)
{
if (MyEvent!=null) MyEvent(this, EventArgs.Empty);
}

Notice the check for a null condition. This is to avoid an error in case
no one has subscribed to the event.
Notice also that we included two arguments, 'sender' and 'e'. This
follows the pattern that Microsoft established for all the events in the
Framework, but it is not a requirement for the event to work. You can define
events with any combination of arguments.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top