Using the Events Tab
As you may know, in the Visual Studio IDE for VB.NET, one uses the Objects and Procedures drop-down
lists in the Code Editor to have the system auto-generate event scaffolding. This works differently
in C#. A special interface is used to auto-generate event code scaffolding. To access it, first make
sure the form is active in its designer (of course, this interface works for other objects that are
seated on the form such as buttons). Next, open the Properties window (Properties Window on the View
menu, or the F4 key). Finally, access the Events tab by clicking the lightening bolt icon on the Properties
window toolbar.

There are three ways you can use the Events tab of the Properties window:
By double-clicking in the left hand column that displays the available
events, an event method scaffolding is created.
You can assign any event to an existing class method by selecting the
method from the drop-down list in the right-hand column of the Events tab.
You can assign an event to a newly created method by typing the new
method name in the right-hand column.
Tip: a shortcut is that you can also create a click event by double-clicking as object, such as a button,
in the form designer.
Let’s have a look at the first of these. If you double-click on the Click event in the left-hand column,
the following code will be generated:
this.Click += new System.EventHandler(this.Form1_Click);
...
private void Form1_Click(object sender,
System.EventArgs e) {
}
You can easily add code that displays a message box to the event method:
private void Form1_Click(object sender,
System.EventArgs e) {
MessageBox.Show ("I've been clicked!",
 "C# for Newbies", MessageBoxButtons.OK,
 MessageBoxIcon.Exclamation);
}
Now, as you’d expect, when the form is clicked, the message box is displayed.
As I noted earlier, you don’t have to use the default name for the event method (such as
Form1_Click). It is generated this way as a convenience for you when you double-click the event.
You can alternatively choose a different event-handler from the drop-down list.
Significantly, one event method can handle multiple events, and the events don’t have to be fired
by the same object. So this becomes a very good way to centralize processing of a particular set
of events (for example, all the click events of a number of different controls). You can use the
sender parameter within the event method to find out which object fired the event—and process
accordingly. (Using the sender parameter in this fashion may involve type casting, which I’ll
explain in detail in an article later in the C# for Newbies series.)
You can also use the Events pane to name the method that responds to a given event anything you’d
like by typing your name in the right-hand column.
For example, if you name the form’s SizeChanged event ICanNameThisWhatIWant, then the following
scaffolding is generated for the SizeChanged event:
this.SizeChanged += new System.EventHandler(this.ICanNameThisWhatIWant);
...
private void ICanNameThisWhatIWant(object sender,
System.EventArgs e) {
}
Once again, it’s easy to add code that displays a message box (or performs some other task) to the event method:
private void ICanNameThisWhatIWant(object sender,
System.EventArgs e) {
MessageBox.Show ("Named my way!",
 "C# for Newbies", MessageBoxButtons.OK,
 MessageBoxIcon.Exclamation);
}
With this in place, when the project is run, if the event is triggered (by a form SizeChanged event),
the code in the ICanNameThisWhatIWant method is processed, and the message box displayed.
Please note that throughout this article I’ve emphasized the code generated by the development environment.
Why? It’s because you should understand the code you need to work with events. The visual development
environment just doesn’t buy you that much in this area, and often times you will find it easier just to
work in code, ignoring the Events tab. (If I haven’t already emphasized the point, this is another way of
saying that you can just go ahead and add the code that would be generated yourself using the Code Editor,
so why bother fussing with the auto-generation mechanism once you get the hang of the thing?)
All this is all very well and good, and quite helpful if you can get by with the canned events that ship
with objects such as forms and buttons. But what if you want to create classes with your own events? How
are custom events fired? Where do you put the code that these events should process? To find the answer
to these questions, tune into the second part of this article.
|
|
Search Engine Optimization
 
Syndication Viewer
Our Web host:
IX WebHosting
|