GST Billing Software

header image

Event Handling in C#

Erachana Line

No GUI application is complete without enabling actions. Even though arranging components is a significant issue, applying actions is also equally important. These actions are what instructs the program to act when something happens. For example, Mouse clicks, Keyboard presses, etc.

Event handling in C# is much more simplified. It's possible to handle various Mouse and Key related events quickly and in a more efficient manner.

The basic principles behind handling events, are elaborated below

1. Invoke the related event by supplying a custom method or event handler using the += operator as shown here:b1.Click += new EventHandler(OnClick);

2. Apply the event handler as described below. It must be in conformance with 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 functions associated with the related event. Listing - 1 below illustrates how to print "Hello C#" inside a Textbox when a Button is clicked:

Example - 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 a 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. The following listing shows how to handle a simple MouseUp Event:

Example - 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. Click, DoubleClick, MouseEnter, and MouseLeave events can be handled in a similar way.

Using KeyBoard Events

Every modern programming language contains all necessary functions 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 the KeyUp Event. Copy the code given below, compile, and observe the output.

Example - 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());

}

}