Event Driven Programming: Week 6
Event Driven Programming: Week 6
Week 6
Linear Structure
Sequential Execution
Non-Interactive
Interactive Command Line Program
Non-linear Structure
Interactive
Unpredictable execution
Event Callback Functions
Events and Event Handlers
In programming terms, an Event is when something special happens. Inbuilt
code gets activated when the event happens. The event is then said to be
"Handled". The Events that will be discussed in this section are called GUI
Events (GUI stand for Graphic User Interface).
They are things like:
clicking a mouse button
leaving a text box
right clicking, and many more.
An Event is a notification sent by an object to signal the occurrence of an
action.
An Event Handler is a method that contains the code that will be executed
when a particular event occurs for a specific object.
GUI Event Handling
Windows Application Flow
Some Basic GUI Components
Windows Forms
A form is a representation of a GUI window.
A form contains components and controls such as buttons, labels or textboxes
etc.
Form, Control and Event
The click event gets activated when a button is clicked on. Examine the default
code for a button:
private void button1_Click(object sender, EventArgs e)
{
}
In between the round brackets, we have this:
object sender, EventArgs e
The object keyword refers to the object which activated the event, a button
in this case. This is being placed in a variable called sender.
Start a new project. Add a button to your new form and double click it. Place
the following code between the curly brackets:
MessageBox.Show( sender.ToString() );
The Click Event for C# Form Buttons
Categories:
1. Empty Fields:
By default, these fields have nothing in their value section.
Most of these properties are dependent on other settings of
your program. For example, you can set a menu property for
a form only after you have created a menu.
To set the property on such a field, you can type in it or
select from a list.
Property Categories
2. Text Fields:
There are fields that expect you to type a value. Most of these fields have a default
value. Here is an example:
To change the value of the property, click the name of the property, type the
desired value, and press Enter.
While some properties, such as the Text, would allow anything, some other fields
expect a specific type of text, such as a numeric value.
Property Categories
3. Numeric Fields:
3. Some fields expect a numeric value. In this case, you
can click the name of the field and type the desired
value. If you type an invalid value, you would receive
a message box notifying you of the error. When this
happens, click OK and type a valid value. If the value
is supposed to be an integer, make sure you don't
type it as a decimal number.
4. Date-Based Fields:
3. Some fields expect you to enter a date. You must
type a valid date recognized by the operating system
and the Regional and Language Settings in Control
Panel. If you enter an invalid date, you would receive
an error.
Property Categories
5. Expandable Fields:
Some fields have a right-point button. This indicates that the property has a
set of sub-properties that actually belong to the same property and are defined
together. To expand such a field:
Click its right-point button
Double-click the property name
After expanding the field, it would be equipped with a down-right pointing arrow
Property Categories
6. Boolean Fields
Some fields can have only a True or False value. To change their setting, you can
either select from the combo box or double-click the property to toggle to the other
value.
.
Property Categories
7. Action Fields
Some fields would require a value or item that needs to be set through an
intermediary action. Such fields display an ellipsis button When you click the
button, a dialog box would come up and you can set the value for the field.
Property Categories
8. List-Based Fields
To change the value of some of the fields, you would use their combo box to
display the available values. After clicking the arrow, a list would display:
There are various types of list-based fields. Some of them display just two items.
To change their value, you can just double-click the field. Some other fields have
more than two values in the field. To change them, you can click their arrow and
select from the list. You can also double-click a few times until the desired value is
selected.
Property Categories
9. Area-Selection Fields
Some properties provide a window from where you can select the desired option.
The field primarily displays the arrow of a combo box. To use the field, you click
the arrow of the combo box and the window appears. Here are examples:
Example to change Property by Code
Control_id.PropertyName=Property_Value.
this.ForeColor = Color.AntiqueWhite;
button1.ForeColor = Color.Beige;
button1.Visible = false;
Events in Visual C# Form Based
Application
Events of different controllers are available on the same panel of Properties.
There is an option of events which provides the list of available events for
control or container selected.