VB Lect 04 - VB GUI
VB Lect 04 - VB GUI
Slide 2
Start a New VB Project
Slide 3
Initial Visual Basic Screen
Slide 4
Toolbox
Slide 5
VB Control
• Toolbox lists all the available
tools/objects can be used to design
GUIs.
• Each tool/object has its own unique
properties but most of them share the
same properties like Name, Font,
Text, Backcolor, etc.
• A tool is selected by double clicking
it or drag it onto the form.
• Different version of VB.NET may
have different set of tools.
Slide 6
Visual Basic Controls
Slide 7
Visual Basic Controls
1. A Window contains a form and a few controls:
a. Form - A window that contains these controls
b. Label - displays text the user cannot change
c. TextBox - allows the user to enter text
d. Button – performs an action when clicked
e. RadioButton - A round button that is selected or deselected with a mouse click
f. CheckBox – A box that is checked or unchecked with a mouse click
2. Forms and controls are objects which produces/responds to events.
3. Forms/Controls have
properties
a. Each property has a value
(or values)
b. Properties mainly deal with
appearance. Also for setting
behaviour, accessibility,
data, design and focus.
Slide 8
Name property
1. “Name” property establishes a means for the program to refer to that control.
Usually, change the given name to something more meaningful
2. The Label controls use the default names (Label1, etc.)
3. Text boxes, buttons, and the Gross Pay label play an active role in the program
and have been changed
Form1
label1 txtHoursWorked
label2 txtPayRate
label3 lblGrossPay
btnCalcGrossPay btnClose
Slide 9
Naming Conventions
1. Control names must start with a letter
2. Remaining characters may be letters, digits, or underscore
3. 1st 3 lowercase letters indicate the type of control
a. txt… for Text Boxes
b. lbl… for Labels
c. btn… for Buttons
4. After that, capitalize the first letter of each word
txthoursworked vs txtHoursWorked
Slide 10
Naming Conventions
1. The following is a list of recommended prefixes of control names.
Slide 11
VB Controls: Text Box
1. Purpose:
a. To handle text data and display text.
b. To capture input from user
2. Common properties:
a. Name
i. The name given to the text box and is
referred in the code
b. Text
i. Text that is displayed or entered by user
ii. Not to be referred in the code
c. Font
i. Set the font type, size and style of the text box
d. BackColor, ForeColor
i. Set the background and foreground color
of the text box
e. TextAlign
i. Controls alignment of text in the text box
Slide 12
VB Events
Slide 13
VB Events: Form Load
Slide 14
VB Events: Click
Slide 15
Developing a VB GUI Application in MS VS
1
1. Create the GUI
2. Set the properties
3. Write the code 2
Button 1
Name: btnClear
Text: Clear
BackColor: Cyan
3
Slide 16
Understanding VB GUI Design Code
Class name
Public Class Form1
Body/
Hander
Procedure event
code
/ Handler
Slide 17
Understanding VB GUI Design Code
End Sub
Refers to method
current
class,
Form1
Slide 18
ACTIVITY 1
Slide 19
Using InputDialog and MessageBox for Accepting Inputs
and Displaying
Slide 20
Input and Events
Slide 21
GUI – Dialogue Box and Message Box
Dialogue box:
Message box:
Messagebox.Show("Start")
MsgBox("File saved", 0, "Save Confirmation")
Slide 22
Constant value for button
Slide 23
BMI Calculator
End Sub
Slide 24
Other Common VB Controls
Slide 25
VB Controls: Masked Text Box
1. Purpose:
a. To simplify data entry process.
b. To improve keying efficiency.
2. Enforcing the use of input patterns
by setting the Mask property:
a. (###)-###-####, ##-##-####, etc
c. For example, you can select a mask for
a. ZIP code, a date, a phone number, or
b. bank account number.
d. At run time the user cannot enter characters that
do not conform to the mask
Slide 26
VB Controls: Group Box
1. Purpose:
a. Improves readability of form by separating the controls into logical groups
b. To group related data together in a box/container
c. To make GUIs looks neat, logical and uncluttered
d. All controls in a Group Box is treated as one unit
grpContact
grpMaritalStatus
grpGender
Slide 27
VB Controls: Check Box
1. Purpose:
a. Allows the user to select or deselect one or more items in any group
2. Use the value of its Checked property to determine whether the check box
is selected or not:
a. Selected = Checked property has a True value
b. Unselected = Checked property has a False value
3. Use the Text property for the text you want to appear next
to the box
Slide 28
VB Controls: Radio Button
1. Purpose:
a. Allows the user to select or deselect only one item in a
group
2. If one Radio Button is clicked on, the others will be set off
automatically
3. It shares the same properties as Check Box like Checked,
Text, Locked, Appearance, etc.
Slide 29
VB Controls: Radio Button vs Check Box
Slide 30
VB Controls: Picture Box
1. Purpose:
a. To display an image of any extension
2. Common properties:
a. SizeMode
i. To set type of sizing either Normal, StrechImage, AutoSize,
CenterImage
b. Visible
i. To make the image appear or disappear using Boolean value:
PictureBox.Visible = True ‘Show the image
PictureBox.Visible = False ‘Hide the image
1. How to load an image into a picture box?
a. Design time: Browse and upload an image from the Image property.
b. Run time:
PictureBox.Image = Image.FromFile(“C:\pic.jpg”)
Slide 31
VB Controls: List Box
1. Purpose:
a. Allow multiple selection of choices via a box with a list of elements
b. With scroll bar to support longer list as alternative to radio buttons and check boxes
2. Common properties:
a. SelectionMode
i. To set type of selection either None, One, MultiSimple or MultiExtended
b. Items
i. To set elements of list box via the String Collection Editor window
Slide 32
VB Controls: Combo Box
1. Purpose:
a. Allow multiple selection of choices via a box with a list of elements
b. Combined the features of a Text Box and a List Box
2. User can enter data in the text box or/and select data from the list.
3. Common properties:
a. DropDownStyle
i. To set type of selection either Simple, DropDown or DropDownList
b. Items
i. To set elements of list box via the String Collection Editor window
Slide 33
Slide 34
Borders and Styles
Slide 35
Selecting Multiple Controls
Slide 36
Keyboard Access Key
Text=&OK
Text=E&xit
Slide 37
Setting the Tab Order for Controls
Slide 38
Setting the Tab Order for Controls
Slide 39
Setting the Form’s Location on Screen
Slide 40
Creating Tool Tips
Slide 41
Creating Tool Tips
Step 1:
Drag the ToolTip control
to the Form
Step 2: Step 3:
The ToolTip control will be Set the text via ToolTip on
made available to some controls on ToolTip1 property of the control
the Form
Slide 42
Other Common VB Events
Slide 43
VB Events: Double Click
Slide 45
VB Events: Leave
Slide 46
Moving from List 1 to List 2
Slide 47
Moving from List 1 to List 2
2. Choose
event
3. Double-click
required event
1. Choose
control
Slide 48
Moving from List 1 to List 2
Note the Handles and the corresponding Event item for the sub.
Slide 49
Moving from List 1 to List 2
Slide 50
Moving from List 1 to List 2
Slide 51
Swapping Between Both List 1 to List 2
Slide 52
The Next Step
Slide 53
ACTIVITY 2 – Write code statement for all the buttons
Slide 54
Public Class Form1
End sub
End Sub
End class
Slide 55
Summary
1. Learn VB controls, property and events.
2. Design GUI by adding, grouping, arranging and aligning controls.
3. Describe and use VB.NET events: Form Load, Click, DoubleClick, KeyPress,
Enter, Leave correctly.
4. Enter VB code correctly according to event type and control.
5. Know how to get input, process and display output in Event-Driven
programming style.
6. Identify parts of VD code.
7. Design a user-friendly GUI by:
a. defining access keys
b. controlling the tab sequence
c. resetting the focus during program execution
d. causing ToolTips to appear
Slide 56