UNIT 1 -
PROGRAMMING
LECTURE 10 – EVENT-DRIVEN PROGRAMMING PART 1
TOPICS
GUI & Visual programming
Form and controls
o Label
o Textbox
o Button
Event-driven programming
2
GRAPHICAL USER INTERFACE (GUI)
• Console application: Program interacts with user by commands
• Graphical user interface:
• Menus
• Text with colors, size, font
• Image, picture
• Button
• Select button
• Scrollbar
• Etc.
3
CREATING GUI APPLICATION
Select
File New
Project
Windows
Application
template
Name
4
CREATING GUI APPLICATION
Design
View
Properties
Window
Form
Toolbox 5
CREATING GUI APPLICATION
Properties
Auto-hide
Solution
Explorer
Dynamic
Help
6
FORMS
Main program windows, includes controls
Form class has a lot of properties and methods
o Properties can be changed in Design View
o Or in Code View
Form class has many events that can be programmed as programmer wants
o Close form
o Mouse moves
o Mouse clicks (left or right)
o etc
7
FORM PROPERTIES Events
Alphabetical
Categorized
Property value
Properties
8
FORM PROPERTIES
9
FORM METHODS
Several Form’s methods:
– Activate: make Form object to get focus
– Close: close Form object
– Hide: hide Form object
– Show: show Form object (user can see it now)
FORM EVENTS
Many events can happen on a Form object: key board pressed, mouse clicked, mouse
moved, etc.
When an event happens, system will call a specific method for that event to response
to it
Programmer need to write that specific method
Event-driven programming:
o Event is the key. Logic of program is represented by events
o Programmer select event that is useful for program to implement (not all)
11
FORM EVENTS
Click to
view the
list of
Form’s
events
12
EVENT HANDLER
Nhấn đúp vào sự kiện MouseHover sẽ tạo ra hàm xử lý sự kiện này
trong Code
private void Form1_MouseHover(object sender, EventArgs e)
{
this.BackColor = Color.Honeydew;
}
Viết lệnh xử lý
Nhấn đúp vào sự kiện MouseLeave
private void Form1_MouseLeave(object sender, EventArgs e)
{
this.BackColor = Color.HotPink;
} 13
CONTROLS
Controls are:
o Button, Label, TextBox, ComboBox, MainMenu, ListBox, CheckBox,
RadioButton, and MonthCalendar, etc.
Each control has different properties / methods / events. Some are
common among controls (Text, Size, etc.)
Controls are inherited from System.Windows.Forms.Control
14
SUB CLASSES OF CONTROL object
object
• They are:
object
• System.Windows.Forms.Label
• System.Windows.Forms.TextBox object
• System.Windows.Forms.Button
object
• V.v. object
• Form’s controls are object of these classes
INHERITANCE TREE OF CONTROLS
16
CONTROLS
EXAMPLES
17
ADD CONTROLS ON FORM (VISUAL
PROGRAMMING)
From Toolbox, select and drag necessary control to Form
Control position can be moved, size can be changed
Controls alignment
o Aligned
o Same size
o Horizontal align, vertical align
o Etc.
18
COMMON PROPERTIES OF CONTROLS
19
COMMON METHOD CONTROLS
20
CONTROLS
EXAMPLE
21
LABELS
• Showing texts
• Naming other controls
• Showing texts that user cannot change
• Important properties
– Text: content to show
– Font: font of text
– BackColor, ForeColor: background color and text color
COMMAND BUTTONS
• A button that user can click to do
something
• Important properties private void cmdAdd_Click(...)
– Text: Content on button {
int i, j, k;
– Font: Font of text i =
System.Convert.ToInt32( this.txtNum1.Text );
– Enabled: false => cannot click j =
• Event: System.Convert.ToInt32( this.txtNum2.Text );
k = i + j;
– Click: Happen when button is MessageBox.Show( "Sum = " +
clicked (by mouse or keyboard) k.ToString() );
}
TEXT BOXES Important properties:
– Text: content to show / edit
• A control that
– Font: font of text
• Show and allow user to
– ReadOnly: content can be viewed only
change text content
• Normally used for data
input Multiline textbox:
– MultiLine: True
– Lines: collection of lines in box
– ScrollBars: none, horizontal, vertical,
or both
TEXT BOX EVENTS
Some important event
– Enter, Leave: happens when focus is changed
– KeyPress: happens when keyboard is pressed (when control has
focus)
– KeyDown, KeyUp: happens when keyboard is down / up
– TextChanged: happens when content is changed
DEMO
Demo some programs to illustrate Form, Label, Textbox, Command and
MessageBox
o Method
o Properties
o Events
26