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: