Changes

Jump to: navigation, search

Understanding C Sharp GUI Events

466 bytes removed, 20:00, 24 January 2008
no edit summary
<table border="0" cellspacing="0" width="100%">
<tr>
<td width="20%">[[Designing Forms in Visual Studio|Previous]]<td align="center">[[Visual Basic Essentials|Table of Contents]]<td width="20%" align="right">[[Hiding and Showing Forms in Visual Basic|Next]]</td>
<tr>
<td width="20%">Designing Forms in Visual Studio<td align="center"><td width="20%" align="right">Hiding and Showing Forms in Visual Basic</td>
</table>
<hr>
 
In the days before graphical environments such as Microsoft windows, applications were developed using a procedural approach. This meant that the path a user would take through an application was dictated by the programmer at the point the application was developed. For example. the application might display a menu listing several options, and prompt the user to select an option. Depending on the user's selection, the application might display a data input screen in which data would need to be entered in the order in which the fields were displayed on the screen. Once the data was entered the user would then be able to press a key to save the data and return to the menu screen again.
The world of the graphical user interface is very different from the old days. Today a user is presented with windows containing multiple controls such as buttons, text fields, toggles and sliders. The mouse allows the user to interact with any of the visible controls in any order they choose. The programmer can, therefore, no longer dictate the path a user will take through an application. A new way of handling the interaction between a user and an application was needed. This approach is known as ''event handling''.
Instead of a procedural approach to programming, applications are now ''event driven''. In essence, the developer defines what the user interface is to look like, and then defines subroutines which will be called when particular events are triggered. For example, the Visual Basic C# programmer might create a Form containing a number of Buttons. For each Button, the programmer will define the Visual Basic C# code to be executed when a ''Click'' event is triggered on each button. When the application runs, an ''event loop'' sits in the background and waits for an event to be triggered in the application. Once an event is triggered (such as button click or a a keystroke in a text field) the event loop looks at the object from which the event was triggered to see if an event handler has been defined by the developer for that particular event type. If a handler has been defined, that subroutine is executed and the event loop continues to loop waiting for the next event to be triggered.
== Event Triggers ==
* Operating System - The Windows operating system can trigger events which can be handled by the application. For example, Windows will trigger an event when part of a window is obscured by another window. In this situation C# receives an event when the previously obscured window area is re-exposed so that it knows to repaint the area.
* Programmatically triggered events - The event of any object may be triggered by the programmer from within the Visual Basic C# code. For example, a program may need to simulate the event triggered by a user clicking on a button.
== Wiring Up Events in Visual Studio ==
In our example we want every keystroke performed by the user in the TextBox to be displayed by the Label control. In this case the ''TextChanged'' event is exactly the event we want.
With the ''TextChanged'' event still selected it is time to write the C# code that will be executed when the event is triggered. To do so, we will need to set the ''Text'' property of the ''myLabel'' control to equal the ''Text'' property of the ''textInput'' control. The properties of objects are accessed using what is called dot notation. For example the ''Text'' property of ''myLabel'' is accessed in Visual basic C# code as follows:
<tt>myLabel.Text</tt>

Navigation menu