Home » C# Basics » 08 - Windows Programming - 1
8

Properties and Events

Defines the two aspects of controls: properties and events

As discussed in the previous section, a windows application consists of controls for user interaction. In order to develop windows application, we need to know about two aspects of the controls - properties and events.

Properties define the looks of the control. For example, a button control has a text property that is used to display a caption on the button. Some controls have BackColor property to set the background color.

Properties can be set at design time (when we are developing the application in the IDE) or at run time (when the application is running). To set the properties of a control at design time, we can select the control and then set the required properties in the properties window of the IDE:

As shown in the figure, to change a property, click inside the cell next to the required property and type the value or select from a drop-down list.

To change the properties at run time, we need to write code. For example, if we want to change the background color of a control when a button is clicked, we need to write code for the click event of the button.

This brings us to the topic of events. An event is an action that takes place when the user interacts with a control. As the developer, we have to write the code that will execute when the user interacts with the control. A control can have multiple events but it is not necessary to write code for each event. We should concentrate only on the events that are required for the application to work properly. For example, a button control has many events but only the click event is important because this event specifies the action that will take place when the user clicks the button.

To write the code for a control's event, we have two options. First, we can double click on the control. This will open up a new page in the IDE with some pre-written code and we can write our code here. However, this method is useful for the default event of that control. If we double click on the button control, for example, we will get the option to write code for its click event because the click event is the default event of the button control.

The second method is to use the properties window. At the top of the properties window, there is a tab to view the list of events for that control. Clicking on this tab will display all the available events. Now all we need to do is to double click inside the cell next to the required event.

Both of these methods will open up a new page (tab) in the IDE. The page consists of some pre-written code. This is where we write all the code of our application. The code for each event of a control is written in a separate section known as event handler. For example, the code for the click event of the button is written inside the click event handler of the button control.

Note that the code that we write in the event handlers uses the same programming elements that we have learned so far. Data types, variables, conditional statements, iteration statements, arrays and so on. The difference is that instead of writing a linear program, we are coding events of different controls i.e. we are specifying what happens when the user interacts with the controls. Also, note that if we do not write code for an event, that event will never execute.

When we are developing a windows application, we have two modes of working - design view and code view. In the design view, we can place the controls, set their properties and get a feeling of how the user interface will look like. In the code view, we write the code for the event handlers. Both these views can be opened side-by-side in the IDE, in different tabs.