No GUI application is complete without enabling actions. Even though arranging components is of significant issue, applying actions is also equally important. It is these actions that instruct the program what to do when something happens, like for example Mouse click, Keyboard press etc. Before beginning our discussion let us review how different API's handle events.
Microsoft Foundation Classes (MFC): These classes are based upon Microsoft Win32 API. Normally development work is done through Visual C++.
Java API: Here majority of the work is done by employing AWT and Swing packages. Actions are enabled by applying Interfaces. The main difficulty is that you should learn and remember all methods in the corresponding interfaces failing which you would get compile time errors.
As compared to the above, Event handling in C# is much more simplified. It is possible to handle various Mouse and Key related events quickly and in a more efficient manner. Learning will be easier, if you understood the basic principles behind handling events, which are elaborated below:
1. Invoke the related event by supplying a custom method or event handler.
using += operator as shown here:
b1.Click += new EventHandler(OnClick);
2. Apply the event handler as described below. It must be in conformity to a delegate of the class System.EventHandler:
public delegate void EventHandler(object sender, Event args)
The first argument indicates the object sending the event and the second argument contains information for the current event. You can use this argument object to handle functionalities associated with the related event. Listing-1 below illustrates how to print "Hello C#" inside a Textbox when a Button is clicked:
Listing-1
using System;
using System.Windows.Forms;
using System.Drawing;
public class Butevent:Form
{
TextBox t1 = new TextBox();
Button b1 = new Button();
public Butevent()
{
this.Text = "Program developed by Anand.N";
t1.Location = new Point(20,30);
b1.Text = "Click here to activate";
b1.Location = new Point(20,55);
b1.Size = new Size(150,20);
// Invoking Method or EventHandler
b1.Click+=new EventHandler(OnClick);
this.Controls.Add(t1);
this.Controls.Add(b1);
// Invoking Method or EventHandler
this.Resize += new EventHandler(OnResize);
}
//Applying EventHandler
public void OnResize(object sender,EventArgs ee)
{
MessageBox.Show("oops! Form Resized");
}
//Applying EventHandler
public void OnClick(object sender,EventArgs e)
{
t1.Text = "Hello C#";
}
public static void Main()
{
Application.Run(new Butevent());
}
}
The above example also shows how to handle Resize event. Try resizing the forms border and observe the result.
Handling MouseEvents
You can handle various Mouse actions by using the events specified in the Control class. Following listing shows how to handle a simple MouseUp Event:
Listing-2
using System;
using System.Windows.Forms;
using System.Drawing;
public class Mousedemo:Form
{
public Mousedemo()
{
this.MouseUp += new MouseEventHandler(OnMouseup);
}
public void OnMouseup(object sender,MouseEventArgs e)
{
this.Text = "Current Position (" +e.X + " , " + e.Y +")";
}
public static void Main()
{
Application.Run(new Mousedemo());
}
}
Try out the above example and observe the result. Similarly Click, DoubleClick, MouseEnter, MouseLeave events can be handled in the similar way.
Using KeyBoard Events
Every modern programming language contains all necessary functionalities for handling KeyBoard related events. C# also provides us with three events KeyPress, KeyUp and KeyDown which you can use to handle Keyboard events. Listing-3 below shows the usage of KeyUp Event. Copy the code given below, compile and observe the output.
Listing-3
using System;
using System.Windows.Forms;
using System.Drawing;
public class Keydemo:Form
{
public Keydemo()
{
this.KeyUp += new KeyEventHandler(OnKeyPress);
}
public void OnKeyPress(object sender, KeyEventArgs e)
{
MessageBox.Show(e.KeyCode.ToString(), "Your input");
}
public static void Main()
{
Application.Run(new Keydemo());
}
}
I hope by now you have got significant exposure to Event Handling techniques using C# language. You can explore more events by going through online help. Meanwhile please don't forget to drop me a mail at [email protected]. I will be glad to help after hearing from you