C Sharp Events and Event Parameters

From Techotopia
Revision as of 19:58, 28 January 2008 by Neil (Talk | contribs) (New page: In Understanding C# Events we looked at the basics of events in applications with graphical user interfaces. In this chapter we will extend this knowledge ...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

In Understanding C# Events we looked at the basics of events in applications with graphical user interfaces. In this chapter we will extend this knowledge base with an overview of how to use event parameters to obtain more information about an event in C#.


Contents


The Anatomy of an Event Handler

In the previous chapter we looked at some basic events. One such event was triggered by the user clicking on a button in a Windows Form. The signature of the button click event appeared as follows:

        private void button1_Click(object sender, EventArgs e)
        {

        }

The event handler show above is triggered when a user clicks on a button named button1. The event handler also receives two objects as parameters, sender and e.

The EventArgs object e is an object which contains a number of properties which can be accessed in the event handler to learn more about the event itself.

A C# EventArgs Example

Perhaps one of the scenarios where the e object is of most use is in the case of a mouse event. In the remainder of this chapter, therefore, we will work through an example where we will handle a mouse event in a Windows Form and obtain information about the mouse click via the e parameter.

Begin by starting Visual Studio and creating a new Windows Forms project called CSharpEvents. When the project has been created select the Form in the design area and add a Label control. In the properties panel change the name of the label to infoLabel.

Select the Form again and click on the lightening bolt button at the top of the properties panel. This will display a list of available events for the Form object. Scroll down the list and double click on the MouseClick event:

Selecting a Mouseclick event on a Windows Form

Once the MouseClick event has been select by double clicking the code for the event will be displayed. This code will appear as follows:

       private void Form1_MouseClick(object sender, MouseEventArgs e)
       {

       }

C# EventArg Object Properties

Now that we have a simple Form design and the outline of a MouseClick event handler the next step is to start interrogating the e event parameter object to learn a little more about the event that triggered to the call the Form1_MouseClick() event handler.

Within the code outline for the methods type e.. Visual Studio will then popup a list of available properties for this event object.

A list of properties available from a C# MouseClick event parameter

A number of useful properties are available to us in our C# code within this event handler. For the purposes of this example we are interesting in finding out which mouse button was pressed by the user and the X and Y co-ordinates. As such, we need to access the Button, X and Y properties.

The X and Y properties are easy to handle since they are simply integer values indicating where the moise pointer was in the form when the user clicked a mouse button. As such we can simplay take these values and display them in our infoLabel control:

       private void Form1_MouseClick(object sender, MouseEventArgs e)
       {
            int x = e.X;
            int y = e.Y;

            infoLabel.Text = "Mouse Clicked at " + x + " , " + y;
       }

After entering the above code press the F5 to compile and run the application. Once tha program is running click at various points in the Form and watch the label change to report the co-ordinates of the click.

Identify Which Mouse Button was Clicked

Amongst the properties of the MouseClick EventArgs object was a property named Button which may be accessed to identify which mouse button was clicked.

The Button' properties has a number of predefined possible values, namely MouseButton.Left, MouseButton.Right and MouseButtons.Middle. With knowledge of these possible property settings we can construct a case statement to identify which button was pressed and report this using the infoLabel control:

        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            int x = e.X;
            int y = e.Y;
            string buttonStr = "";

            switch (e.Button)
            {
                case MouseButtons.Right:
                    buttonStr = "Right";
                    break;
                case MouseButtons.Left:
                    buttonStr = "Left";
                    break;
                case MouseButtons.Middle:
                    buttonStr = "Middle";
                    break;
            }
            infoLabel.Text = buttonStr + " Mouse Clicked at " + x + " , " + y;

        }

Which the above change completed press F5 to build and run the application. Clicking in the form of the running application should update the infoLabel control as follows:

The C# MouseClick event example running


</pre>