Event Handlers in WPF - Page 1
       by kirupa  |  1 March 2007

You've reached the last page! In the previous page, I discussed what the sender object will help you do, and in this page I will explain the event argument and wrap things up!

The Event
The second argument passed to our event handler is the event itself. The event really depends on when you want your control to fire a signal to your event handler. A generic event is what you see in my code called RoutedEventArgs for a button's Click event:

private void ButtonOkClicked(object sender, RoutedEventArgs e)
{
this.Close();
}

You will get more specialized event arguments depending on what you are trying to do, and those arguments provide you with greater flexibility to deal with these events. Let's say you have a textbox, and each time you type a character, you want to do something. For this scenario, you are looking at a very particular type of event argument - one that takes key presses into account.

Let's give our text box the name txtBoxMain, and let's bind a KeyDown event to an event handler called KeyCount. Visually in Expression Blend, you would simply enter your KeyCount event handler and press Enter:

[ again, you can use a visual approach to bind an event to an event handler ]

In code, you would do the following:

txtBoxMain.KeyDown += new KeyEventHandler(KeyCount);

Regardless of which approach (visual or code) that you took, your event handler would look like the following:

private void KeyCount(object sender, KeyEventArgs e)
{

}

Notice that my event argument is now KeyEventArgs. The KeyEventArgs class contains a lot of useful methods that I can use to do more with my event than just recognize that it happened. For example, the following code shows me displaying the letter/name of the key pressed in a message box:

private void KeyCount(object sender, KeyEventArgs e)
{
MessageBox.Show("Key pressed is: " + e.Key.ToString());
}

The following is the image you see when you run the above code and press the letter k:

[ by using Key I can determine which key was pressed ]

While this example dealt with keyboard arguments, you can bind similar events to their respective event handlers for the mouse, the stylus, etc. Depending on which event handler variation you use, the number of properties you can access will vary. The differences in code among the various actions are too minor to cover in detail in this article. It would also be a bit too boring!

To give you a flavor of the extent of the variation, your argument e's type may be MouseEventArgs for a mouse related event, and you may not have a Key structure  from which to determine key presses, but you will have access to the Left and Right mouse buttons. You can find similar yet distinct variations among the other input methods.

Conclusion
As you can see, event handling can be as easy or as complicated depending on what you are trying to accomplish. Actually, complicated may not be the right word, because one you become familiar with the syntax and when to use some of these tricks, many things become easier.

The goal of this article and other articles on this site (and I'm sure others) is to give you a brief overview of the common uses of a particular use of technology and cover in greater detail the subtle, not-so-common uses that cause endless frustration and sleepless nights. At least that is how I justify having the trivial Note boxes and providing similar sets of code for the same task haha.

Got a question or just want to chat? Comment below or drop by our forums (they are actually the same thing!) where a bunch of the friendliest people you'll ever run into will be happy to help you out!

When Kirupa isn’t busy writing about himself in 3rd person, he is practicing social distancing…even on his Twitter, Facebook, and LinkedIn profiles.

Hit Subscribe to get cool tips, tricks, selfies, and more personally hand-delivered to your inbox.

COMMENTS



1 | 2 | 3 | 4





SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.