L3-Enhancing User Interface (Controls)
L3-Enhancing User Interface (Controls)
NET
Lesson 3: Controls
Controls in the Toolbox
• The Toolbox window displays controls
that you can add to Visual Studio
projects.
• View > Toolbox, or press CTRL+ALT+X.
• Use the pin to keep the toolbox open.
2
Form
• Microsoft Visual Studio -
File → New Project →
Windows Forms
Applications
• The container for all the
controls that make up the
user interface.
3
Label (lbl)
4
Button (btn)
5
Text Box (txt)
• Used for user input/data entry Sub textBoxDemo()
txtBox1.Text = "Type here"
• Text Property txtBox1.Location = New Point(20, 70)
• What is displayed in text box txtBox1.Width = 300
6
List Box (lst)
Sub listBoxDemo()
ListBox1.Items.Add(“Boxing")
ListBox1.Items.Add(“Running")
ListBox1.Items.Add("Cycling")
ListBox1.Items.Add("Tennis")
End Sub
7
Format ListBoxes
• ListBoxes can be formatted to display tabular information.
• Specify the format of the ListBoxes by using format string.
• In order to show neat and tabular report in a ListBox, the
FONT property should be set to Courier New.
8
Format ListBoxes (cont)
• Format a ListBox to display a 4-fields report.
fmtStr = "{0,-8}{1,-15}{2,-15}{3,-15}"
• fmtstr is a String variable. The 2 numerical values in the
curly braces are the field number and width of a column
respectively.
• For example: {0, -8} denotes the 1st field and -8 is the width
of the field – left justified.
9
Format ListBoxes Sample
Sub multipleForm()
Dim fmtStr As String
Dim intX As Integer
fmtStr = "{0,-8}{1,-15}”
ListBox1.Items.Add(String.Format(fmtStr, "X", "X+1”))
ListBox1.Items.Add(String.Format(fmtStr, "-", "---”))
For intX = 1 To 3
ListBox1.Items.Add(String.Format(fmtStr, intX, intX + 1))
Next
End Sub
10
ComboBox (cmb)
Sub comboBoxDemo()
ComboBox1.Items.Add("Boxing")
ComboBox1.Items.Add("Running")
ComboBox1.Items.Add("Cycling")
ComboBox1.Items.Add("Tennis")
End Sub
11
Group Box (grp)
•Used as containers for other
controls such as radio buttons and
check boxes
•Holds related items
•Improves readability of form
•Text Property
•What is displayed on the top edge of
the group box
12
Check Box (chk)
Sub btn1_Click() Handles btn1.Click
• Used for user input/data Dim str As String
13
Radio Button (rad)
Sub radio1() Handles RadioButton1.CheckedChanged
Me.ForeColor = Color.Red
End Sub
14
DateTime Picker
Sub dateTimeDemo()
Dim d1 As DateTime = DateTimePicker1.Value
Dim d2 As DateTime = DateTimePicker2.Value
Dim result As TimeSpan = d1.Subtract(d2)
Dim days As Integer = result.TotalDays
MsgBox(days)
End Sub
15
Picture Box (pic)
• Displays/contains a picture/graphic
• Image Property (holds the graphic)
• Complete path and filename of graphic
• .bmp, .gif (including animated), .jpg, .png, .ico, .emf,
.wmf
• SizeMode Property
• StretchImage causes graphic to be resized to match the
size of the control
• Visible Property
16
Selecting Multiple Controls
• SHIFT-Click or CTRL-Click to select/deselect
multiple controls
• Use the mouse to drag a selection box around
multiple controls
• To deselect all selected controls, click elsewhere on
the form
17
Selecting Multiple Controls
(cont.)
Start
here
Drag
to here
19
Designing the User Interface
•To the user the Interface should be
• Easy to understand
• Familiar
• Comfortable
• Organized
• Sans Serif Fonts are best, not boldface or large
• Color Neutral Overall
• Keyboard Accessible
20
Keyboard Access Keys
• Also referred to as Hot Sub buttonDemo()
btn1.Text = "&Ok"
Keys End Sub
• Underlined Letter
• User presses ALT +
underlined letter
• Use Windows-Standard
Keys
• Defined using Text
Text=&OK
Property
Text=E&xit
21
Default & Cancel Buttons
• User presses Enter instead of using the mouse
• Default Button
• Identified visually on Form by its darker outline
• Responds to ENTER key
• Form's AcceptButton Property
• Cancel Button
• Responds to ESC key
• Form's CancelButton Property
22
Focus
• One control on a Form Sub buttonDemo()
always has the Focus btn1.Focus()
• Not all control types can End Sub
receive the focus
• TabStop Property
(applicable only for
controls that are capable
of receiving the focus-
labels and pictures can’t)
• Designates whether a
control is allowed to
receive the focus; True or
False
23
Tab Order
• User should be able to use TAB key to move the
focus through a form in an organized manner; top
to bottom, left to right
• TabStop property (yes is the defaualt)
• TabIndex Property
• Number in tab sequence
• 0 for first control to receive the focus when the form
loads
• Use View Menu, Tab Order to set
24
Setting TabIndex Property
• View → TabOrder
• Click on each control in sequence to set TabIndex
property of controls automatically
25
Form's Screen Location
•StartPosition Property
•Manual
•CenterScreen
•WindowsDefaultLocation
•WindowsDefaultBounds
•CenterParent
26
ToolTips
•Small label that is displayed when user
places pointer on a control and pauses
•Usually used with Command Buttons
Sub tooltipDemo()
Me.ToolTip2.SetToolTip(btn1, "Click the button")
Me.ToolTip2.SetToolTip(txtBox1, "Fill in your name")
Me.ToolTip2.SetToolTip(Label1, "This is a label")
' Me.ToolTip2.SetToolTip(radio2, “Green Text")
End Sub
27
ToolTip Control
Component
Tray
28
Clearing Text Boxes & Labels
•Set Text Property equal to the Empty String
• Empty String is 2 quotation marks with no space
between them ("")
•Use the Clear Method of a Text Box
•Example:
txtName.Text = ""
lblMessage.Text = ""
txtCourse.Clear( )
29
Resetting the Focus
•Places the Insertion Point in a Text Box
•Use the Focus Method
•txtName.Focus( )
30
Checked Property of Check
Boxes and Radio Buttons
•Selects/Deselects Check Box or Radio Button
•Set Checked Property
• True = Checked, selected
• False = Unchecked, deselected
•Example:
radRed.Checked = True
chkBold.Checked = False
31
VB Color Constants
• ForeColor and BackColor Properties
• Use VB Color Constants from the Color Class
• View complete list in Help by using the keyword
Color followed by a period
• Example:
• txtName.ForeColor = Color.AliceBlue
• lblMessage.BackColor = Color.White
• Form1.BackColor = Color.FromArgb(255, 255, 0, 0)
32
With and End With
•Change several properties at once in Code
•Will run more efficiently
•Use the with statement.
•Example:
Sub textBoxDemo() Sub textBoxDemo()
txtBox1.Text = "Type here" With txtBox1
33
Concatenation
•Think of it as "glueing" text strings together
•Example: txtFName contains First Name
txtLName contains Last Name
34
Continuing Lines of Code
• For long lines of code, it is more readable to
continue them to the next line
• At the end of the line type Line Continuation
Character, a Space followed by an Underscore
• lblMessage.Text = txtFName.Text & "" & txtLName.Text & _
", welcome to Aiken Technical College. Today is " & _
txtToday.Text
35
Multiple Forms
Multiple Forms
• Multiple forms can be added to enhance a VB.Net
application.
• Multiple forms can be called by using its form name and its
show method.
• For example: me.close(),frmMenu.Show(),
frmMenu.Close(), etc.
37
Inserting a New Form
38
Setting a Form’s Properties
• Set form’s property such as Text, otherwise the form name
is used instead.
• Put a label on the from2
39
Displaying a Form
• Put the form’s show() mehtod in a button to activate a form.
• Use the form’s Show method to display a form. The close()
method closes a form.
• For example: frmMenu.Show() – make the Menu form
visible, frmMenu.Close() – closes the Menu form.
Sub showDemo() Handles Button1.Click
Form2.Label1.Text = TextBox3.Text
Form2.Show()
End Sub
40