lecture (13)controls and event handling
TRANSCRIPT
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 116
1
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Lecture 14
Controls and Event handling
Mdm Ratnawati Ibrahim
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 216
FSB23103 2
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Introduction
A graphical user interface (GUI) allows a user to interact visually
with a program Windows forms are used to create the GUIs forprograms
bull GUIs are constructed from GUI components referred to as controls or widgets (short for window gadgets)
bull A GUI component is an object with which the user interacts
using the mouse or keyboardbull The ToolBox of the VBNet environment contains a number of
controls and components that may be added to a Windowsform by selecting the control and dragging on to the form
A control such as a button or label is a component that
has a graphical representation at runtime A component eg a timer does not have a graphical
representation at runtime
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 316
FSB23103 3
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Common controls
ComponentDescription
Label An area in which uneditable text is displayed
Text An area in which a user inputs data from the keyboard This area can also display information
Button An area that triggers an event when clicked
Checkbox A component is either selected or unselected
Combobox A drop-down list of items from which the user can makea selection either by clicking an item in a list or bytyping into a box
Listbox An area in which a list of items is displayed The user can
make a selection from the list by clicking on any item
Groupbox A container in which components may be placed
Scrollbar A component that allows access to a range of elementsthat normally cannot fit in the controlrsquos container
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 416
FSB23103 4
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Example GUI with controls
MainMenu
GroupBox
RadioButton
ComboBox
ListBox
Label ScrollBar
TextBox
Track Bar
Button
PictureBox
Run
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 516
FSB23103 5
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Windows Form
bull The general process to create a Windows applicationinvolves generating a Windows Form setting itsproperties adding controls setting their properties andimplementing the event handlers (methods that are calledin response to an event)
bull The Form acts as a container for components and controlsWhen a control is dragged from the Toolbox onto theform Visual BasicNet generates appropriate codeinstantiates the component and sets its basic properties The code generated is contained Form1Designervb within Form1vb
bull Although it is possible to write the code that results fromincorporating a control of the form it is usual to leave thisto the IDE with the programmer writing the necessary event handler
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 616
FSB23103 6
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Event Handler
bull In general the format of a event-handling method is
Event-handler methods take two standard arguments Anobject (usually sender) and an instance of the EventsArgsClass
bull The appropriate method header and footer are generatedon double clicking the control on the form using the Name
set in the controlrsquos propertiesbull An event handler may respond to a number of events or
alternative events by listing the required events afterlsquohandlesrsquo in the method header However the order inwhich alternative events are listed has no significance
Private Sub ControlName_EventName(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles ControlNameEventName
event-handling codeEnd Sub
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 716
FSB23103 7
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Control Properties
Some common properties that can be set for many
controlsProperty Description
BackColor Set the controlrsquos background colour
Enabled Indicates whether the user can interact with it
Focused Indicates whether the control has the focus
Font Sets the Font used to display the controlrsquos text
ForeColor Usually sets the Font colour
TabIndex Sets the tab order of the control
TabStop Indicates whether the Tab key may be used to selectthe control
Text Sets the text associated with the control
TextAlign Allows text to be aligned vertically and horizontally
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 816
FSB23103 8
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Control Methods
The Focus method transfers the focus to a control Acontrol that has the focus is referred to as the activecontrol When the Tab key is pressed controls are givenfocus in the order specified by their TabIndex property
Method Description
Focus Acquires the focus
Hide Hides the control (sets property Visible to False)
Show Shows the control (sets property Visible to True)
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 916
FSB23103 9
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Input Controls
Various controls allow data to be fed into a program The
choice will depend on the nature of the data to be input andthe likely user Often a balance is sought between usabilityand robustnessndash too flexible will allow invalid data entry andtoo restricted could lead to user frustration
Eg
bull Textbox ndash allows text to be input by the user via thekeyboard or displayed by the program
bull Listbox ndash allows the user to view and select from multipleitems in a list
bull Combobox ndash combines Textbox features with a drop-downlist from which a value can be selected It usually appearsas a textbox with a down arrow to its right By default theuser can enter text into the textbox or click the downarrow to display a list of predefined items
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1016FSB23103 10
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Input Controls ndash contrsquod
bull Textbox
By default input text aligned leftIf input is required to be numeric thenadvisable to check using function IsNumeric( ) beforecasting and using in calculations
bull Listbox The property SelectedIndex returns theindex of the selected item commencing at 0If no item has been selected -1 is returnedItems are added to a list box by addingobjects to its Items collection using themethod Add
Alternatively by using the String Collection Editor accessed by clicking on the Items property in theProperties Window
myListBoxItemsAdd(myListItem)
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1116FSB23103 11
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Input Controls ndash contrsquod
bull Combobox As with a listbox the programmer can addobjects to collection Items programmaticallyusing method Add or visually with the StringCollection Editor Comboboxes have propertiessimilar to the corresponding properties inListbox ndash in particular SelectedIndex When
the selected item changes eventSelectedIndexChanged is generated
bull Scrollbar This may be horizontal or vertical and used toenter a range of discrete numerical valuesusing the Value property The limits of the
range are fixed by Maximum and Minimum properties and incremental changes resultingfrom clicking the arrows specified by thesmallChange property
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1216FSB23103 12
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Input Controls ndash contrsquod
bull Trackbar
This has similar properties to scrollbar tospecify range and value Clearly if differs inappearance its orientation is set using theOrientation property and its operation ndash justa slider no end arrows
bull Checkboxes and RadioButtons These may be regarded as state buttons thatmay be in an onoff or TrueFalse state AradioButton is different from a checkBox sinceit is usually one of a group where only one
button is set to true at a time The position of the label is fixed using theRightToLeft and TextAlign properties andbuttonrsquos state is determined by examining theChecked property
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1316FSB23103 13
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Output Controls
bull Label
This allows text to be displayed on a form by assigning aString value to the Text property
bull PictureBox This tends to be used to display Graphics objects and otherimages To display graphics requires a graphics object to
be associated with the control using the CreateGraphicsmethod
Eg
Clearly previously mentioned input controls eg Textboxes listboxes etc can also serve to output data
Dim paper As Graphics = picBoxCreateGraphics
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1416FSB23103 14
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Other controls
Some controls may be seen as not used to input nor output
data but are concerned with program controlbull GroupBox This might be labelled and used to bind togetherseveral radio buttons When buttons are draggedinto the box code is generated to Add eachbutton to the groupBoxrsquos Controls property
bull ButtonCommand buttons are used to initiate actions
bull MainMenu This may be used to provide the means formulti-stage selection It may be introduced to a
form to appear as a menu bar below the title barAlternative options are introduced by type in theoffered cells Doubling clicking a terminal leaf cellallows desired event code to be entered
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1516FSB23103 15
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
Mouse-event handling
A mouse event is generated when the mouse interacts with acontrol
Excluding the first two events certain information becomesavailable Eg
bull The x and y coordinates of the mouse pointer relative to thecontrol Point(00) represents the top-left corner of the control
bull Which mouse button was pressed
bull How many times the mouse was clicked
Mouse Event Description
MouseEnter Enters area of control
MouseLeave Leaves area of control
MouseDown Button pressed while in control area
MouseHover Mouse cursor hovers over control area
MouseMove Mouse moved while in control area
MouseUp Button released while in control area
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1616FSB23103 16
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
Mouse-event properties
To locate a mouse click within a pictureBox
Mouse
Property
Description
Button Indicates which button was pressed
Clicks Number of times mouse was clicked
X The x ndash coordinate of the event within thecontrol
Y The y ndash coordinate of the event within thecontrol
Private Sub PictureBox1_Click(ByVal sender As SystemObject _
ByVal e As SystemWindowsFormsMouseEventArgs) _
Handles PictureBox1MouseDown
Label1Text = eX amp amp eYEnd Sub
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 216
FSB23103 2
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Introduction
A graphical user interface (GUI) allows a user to interact visually
with a program Windows forms are used to create the GUIs forprograms
bull GUIs are constructed from GUI components referred to as controls or widgets (short for window gadgets)
bull A GUI component is an object with which the user interacts
using the mouse or keyboardbull The ToolBox of the VBNet environment contains a number of
controls and components that may be added to a Windowsform by selecting the control and dragging on to the form
A control such as a button or label is a component that
has a graphical representation at runtime A component eg a timer does not have a graphical
representation at runtime
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 316
FSB23103 3
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Common controls
ComponentDescription
Label An area in which uneditable text is displayed
Text An area in which a user inputs data from the keyboard This area can also display information
Button An area that triggers an event when clicked
Checkbox A component is either selected or unselected
Combobox A drop-down list of items from which the user can makea selection either by clicking an item in a list or bytyping into a box
Listbox An area in which a list of items is displayed The user can
make a selection from the list by clicking on any item
Groupbox A container in which components may be placed
Scrollbar A component that allows access to a range of elementsthat normally cannot fit in the controlrsquos container
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 416
FSB23103 4
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Example GUI with controls
MainMenu
GroupBox
RadioButton
ComboBox
ListBox
Label ScrollBar
TextBox
Track Bar
Button
PictureBox
Run
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 516
FSB23103 5
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Windows Form
bull The general process to create a Windows applicationinvolves generating a Windows Form setting itsproperties adding controls setting their properties andimplementing the event handlers (methods that are calledin response to an event)
bull The Form acts as a container for components and controlsWhen a control is dragged from the Toolbox onto theform Visual BasicNet generates appropriate codeinstantiates the component and sets its basic properties The code generated is contained Form1Designervb within Form1vb
bull Although it is possible to write the code that results fromincorporating a control of the form it is usual to leave thisto the IDE with the programmer writing the necessary event handler
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 616
FSB23103 6
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Event Handler
bull In general the format of a event-handling method is
Event-handler methods take two standard arguments Anobject (usually sender) and an instance of the EventsArgsClass
bull The appropriate method header and footer are generatedon double clicking the control on the form using the Name
set in the controlrsquos propertiesbull An event handler may respond to a number of events or
alternative events by listing the required events afterlsquohandlesrsquo in the method header However the order inwhich alternative events are listed has no significance
Private Sub ControlName_EventName(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles ControlNameEventName
event-handling codeEnd Sub
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 716
FSB23103 7
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Control Properties
Some common properties that can be set for many
controlsProperty Description
BackColor Set the controlrsquos background colour
Enabled Indicates whether the user can interact with it
Focused Indicates whether the control has the focus
Font Sets the Font used to display the controlrsquos text
ForeColor Usually sets the Font colour
TabIndex Sets the tab order of the control
TabStop Indicates whether the Tab key may be used to selectthe control
Text Sets the text associated with the control
TextAlign Allows text to be aligned vertically and horizontally
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 816
FSB23103 8
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Control Methods
The Focus method transfers the focus to a control Acontrol that has the focus is referred to as the activecontrol When the Tab key is pressed controls are givenfocus in the order specified by their TabIndex property
Method Description
Focus Acquires the focus
Hide Hides the control (sets property Visible to False)
Show Shows the control (sets property Visible to True)
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 916
FSB23103 9
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Input Controls
Various controls allow data to be fed into a program The
choice will depend on the nature of the data to be input andthe likely user Often a balance is sought between usabilityand robustnessndash too flexible will allow invalid data entry andtoo restricted could lead to user frustration
Eg
bull Textbox ndash allows text to be input by the user via thekeyboard or displayed by the program
bull Listbox ndash allows the user to view and select from multipleitems in a list
bull Combobox ndash combines Textbox features with a drop-downlist from which a value can be selected It usually appearsas a textbox with a down arrow to its right By default theuser can enter text into the textbox or click the downarrow to display a list of predefined items
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1016FSB23103 10
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Input Controls ndash contrsquod
bull Textbox
By default input text aligned leftIf input is required to be numeric thenadvisable to check using function IsNumeric( ) beforecasting and using in calculations
bull Listbox The property SelectedIndex returns theindex of the selected item commencing at 0If no item has been selected -1 is returnedItems are added to a list box by addingobjects to its Items collection using themethod Add
Alternatively by using the String Collection Editor accessed by clicking on the Items property in theProperties Window
myListBoxItemsAdd(myListItem)
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1116FSB23103 11
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Input Controls ndash contrsquod
bull Combobox As with a listbox the programmer can addobjects to collection Items programmaticallyusing method Add or visually with the StringCollection Editor Comboboxes have propertiessimilar to the corresponding properties inListbox ndash in particular SelectedIndex When
the selected item changes eventSelectedIndexChanged is generated
bull Scrollbar This may be horizontal or vertical and used toenter a range of discrete numerical valuesusing the Value property The limits of the
range are fixed by Maximum and Minimum properties and incremental changes resultingfrom clicking the arrows specified by thesmallChange property
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1216FSB23103 12
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Input Controls ndash contrsquod
bull Trackbar
This has similar properties to scrollbar tospecify range and value Clearly if differs inappearance its orientation is set using theOrientation property and its operation ndash justa slider no end arrows
bull Checkboxes and RadioButtons These may be regarded as state buttons thatmay be in an onoff or TrueFalse state AradioButton is different from a checkBox sinceit is usually one of a group where only one
button is set to true at a time The position of the label is fixed using theRightToLeft and TextAlign properties andbuttonrsquos state is determined by examining theChecked property
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1316FSB23103 13
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Output Controls
bull Label
This allows text to be displayed on a form by assigning aString value to the Text property
bull PictureBox This tends to be used to display Graphics objects and otherimages To display graphics requires a graphics object to
be associated with the control using the CreateGraphicsmethod
Eg
Clearly previously mentioned input controls eg Textboxes listboxes etc can also serve to output data
Dim paper As Graphics = picBoxCreateGraphics
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1416FSB23103 14
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Other controls
Some controls may be seen as not used to input nor output
data but are concerned with program controlbull GroupBox This might be labelled and used to bind togetherseveral radio buttons When buttons are draggedinto the box code is generated to Add eachbutton to the groupBoxrsquos Controls property
bull ButtonCommand buttons are used to initiate actions
bull MainMenu This may be used to provide the means formulti-stage selection It may be introduced to a
form to appear as a menu bar below the title barAlternative options are introduced by type in theoffered cells Doubling clicking a terminal leaf cellallows desired event code to be entered
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1516FSB23103 15
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
Mouse-event handling
A mouse event is generated when the mouse interacts with acontrol
Excluding the first two events certain information becomesavailable Eg
bull The x and y coordinates of the mouse pointer relative to thecontrol Point(00) represents the top-left corner of the control
bull Which mouse button was pressed
bull How many times the mouse was clicked
Mouse Event Description
MouseEnter Enters area of control
MouseLeave Leaves area of control
MouseDown Button pressed while in control area
MouseHover Mouse cursor hovers over control area
MouseMove Mouse moved while in control area
MouseUp Button released while in control area
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1616FSB23103 16
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
Mouse-event properties
To locate a mouse click within a pictureBox
Mouse
Property
Description
Button Indicates which button was pressed
Clicks Number of times mouse was clicked
X The x ndash coordinate of the event within thecontrol
Y The y ndash coordinate of the event within thecontrol
Private Sub PictureBox1_Click(ByVal sender As SystemObject _
ByVal e As SystemWindowsFormsMouseEventArgs) _
Handles PictureBox1MouseDown
Label1Text = eX amp amp eYEnd Sub
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 316
FSB23103 3
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Common controls
ComponentDescription
Label An area in which uneditable text is displayed
Text An area in which a user inputs data from the keyboard This area can also display information
Button An area that triggers an event when clicked
Checkbox A component is either selected or unselected
Combobox A drop-down list of items from which the user can makea selection either by clicking an item in a list or bytyping into a box
Listbox An area in which a list of items is displayed The user can
make a selection from the list by clicking on any item
Groupbox A container in which components may be placed
Scrollbar A component that allows access to a range of elementsthat normally cannot fit in the controlrsquos container
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 416
FSB23103 4
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Example GUI with controls
MainMenu
GroupBox
RadioButton
ComboBox
ListBox
Label ScrollBar
TextBox
Track Bar
Button
PictureBox
Run
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 516
FSB23103 5
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Windows Form
bull The general process to create a Windows applicationinvolves generating a Windows Form setting itsproperties adding controls setting their properties andimplementing the event handlers (methods that are calledin response to an event)
bull The Form acts as a container for components and controlsWhen a control is dragged from the Toolbox onto theform Visual BasicNet generates appropriate codeinstantiates the component and sets its basic properties The code generated is contained Form1Designervb within Form1vb
bull Although it is possible to write the code that results fromincorporating a control of the form it is usual to leave thisto the IDE with the programmer writing the necessary event handler
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 616
FSB23103 6
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Event Handler
bull In general the format of a event-handling method is
Event-handler methods take two standard arguments Anobject (usually sender) and an instance of the EventsArgsClass
bull The appropriate method header and footer are generatedon double clicking the control on the form using the Name
set in the controlrsquos propertiesbull An event handler may respond to a number of events or
alternative events by listing the required events afterlsquohandlesrsquo in the method header However the order inwhich alternative events are listed has no significance
Private Sub ControlName_EventName(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles ControlNameEventName
event-handling codeEnd Sub
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 716
FSB23103 7
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Control Properties
Some common properties that can be set for many
controlsProperty Description
BackColor Set the controlrsquos background colour
Enabled Indicates whether the user can interact with it
Focused Indicates whether the control has the focus
Font Sets the Font used to display the controlrsquos text
ForeColor Usually sets the Font colour
TabIndex Sets the tab order of the control
TabStop Indicates whether the Tab key may be used to selectthe control
Text Sets the text associated with the control
TextAlign Allows text to be aligned vertically and horizontally
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 816
FSB23103 8
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Control Methods
The Focus method transfers the focus to a control Acontrol that has the focus is referred to as the activecontrol When the Tab key is pressed controls are givenfocus in the order specified by their TabIndex property
Method Description
Focus Acquires the focus
Hide Hides the control (sets property Visible to False)
Show Shows the control (sets property Visible to True)
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 916
FSB23103 9
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Input Controls
Various controls allow data to be fed into a program The
choice will depend on the nature of the data to be input andthe likely user Often a balance is sought between usabilityand robustnessndash too flexible will allow invalid data entry andtoo restricted could lead to user frustration
Eg
bull Textbox ndash allows text to be input by the user via thekeyboard or displayed by the program
bull Listbox ndash allows the user to view and select from multipleitems in a list
bull Combobox ndash combines Textbox features with a drop-downlist from which a value can be selected It usually appearsas a textbox with a down arrow to its right By default theuser can enter text into the textbox or click the downarrow to display a list of predefined items
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1016FSB23103 10
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Input Controls ndash contrsquod
bull Textbox
By default input text aligned leftIf input is required to be numeric thenadvisable to check using function IsNumeric( ) beforecasting and using in calculations
bull Listbox The property SelectedIndex returns theindex of the selected item commencing at 0If no item has been selected -1 is returnedItems are added to a list box by addingobjects to its Items collection using themethod Add
Alternatively by using the String Collection Editor accessed by clicking on the Items property in theProperties Window
myListBoxItemsAdd(myListItem)
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1116FSB23103 11
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Input Controls ndash contrsquod
bull Combobox As with a listbox the programmer can addobjects to collection Items programmaticallyusing method Add or visually with the StringCollection Editor Comboboxes have propertiessimilar to the corresponding properties inListbox ndash in particular SelectedIndex When
the selected item changes eventSelectedIndexChanged is generated
bull Scrollbar This may be horizontal or vertical and used toenter a range of discrete numerical valuesusing the Value property The limits of the
range are fixed by Maximum and Minimum properties and incremental changes resultingfrom clicking the arrows specified by thesmallChange property
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1216FSB23103 12
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Input Controls ndash contrsquod
bull Trackbar
This has similar properties to scrollbar tospecify range and value Clearly if differs inappearance its orientation is set using theOrientation property and its operation ndash justa slider no end arrows
bull Checkboxes and RadioButtons These may be regarded as state buttons thatmay be in an onoff or TrueFalse state AradioButton is different from a checkBox sinceit is usually one of a group where only one
button is set to true at a time The position of the label is fixed using theRightToLeft and TextAlign properties andbuttonrsquos state is determined by examining theChecked property
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1316FSB23103 13
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Output Controls
bull Label
This allows text to be displayed on a form by assigning aString value to the Text property
bull PictureBox This tends to be used to display Graphics objects and otherimages To display graphics requires a graphics object to
be associated with the control using the CreateGraphicsmethod
Eg
Clearly previously mentioned input controls eg Textboxes listboxes etc can also serve to output data
Dim paper As Graphics = picBoxCreateGraphics
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1416FSB23103 14
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Other controls
Some controls may be seen as not used to input nor output
data but are concerned with program controlbull GroupBox This might be labelled and used to bind togetherseveral radio buttons When buttons are draggedinto the box code is generated to Add eachbutton to the groupBoxrsquos Controls property
bull ButtonCommand buttons are used to initiate actions
bull MainMenu This may be used to provide the means formulti-stage selection It may be introduced to a
form to appear as a menu bar below the title barAlternative options are introduced by type in theoffered cells Doubling clicking a terminal leaf cellallows desired event code to be entered
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1516FSB23103 15
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
Mouse-event handling
A mouse event is generated when the mouse interacts with acontrol
Excluding the first two events certain information becomesavailable Eg
bull The x and y coordinates of the mouse pointer relative to thecontrol Point(00) represents the top-left corner of the control
bull Which mouse button was pressed
bull How many times the mouse was clicked
Mouse Event Description
MouseEnter Enters area of control
MouseLeave Leaves area of control
MouseDown Button pressed while in control area
MouseHover Mouse cursor hovers over control area
MouseMove Mouse moved while in control area
MouseUp Button released while in control area
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1616FSB23103 16
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
Mouse-event properties
To locate a mouse click within a pictureBox
Mouse
Property
Description
Button Indicates which button was pressed
Clicks Number of times mouse was clicked
X The x ndash coordinate of the event within thecontrol
Y The y ndash coordinate of the event within thecontrol
Private Sub PictureBox1_Click(ByVal sender As SystemObject _
ByVal e As SystemWindowsFormsMouseEventArgs) _
Handles PictureBox1MouseDown
Label1Text = eX amp amp eYEnd Sub
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 416
FSB23103 4
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Example GUI with controls
MainMenu
GroupBox
RadioButton
ComboBox
ListBox
Label ScrollBar
TextBox
Track Bar
Button
PictureBox
Run
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 516
FSB23103 5
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Windows Form
bull The general process to create a Windows applicationinvolves generating a Windows Form setting itsproperties adding controls setting their properties andimplementing the event handlers (methods that are calledin response to an event)
bull The Form acts as a container for components and controlsWhen a control is dragged from the Toolbox onto theform Visual BasicNet generates appropriate codeinstantiates the component and sets its basic properties The code generated is contained Form1Designervb within Form1vb
bull Although it is possible to write the code that results fromincorporating a control of the form it is usual to leave thisto the IDE with the programmer writing the necessary event handler
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 616
FSB23103 6
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Event Handler
bull In general the format of a event-handling method is
Event-handler methods take two standard arguments Anobject (usually sender) and an instance of the EventsArgsClass
bull The appropriate method header and footer are generatedon double clicking the control on the form using the Name
set in the controlrsquos propertiesbull An event handler may respond to a number of events or
alternative events by listing the required events afterlsquohandlesrsquo in the method header However the order inwhich alternative events are listed has no significance
Private Sub ControlName_EventName(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles ControlNameEventName
event-handling codeEnd Sub
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 716
FSB23103 7
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Control Properties
Some common properties that can be set for many
controlsProperty Description
BackColor Set the controlrsquos background colour
Enabled Indicates whether the user can interact with it
Focused Indicates whether the control has the focus
Font Sets the Font used to display the controlrsquos text
ForeColor Usually sets the Font colour
TabIndex Sets the tab order of the control
TabStop Indicates whether the Tab key may be used to selectthe control
Text Sets the text associated with the control
TextAlign Allows text to be aligned vertically and horizontally
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 816
FSB23103 8
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Control Methods
The Focus method transfers the focus to a control Acontrol that has the focus is referred to as the activecontrol When the Tab key is pressed controls are givenfocus in the order specified by their TabIndex property
Method Description
Focus Acquires the focus
Hide Hides the control (sets property Visible to False)
Show Shows the control (sets property Visible to True)
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 916
FSB23103 9
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Input Controls
Various controls allow data to be fed into a program The
choice will depend on the nature of the data to be input andthe likely user Often a balance is sought between usabilityand robustnessndash too flexible will allow invalid data entry andtoo restricted could lead to user frustration
Eg
bull Textbox ndash allows text to be input by the user via thekeyboard or displayed by the program
bull Listbox ndash allows the user to view and select from multipleitems in a list
bull Combobox ndash combines Textbox features with a drop-downlist from which a value can be selected It usually appearsas a textbox with a down arrow to its right By default theuser can enter text into the textbox or click the downarrow to display a list of predefined items
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1016FSB23103 10
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Input Controls ndash contrsquod
bull Textbox
By default input text aligned leftIf input is required to be numeric thenadvisable to check using function IsNumeric( ) beforecasting and using in calculations
bull Listbox The property SelectedIndex returns theindex of the selected item commencing at 0If no item has been selected -1 is returnedItems are added to a list box by addingobjects to its Items collection using themethod Add
Alternatively by using the String Collection Editor accessed by clicking on the Items property in theProperties Window
myListBoxItemsAdd(myListItem)
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1116FSB23103 11
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Input Controls ndash contrsquod
bull Combobox As with a listbox the programmer can addobjects to collection Items programmaticallyusing method Add or visually with the StringCollection Editor Comboboxes have propertiessimilar to the corresponding properties inListbox ndash in particular SelectedIndex When
the selected item changes eventSelectedIndexChanged is generated
bull Scrollbar This may be horizontal or vertical and used toenter a range of discrete numerical valuesusing the Value property The limits of the
range are fixed by Maximum and Minimum properties and incremental changes resultingfrom clicking the arrows specified by thesmallChange property
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1216FSB23103 12
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Input Controls ndash contrsquod
bull Trackbar
This has similar properties to scrollbar tospecify range and value Clearly if differs inappearance its orientation is set using theOrientation property and its operation ndash justa slider no end arrows
bull Checkboxes and RadioButtons These may be regarded as state buttons thatmay be in an onoff or TrueFalse state AradioButton is different from a checkBox sinceit is usually one of a group where only one
button is set to true at a time The position of the label is fixed using theRightToLeft and TextAlign properties andbuttonrsquos state is determined by examining theChecked property
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1316FSB23103 13
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Output Controls
bull Label
This allows text to be displayed on a form by assigning aString value to the Text property
bull PictureBox This tends to be used to display Graphics objects and otherimages To display graphics requires a graphics object to
be associated with the control using the CreateGraphicsmethod
Eg
Clearly previously mentioned input controls eg Textboxes listboxes etc can also serve to output data
Dim paper As Graphics = picBoxCreateGraphics
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1416FSB23103 14
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Other controls
Some controls may be seen as not used to input nor output
data but are concerned with program controlbull GroupBox This might be labelled and used to bind togetherseveral radio buttons When buttons are draggedinto the box code is generated to Add eachbutton to the groupBoxrsquos Controls property
bull ButtonCommand buttons are used to initiate actions
bull MainMenu This may be used to provide the means formulti-stage selection It may be introduced to a
form to appear as a menu bar below the title barAlternative options are introduced by type in theoffered cells Doubling clicking a terminal leaf cellallows desired event code to be entered
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1516FSB23103 15
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
Mouse-event handling
A mouse event is generated when the mouse interacts with acontrol
Excluding the first two events certain information becomesavailable Eg
bull The x and y coordinates of the mouse pointer relative to thecontrol Point(00) represents the top-left corner of the control
bull Which mouse button was pressed
bull How many times the mouse was clicked
Mouse Event Description
MouseEnter Enters area of control
MouseLeave Leaves area of control
MouseDown Button pressed while in control area
MouseHover Mouse cursor hovers over control area
MouseMove Mouse moved while in control area
MouseUp Button released while in control area
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1616FSB23103 16
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
Mouse-event properties
To locate a mouse click within a pictureBox
Mouse
Property
Description
Button Indicates which button was pressed
Clicks Number of times mouse was clicked
X The x ndash coordinate of the event within thecontrol
Y The y ndash coordinate of the event within thecontrol
Private Sub PictureBox1_Click(ByVal sender As SystemObject _
ByVal e As SystemWindowsFormsMouseEventArgs) _
Handles PictureBox1MouseDown
Label1Text = eX amp amp eYEnd Sub
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 516
FSB23103 5
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Windows Form
bull The general process to create a Windows applicationinvolves generating a Windows Form setting itsproperties adding controls setting their properties andimplementing the event handlers (methods that are calledin response to an event)
bull The Form acts as a container for components and controlsWhen a control is dragged from the Toolbox onto theform Visual BasicNet generates appropriate codeinstantiates the component and sets its basic properties The code generated is contained Form1Designervb within Form1vb
bull Although it is possible to write the code that results fromincorporating a control of the form it is usual to leave thisto the IDE with the programmer writing the necessary event handler
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 616
FSB23103 6
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Event Handler
bull In general the format of a event-handling method is
Event-handler methods take two standard arguments Anobject (usually sender) and an instance of the EventsArgsClass
bull The appropriate method header and footer are generatedon double clicking the control on the form using the Name
set in the controlrsquos propertiesbull An event handler may respond to a number of events or
alternative events by listing the required events afterlsquohandlesrsquo in the method header However the order inwhich alternative events are listed has no significance
Private Sub ControlName_EventName(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles ControlNameEventName
event-handling codeEnd Sub
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 716
FSB23103 7
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Control Properties
Some common properties that can be set for many
controlsProperty Description
BackColor Set the controlrsquos background colour
Enabled Indicates whether the user can interact with it
Focused Indicates whether the control has the focus
Font Sets the Font used to display the controlrsquos text
ForeColor Usually sets the Font colour
TabIndex Sets the tab order of the control
TabStop Indicates whether the Tab key may be used to selectthe control
Text Sets the text associated with the control
TextAlign Allows text to be aligned vertically and horizontally
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 816
FSB23103 8
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Control Methods
The Focus method transfers the focus to a control Acontrol that has the focus is referred to as the activecontrol When the Tab key is pressed controls are givenfocus in the order specified by their TabIndex property
Method Description
Focus Acquires the focus
Hide Hides the control (sets property Visible to False)
Show Shows the control (sets property Visible to True)
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 916
FSB23103 9
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Input Controls
Various controls allow data to be fed into a program The
choice will depend on the nature of the data to be input andthe likely user Often a balance is sought between usabilityand robustnessndash too flexible will allow invalid data entry andtoo restricted could lead to user frustration
Eg
bull Textbox ndash allows text to be input by the user via thekeyboard or displayed by the program
bull Listbox ndash allows the user to view and select from multipleitems in a list
bull Combobox ndash combines Textbox features with a drop-downlist from which a value can be selected It usually appearsas a textbox with a down arrow to its right By default theuser can enter text into the textbox or click the downarrow to display a list of predefined items
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1016FSB23103 10
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Input Controls ndash contrsquod
bull Textbox
By default input text aligned leftIf input is required to be numeric thenadvisable to check using function IsNumeric( ) beforecasting and using in calculations
bull Listbox The property SelectedIndex returns theindex of the selected item commencing at 0If no item has been selected -1 is returnedItems are added to a list box by addingobjects to its Items collection using themethod Add
Alternatively by using the String Collection Editor accessed by clicking on the Items property in theProperties Window
myListBoxItemsAdd(myListItem)
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1116FSB23103 11
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Input Controls ndash contrsquod
bull Combobox As with a listbox the programmer can addobjects to collection Items programmaticallyusing method Add or visually with the StringCollection Editor Comboboxes have propertiessimilar to the corresponding properties inListbox ndash in particular SelectedIndex When
the selected item changes eventSelectedIndexChanged is generated
bull Scrollbar This may be horizontal or vertical and used toenter a range of discrete numerical valuesusing the Value property The limits of the
range are fixed by Maximum and Minimum properties and incremental changes resultingfrom clicking the arrows specified by thesmallChange property
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1216FSB23103 12
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Input Controls ndash contrsquod
bull Trackbar
This has similar properties to scrollbar tospecify range and value Clearly if differs inappearance its orientation is set using theOrientation property and its operation ndash justa slider no end arrows
bull Checkboxes and RadioButtons These may be regarded as state buttons thatmay be in an onoff or TrueFalse state AradioButton is different from a checkBox sinceit is usually one of a group where only one
button is set to true at a time The position of the label is fixed using theRightToLeft and TextAlign properties andbuttonrsquos state is determined by examining theChecked property
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1316FSB23103 13
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Output Controls
bull Label
This allows text to be displayed on a form by assigning aString value to the Text property
bull PictureBox This tends to be used to display Graphics objects and otherimages To display graphics requires a graphics object to
be associated with the control using the CreateGraphicsmethod
Eg
Clearly previously mentioned input controls eg Textboxes listboxes etc can also serve to output data
Dim paper As Graphics = picBoxCreateGraphics
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1416FSB23103 14
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Other controls
Some controls may be seen as not used to input nor output
data but are concerned with program controlbull GroupBox This might be labelled and used to bind togetherseveral radio buttons When buttons are draggedinto the box code is generated to Add eachbutton to the groupBoxrsquos Controls property
bull ButtonCommand buttons are used to initiate actions
bull MainMenu This may be used to provide the means formulti-stage selection It may be introduced to a
form to appear as a menu bar below the title barAlternative options are introduced by type in theoffered cells Doubling clicking a terminal leaf cellallows desired event code to be entered
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1516FSB23103 15
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
Mouse-event handling
A mouse event is generated when the mouse interacts with acontrol
Excluding the first two events certain information becomesavailable Eg
bull The x and y coordinates of the mouse pointer relative to thecontrol Point(00) represents the top-left corner of the control
bull Which mouse button was pressed
bull How many times the mouse was clicked
Mouse Event Description
MouseEnter Enters area of control
MouseLeave Leaves area of control
MouseDown Button pressed while in control area
MouseHover Mouse cursor hovers over control area
MouseMove Mouse moved while in control area
MouseUp Button released while in control area
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1616FSB23103 16
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
Mouse-event properties
To locate a mouse click within a pictureBox
Mouse
Property
Description
Button Indicates which button was pressed
Clicks Number of times mouse was clicked
X The x ndash coordinate of the event within thecontrol
Y The y ndash coordinate of the event within thecontrol
Private Sub PictureBox1_Click(ByVal sender As SystemObject _
ByVal e As SystemWindowsFormsMouseEventArgs) _
Handles PictureBox1MouseDown
Label1Text = eX amp amp eYEnd Sub
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 616
FSB23103 6
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Event Handler
bull In general the format of a event-handling method is
Event-handler methods take two standard arguments Anobject (usually sender) and an instance of the EventsArgsClass
bull The appropriate method header and footer are generatedon double clicking the control on the form using the Name
set in the controlrsquos propertiesbull An event handler may respond to a number of events or
alternative events by listing the required events afterlsquohandlesrsquo in the method header However the order inwhich alternative events are listed has no significance
Private Sub ControlName_EventName(ByVal sender As SystemObject _
ByVal e As SystemEventArgs) Handles ControlNameEventName
event-handling codeEnd Sub
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 716
FSB23103 7
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Control Properties
Some common properties that can be set for many
controlsProperty Description
BackColor Set the controlrsquos background colour
Enabled Indicates whether the user can interact with it
Focused Indicates whether the control has the focus
Font Sets the Font used to display the controlrsquos text
ForeColor Usually sets the Font colour
TabIndex Sets the tab order of the control
TabStop Indicates whether the Tab key may be used to selectthe control
Text Sets the text associated with the control
TextAlign Allows text to be aligned vertically and horizontally
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 816
FSB23103 8
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Control Methods
The Focus method transfers the focus to a control Acontrol that has the focus is referred to as the activecontrol When the Tab key is pressed controls are givenfocus in the order specified by their TabIndex property
Method Description
Focus Acquires the focus
Hide Hides the control (sets property Visible to False)
Show Shows the control (sets property Visible to True)
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 916
FSB23103 9
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Input Controls
Various controls allow data to be fed into a program The
choice will depend on the nature of the data to be input andthe likely user Often a balance is sought between usabilityand robustnessndash too flexible will allow invalid data entry andtoo restricted could lead to user frustration
Eg
bull Textbox ndash allows text to be input by the user via thekeyboard or displayed by the program
bull Listbox ndash allows the user to view and select from multipleitems in a list
bull Combobox ndash combines Textbox features with a drop-downlist from which a value can be selected It usually appearsas a textbox with a down arrow to its right By default theuser can enter text into the textbox or click the downarrow to display a list of predefined items
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1016FSB23103 10
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Input Controls ndash contrsquod
bull Textbox
By default input text aligned leftIf input is required to be numeric thenadvisable to check using function IsNumeric( ) beforecasting and using in calculations
bull Listbox The property SelectedIndex returns theindex of the selected item commencing at 0If no item has been selected -1 is returnedItems are added to a list box by addingobjects to its Items collection using themethod Add
Alternatively by using the String Collection Editor accessed by clicking on the Items property in theProperties Window
myListBoxItemsAdd(myListItem)
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1116FSB23103 11
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Input Controls ndash contrsquod
bull Combobox As with a listbox the programmer can addobjects to collection Items programmaticallyusing method Add or visually with the StringCollection Editor Comboboxes have propertiessimilar to the corresponding properties inListbox ndash in particular SelectedIndex When
the selected item changes eventSelectedIndexChanged is generated
bull Scrollbar This may be horizontal or vertical and used toenter a range of discrete numerical valuesusing the Value property The limits of the
range are fixed by Maximum and Minimum properties and incremental changes resultingfrom clicking the arrows specified by thesmallChange property
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1216FSB23103 12
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Input Controls ndash contrsquod
bull Trackbar
This has similar properties to scrollbar tospecify range and value Clearly if differs inappearance its orientation is set using theOrientation property and its operation ndash justa slider no end arrows
bull Checkboxes and RadioButtons These may be regarded as state buttons thatmay be in an onoff or TrueFalse state AradioButton is different from a checkBox sinceit is usually one of a group where only one
button is set to true at a time The position of the label is fixed using theRightToLeft and TextAlign properties andbuttonrsquos state is determined by examining theChecked property
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1316FSB23103 13
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Output Controls
bull Label
This allows text to be displayed on a form by assigning aString value to the Text property
bull PictureBox This tends to be used to display Graphics objects and otherimages To display graphics requires a graphics object to
be associated with the control using the CreateGraphicsmethod
Eg
Clearly previously mentioned input controls eg Textboxes listboxes etc can also serve to output data
Dim paper As Graphics = picBoxCreateGraphics
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1416FSB23103 14
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Other controls
Some controls may be seen as not used to input nor output
data but are concerned with program controlbull GroupBox This might be labelled and used to bind togetherseveral radio buttons When buttons are draggedinto the box code is generated to Add eachbutton to the groupBoxrsquos Controls property
bull ButtonCommand buttons are used to initiate actions
bull MainMenu This may be used to provide the means formulti-stage selection It may be introduced to a
form to appear as a menu bar below the title barAlternative options are introduced by type in theoffered cells Doubling clicking a terminal leaf cellallows desired event code to be entered
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1516FSB23103 15
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
Mouse-event handling
A mouse event is generated when the mouse interacts with acontrol
Excluding the first two events certain information becomesavailable Eg
bull The x and y coordinates of the mouse pointer relative to thecontrol Point(00) represents the top-left corner of the control
bull Which mouse button was pressed
bull How many times the mouse was clicked
Mouse Event Description
MouseEnter Enters area of control
MouseLeave Leaves area of control
MouseDown Button pressed while in control area
MouseHover Mouse cursor hovers over control area
MouseMove Mouse moved while in control area
MouseUp Button released while in control area
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1616FSB23103 16
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
Mouse-event properties
To locate a mouse click within a pictureBox
Mouse
Property
Description
Button Indicates which button was pressed
Clicks Number of times mouse was clicked
X The x ndash coordinate of the event within thecontrol
Y The y ndash coordinate of the event within thecontrol
Private Sub PictureBox1_Click(ByVal sender As SystemObject _
ByVal e As SystemWindowsFormsMouseEventArgs) _
Handles PictureBox1MouseDown
Label1Text = eX amp amp eYEnd Sub
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 716
FSB23103 7
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Control Properties
Some common properties that can be set for many
controlsProperty Description
BackColor Set the controlrsquos background colour
Enabled Indicates whether the user can interact with it
Focused Indicates whether the control has the focus
Font Sets the Font used to display the controlrsquos text
ForeColor Usually sets the Font colour
TabIndex Sets the tab order of the control
TabStop Indicates whether the Tab key may be used to selectthe control
Text Sets the text associated with the control
TextAlign Allows text to be aligned vertically and horizontally
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 816
FSB23103 8
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Control Methods
The Focus method transfers the focus to a control Acontrol that has the focus is referred to as the activecontrol When the Tab key is pressed controls are givenfocus in the order specified by their TabIndex property
Method Description
Focus Acquires the focus
Hide Hides the control (sets property Visible to False)
Show Shows the control (sets property Visible to True)
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 916
FSB23103 9
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Input Controls
Various controls allow data to be fed into a program The
choice will depend on the nature of the data to be input andthe likely user Often a balance is sought between usabilityand robustnessndash too flexible will allow invalid data entry andtoo restricted could lead to user frustration
Eg
bull Textbox ndash allows text to be input by the user via thekeyboard or displayed by the program
bull Listbox ndash allows the user to view and select from multipleitems in a list
bull Combobox ndash combines Textbox features with a drop-downlist from which a value can be selected It usually appearsas a textbox with a down arrow to its right By default theuser can enter text into the textbox or click the downarrow to display a list of predefined items
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1016FSB23103 10
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Input Controls ndash contrsquod
bull Textbox
By default input text aligned leftIf input is required to be numeric thenadvisable to check using function IsNumeric( ) beforecasting and using in calculations
bull Listbox The property SelectedIndex returns theindex of the selected item commencing at 0If no item has been selected -1 is returnedItems are added to a list box by addingobjects to its Items collection using themethod Add
Alternatively by using the String Collection Editor accessed by clicking on the Items property in theProperties Window
myListBoxItemsAdd(myListItem)
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1116FSB23103 11
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Input Controls ndash contrsquod
bull Combobox As with a listbox the programmer can addobjects to collection Items programmaticallyusing method Add or visually with the StringCollection Editor Comboboxes have propertiessimilar to the corresponding properties inListbox ndash in particular SelectedIndex When
the selected item changes eventSelectedIndexChanged is generated
bull Scrollbar This may be horizontal or vertical and used toenter a range of discrete numerical valuesusing the Value property The limits of the
range are fixed by Maximum and Minimum properties and incremental changes resultingfrom clicking the arrows specified by thesmallChange property
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1216FSB23103 12
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Input Controls ndash contrsquod
bull Trackbar
This has similar properties to scrollbar tospecify range and value Clearly if differs inappearance its orientation is set using theOrientation property and its operation ndash justa slider no end arrows
bull Checkboxes and RadioButtons These may be regarded as state buttons thatmay be in an onoff or TrueFalse state AradioButton is different from a checkBox sinceit is usually one of a group where only one
button is set to true at a time The position of the label is fixed using theRightToLeft and TextAlign properties andbuttonrsquos state is determined by examining theChecked property
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1316FSB23103 13
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Output Controls
bull Label
This allows text to be displayed on a form by assigning aString value to the Text property
bull PictureBox This tends to be used to display Graphics objects and otherimages To display graphics requires a graphics object to
be associated with the control using the CreateGraphicsmethod
Eg
Clearly previously mentioned input controls eg Textboxes listboxes etc can also serve to output data
Dim paper As Graphics = picBoxCreateGraphics
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1416FSB23103 14
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Other controls
Some controls may be seen as not used to input nor output
data but are concerned with program controlbull GroupBox This might be labelled and used to bind togetherseveral radio buttons When buttons are draggedinto the box code is generated to Add eachbutton to the groupBoxrsquos Controls property
bull ButtonCommand buttons are used to initiate actions
bull MainMenu This may be used to provide the means formulti-stage selection It may be introduced to a
form to appear as a menu bar below the title barAlternative options are introduced by type in theoffered cells Doubling clicking a terminal leaf cellallows desired event code to be entered
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1516FSB23103 15
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
Mouse-event handling
A mouse event is generated when the mouse interacts with acontrol
Excluding the first two events certain information becomesavailable Eg
bull The x and y coordinates of the mouse pointer relative to thecontrol Point(00) represents the top-left corner of the control
bull Which mouse button was pressed
bull How many times the mouse was clicked
Mouse Event Description
MouseEnter Enters area of control
MouseLeave Leaves area of control
MouseDown Button pressed while in control area
MouseHover Mouse cursor hovers over control area
MouseMove Mouse moved while in control area
MouseUp Button released while in control area
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1616FSB23103 16
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
Mouse-event properties
To locate a mouse click within a pictureBox
Mouse
Property
Description
Button Indicates which button was pressed
Clicks Number of times mouse was clicked
X The x ndash coordinate of the event within thecontrol
Y The y ndash coordinate of the event within thecontrol
Private Sub PictureBox1_Click(ByVal sender As SystemObject _
ByVal e As SystemWindowsFormsMouseEventArgs) _
Handles PictureBox1MouseDown
Label1Text = eX amp amp eYEnd Sub
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 816
FSB23103 8
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Control Methods
The Focus method transfers the focus to a control Acontrol that has the focus is referred to as the activecontrol When the Tab key is pressed controls are givenfocus in the order specified by their TabIndex property
Method Description
Focus Acquires the focus
Hide Hides the control (sets property Visible to False)
Show Shows the control (sets property Visible to True)
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 916
FSB23103 9
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Input Controls
Various controls allow data to be fed into a program The
choice will depend on the nature of the data to be input andthe likely user Often a balance is sought between usabilityand robustnessndash too flexible will allow invalid data entry andtoo restricted could lead to user frustration
Eg
bull Textbox ndash allows text to be input by the user via thekeyboard or displayed by the program
bull Listbox ndash allows the user to view and select from multipleitems in a list
bull Combobox ndash combines Textbox features with a drop-downlist from which a value can be selected It usually appearsas a textbox with a down arrow to its right By default theuser can enter text into the textbox or click the downarrow to display a list of predefined items
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1016FSB23103 10
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Input Controls ndash contrsquod
bull Textbox
By default input text aligned leftIf input is required to be numeric thenadvisable to check using function IsNumeric( ) beforecasting and using in calculations
bull Listbox The property SelectedIndex returns theindex of the selected item commencing at 0If no item has been selected -1 is returnedItems are added to a list box by addingobjects to its Items collection using themethod Add
Alternatively by using the String Collection Editor accessed by clicking on the Items property in theProperties Window
myListBoxItemsAdd(myListItem)
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1116FSB23103 11
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Input Controls ndash contrsquod
bull Combobox As with a listbox the programmer can addobjects to collection Items programmaticallyusing method Add or visually with the StringCollection Editor Comboboxes have propertiessimilar to the corresponding properties inListbox ndash in particular SelectedIndex When
the selected item changes eventSelectedIndexChanged is generated
bull Scrollbar This may be horizontal or vertical and used toenter a range of discrete numerical valuesusing the Value property The limits of the
range are fixed by Maximum and Minimum properties and incremental changes resultingfrom clicking the arrows specified by thesmallChange property
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1216FSB23103 12
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Input Controls ndash contrsquod
bull Trackbar
This has similar properties to scrollbar tospecify range and value Clearly if differs inappearance its orientation is set using theOrientation property and its operation ndash justa slider no end arrows
bull Checkboxes and RadioButtons These may be regarded as state buttons thatmay be in an onoff or TrueFalse state AradioButton is different from a checkBox sinceit is usually one of a group where only one
button is set to true at a time The position of the label is fixed using theRightToLeft and TextAlign properties andbuttonrsquos state is determined by examining theChecked property
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1316FSB23103 13
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Output Controls
bull Label
This allows text to be displayed on a form by assigning aString value to the Text property
bull PictureBox This tends to be used to display Graphics objects and otherimages To display graphics requires a graphics object to
be associated with the control using the CreateGraphicsmethod
Eg
Clearly previously mentioned input controls eg Textboxes listboxes etc can also serve to output data
Dim paper As Graphics = picBoxCreateGraphics
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1416FSB23103 14
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Other controls
Some controls may be seen as not used to input nor output
data but are concerned with program controlbull GroupBox This might be labelled and used to bind togetherseveral radio buttons When buttons are draggedinto the box code is generated to Add eachbutton to the groupBoxrsquos Controls property
bull ButtonCommand buttons are used to initiate actions
bull MainMenu This may be used to provide the means formulti-stage selection It may be introduced to a
form to appear as a menu bar below the title barAlternative options are introduced by type in theoffered cells Doubling clicking a terminal leaf cellallows desired event code to be entered
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1516FSB23103 15
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
Mouse-event handling
A mouse event is generated when the mouse interacts with acontrol
Excluding the first two events certain information becomesavailable Eg
bull The x and y coordinates of the mouse pointer relative to thecontrol Point(00) represents the top-left corner of the control
bull Which mouse button was pressed
bull How many times the mouse was clicked
Mouse Event Description
MouseEnter Enters area of control
MouseLeave Leaves area of control
MouseDown Button pressed while in control area
MouseHover Mouse cursor hovers over control area
MouseMove Mouse moved while in control area
MouseUp Button released while in control area
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1616FSB23103 16
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
Mouse-event properties
To locate a mouse click within a pictureBox
Mouse
Property
Description
Button Indicates which button was pressed
Clicks Number of times mouse was clicked
X The x ndash coordinate of the event within thecontrol
Y The y ndash coordinate of the event within thecontrol
Private Sub PictureBox1_Click(ByVal sender As SystemObject _
ByVal e As SystemWindowsFormsMouseEventArgs) _
Handles PictureBox1MouseDown
Label1Text = eX amp amp eYEnd Sub
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 916
FSB23103 9
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceI
nstit
ute
Input Controls
Various controls allow data to be fed into a program The
choice will depend on the nature of the data to be input andthe likely user Often a balance is sought between usabilityand robustnessndash too flexible will allow invalid data entry andtoo restricted could lead to user frustration
Eg
bull Textbox ndash allows text to be input by the user via thekeyboard or displayed by the program
bull Listbox ndash allows the user to view and select from multipleitems in a list
bull Combobox ndash combines Textbox features with a drop-downlist from which a value can be selected It usually appearsas a textbox with a down arrow to its right By default theuser can enter text into the textbox or click the downarrow to display a list of predefined items
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1016FSB23103 10
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Input Controls ndash contrsquod
bull Textbox
By default input text aligned leftIf input is required to be numeric thenadvisable to check using function IsNumeric( ) beforecasting and using in calculations
bull Listbox The property SelectedIndex returns theindex of the selected item commencing at 0If no item has been selected -1 is returnedItems are added to a list box by addingobjects to its Items collection using themethod Add
Alternatively by using the String Collection Editor accessed by clicking on the Items property in theProperties Window
myListBoxItemsAdd(myListItem)
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1116FSB23103 11
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Input Controls ndash contrsquod
bull Combobox As with a listbox the programmer can addobjects to collection Items programmaticallyusing method Add or visually with the StringCollection Editor Comboboxes have propertiessimilar to the corresponding properties inListbox ndash in particular SelectedIndex When
the selected item changes eventSelectedIndexChanged is generated
bull Scrollbar This may be horizontal or vertical and used toenter a range of discrete numerical valuesusing the Value property The limits of the
range are fixed by Maximum and Minimum properties and incremental changes resultingfrom clicking the arrows specified by thesmallChange property
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1216FSB23103 12
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Input Controls ndash contrsquod
bull Trackbar
This has similar properties to scrollbar tospecify range and value Clearly if differs inappearance its orientation is set using theOrientation property and its operation ndash justa slider no end arrows
bull Checkboxes and RadioButtons These may be regarded as state buttons thatmay be in an onoff or TrueFalse state AradioButton is different from a checkBox sinceit is usually one of a group where only one
button is set to true at a time The position of the label is fixed using theRightToLeft and TextAlign properties andbuttonrsquos state is determined by examining theChecked property
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1316FSB23103 13
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Output Controls
bull Label
This allows text to be displayed on a form by assigning aString value to the Text property
bull PictureBox This tends to be used to display Graphics objects and otherimages To display graphics requires a graphics object to
be associated with the control using the CreateGraphicsmethod
Eg
Clearly previously mentioned input controls eg Textboxes listboxes etc can also serve to output data
Dim paper As Graphics = picBoxCreateGraphics
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1416FSB23103 14
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Other controls
Some controls may be seen as not used to input nor output
data but are concerned with program controlbull GroupBox This might be labelled and used to bind togetherseveral radio buttons When buttons are draggedinto the box code is generated to Add eachbutton to the groupBoxrsquos Controls property
bull ButtonCommand buttons are used to initiate actions
bull MainMenu This may be used to provide the means formulti-stage selection It may be introduced to a
form to appear as a menu bar below the title barAlternative options are introduced by type in theoffered cells Doubling clicking a terminal leaf cellallows desired event code to be entered
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1516FSB23103 15
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
Mouse-event handling
A mouse event is generated when the mouse interacts with acontrol
Excluding the first two events certain information becomesavailable Eg
bull The x and y coordinates of the mouse pointer relative to thecontrol Point(00) represents the top-left corner of the control
bull Which mouse button was pressed
bull How many times the mouse was clicked
Mouse Event Description
MouseEnter Enters area of control
MouseLeave Leaves area of control
MouseDown Button pressed while in control area
MouseHover Mouse cursor hovers over control area
MouseMove Mouse moved while in control area
MouseUp Button released while in control area
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1616FSB23103 16
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
Mouse-event properties
To locate a mouse click within a pictureBox
Mouse
Property
Description
Button Indicates which button was pressed
Clicks Number of times mouse was clicked
X The x ndash coordinate of the event within thecontrol
Y The y ndash coordinate of the event within thecontrol
Private Sub PictureBox1_Click(ByVal sender As SystemObject _
ByVal e As SystemWindowsFormsMouseEventArgs) _
Handles PictureBox1MouseDown
Label1Text = eX amp amp eYEnd Sub
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1016FSB23103 10
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Input Controls ndash contrsquod
bull Textbox
By default input text aligned leftIf input is required to be numeric thenadvisable to check using function IsNumeric( ) beforecasting and using in calculations
bull Listbox The property SelectedIndex returns theindex of the selected item commencing at 0If no item has been selected -1 is returnedItems are added to a list box by addingobjects to its Items collection using themethod Add
Alternatively by using the String Collection Editor accessed by clicking on the Items property in theProperties Window
myListBoxItemsAdd(myListItem)
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1116FSB23103 11
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Input Controls ndash contrsquod
bull Combobox As with a listbox the programmer can addobjects to collection Items programmaticallyusing method Add or visually with the StringCollection Editor Comboboxes have propertiessimilar to the corresponding properties inListbox ndash in particular SelectedIndex When
the selected item changes eventSelectedIndexChanged is generated
bull Scrollbar This may be horizontal or vertical and used toenter a range of discrete numerical valuesusing the Value property The limits of the
range are fixed by Maximum and Minimum properties and incremental changes resultingfrom clicking the arrows specified by thesmallChange property
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1216FSB23103 12
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Input Controls ndash contrsquod
bull Trackbar
This has similar properties to scrollbar tospecify range and value Clearly if differs inappearance its orientation is set using theOrientation property and its operation ndash justa slider no end arrows
bull Checkboxes and RadioButtons These may be regarded as state buttons thatmay be in an onoff or TrueFalse state AradioButton is different from a checkBox sinceit is usually one of a group where only one
button is set to true at a time The position of the label is fixed using theRightToLeft and TextAlign properties andbuttonrsquos state is determined by examining theChecked property
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1316FSB23103 13
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Output Controls
bull Label
This allows text to be displayed on a form by assigning aString value to the Text property
bull PictureBox This tends to be used to display Graphics objects and otherimages To display graphics requires a graphics object to
be associated with the control using the CreateGraphicsmethod
Eg
Clearly previously mentioned input controls eg Textboxes listboxes etc can also serve to output data
Dim paper As Graphics = picBoxCreateGraphics
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1416FSB23103 14
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Other controls
Some controls may be seen as not used to input nor output
data but are concerned with program controlbull GroupBox This might be labelled and used to bind togetherseveral radio buttons When buttons are draggedinto the box code is generated to Add eachbutton to the groupBoxrsquos Controls property
bull ButtonCommand buttons are used to initiate actions
bull MainMenu This may be used to provide the means formulti-stage selection It may be introduced to a
form to appear as a menu bar below the title barAlternative options are introduced by type in theoffered cells Doubling clicking a terminal leaf cellallows desired event code to be entered
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1516FSB23103 15
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
Mouse-event handling
A mouse event is generated when the mouse interacts with acontrol
Excluding the first two events certain information becomesavailable Eg
bull The x and y coordinates of the mouse pointer relative to thecontrol Point(00) represents the top-left corner of the control
bull Which mouse button was pressed
bull How many times the mouse was clicked
Mouse Event Description
MouseEnter Enters area of control
MouseLeave Leaves area of control
MouseDown Button pressed while in control area
MouseHover Mouse cursor hovers over control area
MouseMove Mouse moved while in control area
MouseUp Button released while in control area
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1616FSB23103 16
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
Mouse-event properties
To locate a mouse click within a pictureBox
Mouse
Property
Description
Button Indicates which button was pressed
Clicks Number of times mouse was clicked
X The x ndash coordinate of the event within thecontrol
Y The y ndash coordinate of the event within thecontrol
Private Sub PictureBox1_Click(ByVal sender As SystemObject _
ByVal e As SystemWindowsFormsMouseEventArgs) _
Handles PictureBox1MouseDown
Label1Text = eX amp amp eYEnd Sub
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1116FSB23103 11
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Input Controls ndash contrsquod
bull Combobox As with a listbox the programmer can addobjects to collection Items programmaticallyusing method Add or visually with the StringCollection Editor Comboboxes have propertiessimilar to the corresponding properties inListbox ndash in particular SelectedIndex When
the selected item changes eventSelectedIndexChanged is generated
bull Scrollbar This may be horizontal or vertical and used toenter a range of discrete numerical valuesusing the Value property The limits of the
range are fixed by Maximum and Minimum properties and incremental changes resultingfrom clicking the arrows specified by thesmallChange property
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1216FSB23103 12
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Input Controls ndash contrsquod
bull Trackbar
This has similar properties to scrollbar tospecify range and value Clearly if differs inappearance its orientation is set using theOrientation property and its operation ndash justa slider no end arrows
bull Checkboxes and RadioButtons These may be regarded as state buttons thatmay be in an onoff or TrueFalse state AradioButton is different from a checkBox sinceit is usually one of a group where only one
button is set to true at a time The position of the label is fixed using theRightToLeft and TextAlign properties andbuttonrsquos state is determined by examining theChecked property
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1316FSB23103 13
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Output Controls
bull Label
This allows text to be displayed on a form by assigning aString value to the Text property
bull PictureBox This tends to be used to display Graphics objects and otherimages To display graphics requires a graphics object to
be associated with the control using the CreateGraphicsmethod
Eg
Clearly previously mentioned input controls eg Textboxes listboxes etc can also serve to output data
Dim paper As Graphics = picBoxCreateGraphics
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1416FSB23103 14
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Other controls
Some controls may be seen as not used to input nor output
data but are concerned with program controlbull GroupBox This might be labelled and used to bind togetherseveral radio buttons When buttons are draggedinto the box code is generated to Add eachbutton to the groupBoxrsquos Controls property
bull ButtonCommand buttons are used to initiate actions
bull MainMenu This may be used to provide the means formulti-stage selection It may be introduced to a
form to appear as a menu bar below the title barAlternative options are introduced by type in theoffered cells Doubling clicking a terminal leaf cellallows desired event code to be entered
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1516FSB23103 15
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
Mouse-event handling
A mouse event is generated when the mouse interacts with acontrol
Excluding the first two events certain information becomesavailable Eg
bull The x and y coordinates of the mouse pointer relative to thecontrol Point(00) represents the top-left corner of the control
bull Which mouse button was pressed
bull How many times the mouse was clicked
Mouse Event Description
MouseEnter Enters area of control
MouseLeave Leaves area of control
MouseDown Button pressed while in control area
MouseHover Mouse cursor hovers over control area
MouseMove Mouse moved while in control area
MouseUp Button released while in control area
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1616FSB23103 16
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
Mouse-event properties
To locate a mouse click within a pictureBox
Mouse
Property
Description
Button Indicates which button was pressed
Clicks Number of times mouse was clicked
X The x ndash coordinate of the event within thecontrol
Y The y ndash coordinate of the event within thecontrol
Private Sub PictureBox1_Click(ByVal sender As SystemObject _
ByVal e As SystemWindowsFormsMouseEventArgs) _
Handles PictureBox1MouseDown
Label1Text = eX amp amp eYEnd Sub
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1216FSB23103 12
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Input Controls ndash contrsquod
bull Trackbar
This has similar properties to scrollbar tospecify range and value Clearly if differs inappearance its orientation is set using theOrientation property and its operation ndash justa slider no end arrows
bull Checkboxes and RadioButtons These may be regarded as state buttons thatmay be in an onoff or TrueFalse state AradioButton is different from a checkBox sinceit is usually one of a group where only one
button is set to true at a time The position of the label is fixed using theRightToLeft and TextAlign properties andbuttonrsquos state is determined by examining theChecked property
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1316FSB23103 13
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Output Controls
bull Label
This allows text to be displayed on a form by assigning aString value to the Text property
bull PictureBox This tends to be used to display Graphics objects and otherimages To display graphics requires a graphics object to
be associated with the control using the CreateGraphicsmethod
Eg
Clearly previously mentioned input controls eg Textboxes listboxes etc can also serve to output data
Dim paper As Graphics = picBoxCreateGraphics
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1416FSB23103 14
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Other controls
Some controls may be seen as not used to input nor output
data but are concerned with program controlbull GroupBox This might be labelled and used to bind togetherseveral radio buttons When buttons are draggedinto the box code is generated to Add eachbutton to the groupBoxrsquos Controls property
bull ButtonCommand buttons are used to initiate actions
bull MainMenu This may be used to provide the means formulti-stage selection It may be introduced to a
form to appear as a menu bar below the title barAlternative options are introduced by type in theoffered cells Doubling clicking a terminal leaf cellallows desired event code to be entered
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1516FSB23103 15
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
Mouse-event handling
A mouse event is generated when the mouse interacts with acontrol
Excluding the first two events certain information becomesavailable Eg
bull The x and y coordinates of the mouse pointer relative to thecontrol Point(00) represents the top-left corner of the control
bull Which mouse button was pressed
bull How many times the mouse was clicked
Mouse Event Description
MouseEnter Enters area of control
MouseLeave Leaves area of control
MouseDown Button pressed while in control area
MouseHover Mouse cursor hovers over control area
MouseMove Mouse moved while in control area
MouseUp Button released while in control area
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1616FSB23103 16
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
Mouse-event properties
To locate a mouse click within a pictureBox
Mouse
Property
Description
Button Indicates which button was pressed
Clicks Number of times mouse was clicked
X The x ndash coordinate of the event within thecontrol
Y The y ndash coordinate of the event within thecontrol
Private Sub PictureBox1_Click(ByVal sender As SystemObject _
ByVal e As SystemWindowsFormsMouseEventArgs) _
Handles PictureBox1MouseDown
Label1Text = eX amp amp eYEnd Sub
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1316FSB23103 13
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Output Controls
bull Label
This allows text to be displayed on a form by assigning aString value to the Text property
bull PictureBox This tends to be used to display Graphics objects and otherimages To display graphics requires a graphics object to
be associated with the control using the CreateGraphicsmethod
Eg
Clearly previously mentioned input controls eg Textboxes listboxes etc can also serve to output data
Dim paper As Graphics = picBoxCreateGraphics
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1416FSB23103 14
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Other controls
Some controls may be seen as not used to input nor output
data but are concerned with program controlbull GroupBox This might be labelled and used to bind togetherseveral radio buttons When buttons are draggedinto the box code is generated to Add eachbutton to the groupBoxrsquos Controls property
bull ButtonCommand buttons are used to initiate actions
bull MainMenu This may be used to provide the means formulti-stage selection It may be introduced to a
form to appear as a menu bar below the title barAlternative options are introduced by type in theoffered cells Doubling clicking a terminal leaf cellallows desired event code to be entered
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1516FSB23103 15
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
Mouse-event handling
A mouse event is generated when the mouse interacts with acontrol
Excluding the first two events certain information becomesavailable Eg
bull The x and y coordinates of the mouse pointer relative to thecontrol Point(00) represents the top-left corner of the control
bull Which mouse button was pressed
bull How many times the mouse was clicked
Mouse Event Description
MouseEnter Enters area of control
MouseLeave Leaves area of control
MouseDown Button pressed while in control area
MouseHover Mouse cursor hovers over control area
MouseMove Mouse moved while in control area
MouseUp Button released while in control area
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1616FSB23103 16
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
Mouse-event properties
To locate a mouse click within a pictureBox
Mouse
Property
Description
Button Indicates which button was pressed
Clicks Number of times mouse was clicked
X The x ndash coordinate of the event within thecontrol
Y The y ndash coordinate of the event within thecontrol
Private Sub PictureBox1_Click(ByVal sender As SystemObject _
ByVal e As SystemWindowsFormsMouseEventArgs) _
Handles PictureBox1MouseDown
Label1Text = eX amp amp eYEnd Sub
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1416FSB23103 14
U
NIVERS
ITIKUAL
A
LUMPUR
M
alaysi a
FranceInstit
ute
Other controls
Some controls may be seen as not used to input nor output
data but are concerned with program controlbull GroupBox This might be labelled and used to bind togetherseveral radio buttons When buttons are draggedinto the box code is generated to Add eachbutton to the groupBoxrsquos Controls property
bull ButtonCommand buttons are used to initiate actions
bull MainMenu This may be used to provide the means formulti-stage selection It may be introduced to a
form to appear as a menu bar below the title barAlternative options are introduced by type in theoffered cells Doubling clicking a terminal leaf cellallows desired event code to be entered
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1516FSB23103 15
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
Mouse-event handling
A mouse event is generated when the mouse interacts with acontrol
Excluding the first two events certain information becomesavailable Eg
bull The x and y coordinates of the mouse pointer relative to thecontrol Point(00) represents the top-left corner of the control
bull Which mouse button was pressed
bull How many times the mouse was clicked
Mouse Event Description
MouseEnter Enters area of control
MouseLeave Leaves area of control
MouseDown Button pressed while in control area
MouseHover Mouse cursor hovers over control area
MouseMove Mouse moved while in control area
MouseUp Button released while in control area
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1616FSB23103 16
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
Mouse-event properties
To locate a mouse click within a pictureBox
Mouse
Property
Description
Button Indicates which button was pressed
Clicks Number of times mouse was clicked
X The x ndash coordinate of the event within thecontrol
Y The y ndash coordinate of the event within thecontrol
Private Sub PictureBox1_Click(ByVal sender As SystemObject _
ByVal e As SystemWindowsFormsMouseEventArgs) _
Handles PictureBox1MouseDown
Label1Text = eX amp amp eYEnd Sub
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1516FSB23103 15
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
Mouse-event handling
A mouse event is generated when the mouse interacts with acontrol
Excluding the first two events certain information becomesavailable Eg
bull The x and y coordinates of the mouse pointer relative to thecontrol Point(00) represents the top-left corner of the control
bull Which mouse button was pressed
bull How many times the mouse was clicked
Mouse Event Description
MouseEnter Enters area of control
MouseLeave Leaves area of control
MouseDown Button pressed while in control area
MouseHover Mouse cursor hovers over control area
MouseMove Mouse moved while in control area
MouseUp Button released while in control area
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1616FSB23103 16
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
Mouse-event properties
To locate a mouse click within a pictureBox
Mouse
Property
Description
Button Indicates which button was pressed
Clicks Number of times mouse was clicked
X The x ndash coordinate of the event within thecontrol
Y The y ndash coordinate of the event within thecontrol
Private Sub PictureBox1_Click(ByVal sender As SystemObject _
ByVal e As SystemWindowsFormsMouseEventArgs) _
Handles PictureBox1MouseDown
Label1Text = eX amp amp eYEnd Sub
8142019 Lecture (13)Controls and Event Handling
httpslidepdfcomreaderfulllecture-13controls-and-event-handling 1616FSB23103 16
U
NIVERS
ITIKUAL A
LUMPUR
M
alaysi a
FranceInstit
ute
Mouse-event properties
To locate a mouse click within a pictureBox
Mouse
Property
Description
Button Indicates which button was pressed
Clicks Number of times mouse was clicked
X The x ndash coordinate of the event within thecontrol
Y The y ndash coordinate of the event within thecontrol
Private Sub PictureBox1_Click(ByVal sender As SystemObject _
ByVal e As SystemWindowsFormsMouseEventArgs) _
Handles PictureBox1MouseDown
Label1Text = eX amp amp eYEnd Sub