Newbie: In what order do events happen?

J

Jamey Bon

So far I have not been able to find a comprehensive listing of in what
order events fire and exactly what they are (with adequate descriptions for
a newbie like me) for C#. I have many questions, like what is the
differnce between a click event and a mouse click event for a given
control. As mentioned before, I am also often confused about the order of
certain events.

So, if anyone can direct me to a comprehensive discussion of .Net events, I
would greatly appreciate it.

Thanks,

JB
 
P

Peter Duniho

[...]
So, if anyone can direct me to a comprehensive discussion of .Net
events, I would greatly appreciate it.

I have found the MSDN documentation to be pretty clear in this particular
area. The thing to keep in mind is that one group of events is a
high-level look at user interaction (eg "click"), while the other concerns
itself with the actual manipulations of the user input device (eg
"mousedown"). If you look the events up in MSDN, they do a pretty decent
job of explaining what causes each event to occur and in what order they
occur.

Note that a more general description organizing ALL events relative to
each other is probably impossible. Events that don't have anything to do
with each other also will have no reliable order relative to each other.

Pete
 
G

Guest

When I was a "n00b", I would bring up the events property sheet (click the
little lightning symbol at the top of the properties window for a highlighted
control, or form), and double -click to create stub code for each
eventhandler. Then, I'd have each eventhandler write out
System.Diagnostics.Debug.WriteLine("It's me, the " +sender.ToString() +"
event!");

You can watch the Output window for the messages.
Peter
 
M

Morten Wennevik [C# MVP]

Hi Jamey,

Events are messages that exist alongside your program. Usually they arecreated by windows in response to something a user does with the mouse or keyboard, but they can also be generated by code. When an event is created, nothing will happen unless something is 'subscribing' to this event. If something is subscribing to the event, all 'subscribers' will receive a message notifying the event, and once they get time to process the event, a bit of code will run.

myButton.Click += new EventHandler( theNameOfTheMethodThatShouldRunIfThisEventIsFired );

The code above says that we want to add a subscription to myButton's Click-event. The code inside the 'theNameOfTheMethodThatShouldRunIfThisEventIsFired' method will run in response to the event.

protected void theNameOfTheMethodThatShouldRunIfThisEventIsFired(object sender, EventArgs e)
{
}

The parameters are strictly defined, and sender will be a reference to the button firing this event (you can have several different buttons using the same event method). 'e' contains any useful information that may be passed with this event, although in case of a click event there will be none. The click event simply tells you the button has been clicked.

A MouseDown/Up event is a more specialized kind of event compared to Click and will fire anytime you click a mouse button.

myButton.MouseDown += new MouseEventHandler( theMethodThatShouldRunOnMouseDown );

The code above will cause 'theMethodThatShouldRunOnMouseDown' to executewhen you click a mouse button on top of myButton.

protected void theMethodThatShouldRunOnMouseDown(object sender, MouseEventArgs e)
{
}

This method will receive a MouseEventArgs, which contains information about the position of the mouse, which buttons was clicked etc.

At last, a Click event is simply a MouseDown event followed by a MouseUp event.

There is much more to events, but this may get you started.
Good Luck
 
J

Jamey Bon

=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
When I was a "n00b", I would bring up the events property sheet (click
the little lightning symbol at the top of the properties window for a
highlighted control, or form), and double -click to create stub code
for each eventhandler. Then, I'd have each eventhandler write out
System.Diagnostics.Debug.WriteLine("It's me, the " +sender.ToString()
+" event!");

You can watch the Output window for the messages.
Peter

Great tip. Thank you!
 

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