Event Handling in windows programming using C#


This article has been excerpted from book "The Complete Visual C# Programmer's Guide from the Authors of C# Corner".

Event handling is performed differently in .NET than it is in Visual C++. In Visual C++, each control is derived from CWnd and a message map handles any events. The message map concept doesn't exist in .NET. The Control class, or any derived class, has virtual functions, which can be overridden to raise an event. Therefore, a Form class can use any event handler in its hierarchy. For example, the Control class has many event methods including GotFocus, ControlRemoved, LostFocus, and MouseWheel. Table 9.7 lists some common event-handling methods.

table-9.7.gif

Table 9.7: Windows Controls Events

To override the OnPaint method, for example, add the following method to your new form or control:


        protected override void OnPaint(PaintEventArgs e)
        {
            // your code here
        }


Adding Event Handlers at Design-Time

To attach events to a control at design-time, use the Properties window. Right-click the control and click the Properties menu item. Select the Events tab by pressing the lightening button at the top of the Properties window. Now, pick an event and type the corresponding function name. Alternatively, you could double-click in the field next to the event and VS.NET will choose a name for you. In Figure 9.17, we add a button click event handler as button1_Click.

Fig9.17.gif

Figure 9.17: Adding an Event Handler Using the Properties Window

This action adds a delegate to the event, which looks like the following:


       
this.button1.Click += new System.EventHandler(this.button1_Click);

The event handler method takes the following form:


        private void button1_Click(object sender, System.EventArgs e) { }


Now write whatever code, within the method, you wish to execute after the event takes place. You can use the "e" (event argument) parameter that is passed to retrieve any pertinent information about the event, such as which key was pressed. The sender parameter is the control that initiated the event, in this case, the button1 member.

Handling Mouse Events

The window in Figure 9.18 lists multiple mouse events. Event handlers can be generated simply by double-clicking the desired event.

Fig9.18.gif

Figure 9.18: Mouse Events

To test the sample code that follows, create a Windows application and write the OnMouseDown and OnMouseMove event handlers in the InitializeComponent method.


this
.MouseDown += new System.Windows.Forms.MouseEventHandler(this.OnMouseDown); 

this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.OnMouseMove);

The preceding code results in the OnMouseDown method being called if a mouse button is pressed and the OnMouseMove method being called whenever the mouse moves over the control, which in this case is the form.

To carry out some action after the mouse event occurs, we need to write the event handlers. The second parameter of the event handler method is a System.WinForms.MouseEventArgs object, which details the mouse's state. The MouseEventArgs' members are listed in Table 9.8.

table-9.8.gif

Table 9.8: MouseEventArgs Members

Listing 9.7 shows the event handler code for the MouseDown and MouseMove events.

Listing 9.7: Mouse Event Handlers


        public void OnMouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            switch (e.Button)
            {
                case MouseButtons.Left:
                    MessageBox.Show(this, "Left Button Click");
                    break;
                case MouseButtons.Right:
                    MessageBox.Show(this, "Right Button Click");
                    break;
                case MouseButtons.Middle:
                    break;
                default:
                    break;
            }
        }

        private void OnMouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            this.Text = "Mouse Position:" + e.X.ToString() + "," + e.Y.ToString();
        }


Figure 9.19 shows the output of Listing 9.7. A mouse click displays, in a message box, the mouse button clicked, while a mouse move shows the mouse's coordinates as the title of the form.

Fig9.19.gif

Figure 9.19: Handling Mouse Click and Mouse Move Events

Conclusion

Hope this article would have helped you in understanding Event Handling in windows programming using C#. See other articles on the website on .NET and C#.

visual C-sharp.jpg The Complete Visual C# Programmer's Guide covers most of the major components that make up C# and the .net environment. The book is geared toward the intermediate programmer, but contains enough material to satisfy the advanced developer.

erver'>

Similar Articles