0% found this document useful (0 votes)
134 views

Event Driven Programming: Week 6

The document discusses event driven programming and graphical user interfaces (GUIs). Some key points: - Event driven programs execute based on events, not a predefined sequence of instructions. GUIs are a common example of event driven programming. - A GUI uses visual components like buttons and text boxes that the user interacts with via events like clicks or text entry. - In a GUI, events trigger event handler functions that contain the code executed in response to that event. - GUI programs are nonlinear, interactive, and have unpredictable execution based on user input events.

Uploaded by

Master Planer
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
134 views

Event Driven Programming: Week 6

The document discusses event driven programming and graphical user interfaces (GUIs). Some key points: - Event driven programs execute based on events, not a predefined sequence of instructions. GUIs are a common example of event driven programming. - A GUI uses visual components like buttons and text boxes that the user interacts with via events like clicks or text entry. - In a GUI, events trigger event handler functions that contain the code executed in response to that event. - GUI programs are nonlinear, interactive, and have unpredictable execution based on user input events.

Uploaded by

Master Planer
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Event Driven Programming

Week 6

Instructor: Jawad Shaukat


Outline

 Command Line Program


 Event Driven Program
 Graphical User Interface
 GUI Event Handling
 Events and Event Handler
 GUI Components
 Windows Form
 Event handler Example
 Properties of Controls
 Events Tab.
Typical Command Line Program

 Linear Structure
 Sequential Execution
 Non-Interactive
Interactive Command Line Program

 User Input Commands


 Non-linear Structure
 Unpredictable Execution
 Interactive
Event Driven Program

 Event-driven programs do not have a predefined sequence of instructions


to execute.
 They also do not have a predetermined finish.
 The most common example of event-driven programming is graphical user
interfaces (GUIs).
Graphical User Interface
Graphical User Interface

 Allows visual interaction with program


 Gives program distinct look and feel
 Built from GUI components/controls
 Is an object, accessed via keyboard or mouse
GUI Program Structure

 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

 A form is a visual surface on which you display information to the user.


 A control is a user interface (UI) element that displays data or accepts data
input.
 When a user does something to the form or one of its controls, the action
generates an event.
 A GUI control, such as a button or label, is a component with a graphical
part
 Some components, which we call containers, can contain other components
e.g. Form, Panel, GroupBox
 Components can generate events to which event handler can respond
GUI Components and Controls

 Components and Controls are organized into an inheritance class hierarchy


so that they can easily share characteristics
 Each component/control defines some
 Properties
 Methods
 Events
GUI Components and Controls
 Properties
 Like data members
 get, set
 e.g. myButton.Text = “Press Me”
 Methods
 Like member functions
 Tell component to do something
 e.g. myButton.Hide( )
 Events
 Like callback functions
 Receive notifications from component
 e.g. myButton.Click(Event e)
GUI Design
GUI Design
GUI Design
Visual Studio Design View
The Click Event for C# Form Buttons

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

 The other argument in between the round brackets was this:


 EventArgs e
 EventArgs is a class. It's short for event arguments, and tells you which events
was raised. The letter "e" sets up a variable to use this class. If you change
your line of code to this:
 MessageBox.Show( e.ToString() );
The Properties Window

 A Property is a piece of information that characterizes or describes a control.


 It could be related to its location or size, it could be its color or identification
or any other visual aspect that gives it meaning.

 Access Property Windows:


 On main menu, click view->properties windows.
 On the form, right click anything form or control and click properties.

 When control is selected, the properties window displays only its


characteristics.
Properties Categories

 Each field in property has two values: Property name and


property value.

 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.

 Events for Forms:


 Click,
 Double Click,
 Scroll,
 MouseEnter,
 MouseDown and many more
Coding in Event Handler of Button Click

 On the click button, Color of button is changed by the following code.

 private void button1_Click(object sender, EventArgs e)


 {
 button1.BackColor = Color.Black;
 }

You might also like