0% found this document useful (0 votes)
16 views40 pages

L3-Enhancing User Interface (Controls)

This document provides an overview of various controls available in Visual Basic.NET, including their properties and usage within a Windows Forms application. It covers controls such as Labels, Buttons, Text Boxes, List Boxes, Combo Boxes, Group Boxes, Check Boxes, Radio Buttons, and more, detailing how to implement them in code. Additionally, it discusses user interface design principles, keyboard access keys, focus management, and handling multiple forms in an application.

Uploaded by

Lakeishaa Edward
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views40 pages

L3-Enhancing User Interface (Controls)

This document provides an overview of various controls available in Visual Basic.NET, including their properties and usage within a Windows Forms application. It covers controls such as Labels, Buttons, Text Boxes, List Boxes, Combo Boxes, Group Boxes, Check Boxes, Radio Buttons, and more, detailing how to implement them in code. Additionally, it discusses user interface design principles, keyboard access keys, focus management, and handling multiple forms in an application.

Uploaded by

Lakeishaa Edward
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Programming In Visual Basic.

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.

• If your window is messy, you can reset


it.
• Window > Reset Window Layout

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)

• Used for Sub labelDemo()


' Create a new label control
• Output on a form
Dim label1 As New Label()
• Identification of objects
• Directions/Information ' Set label properties
label1.Text = “Hello World"
• Cannot by modified by label1.Location = New Point(10, 10)
label1.ForeColor = Color.Orange
user
' Adjust the size to fit the text
label1.AutoSize = True

' Add the label to the form


Me.Controls.Add(label1)
End Sub

4
Button (btn)

• Used to run/ activate an Sub buttonDemo()


'set properties
Event Procedure btn1.Text = "Click Me"
btn1.Location = New Point(20, 40)
• It is generally used to btn1.BackColor = Color.Orange

generate a Click event btn1.ForeColor = Color.Red


End Sub
by providing a handler
for the Click event.
• Double click on the
button, an event
controller will be
created.

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

• What user entered in text box txtBox1.BorderStyle = BorderStyle.Fixed3D


txtBox1.TextAlign =
• TextAlign Property HorizontalAlignment.Center
End Sub
• Controls alignment of text in
the Text Box
• Change Event
• BorderStyle Property.
• None
• FixedSingle
• Fixed3D

6
List Box (lst)
Sub listBoxDemo()
ListBox1.Items.Add(“Boxing")
ListBox1.Items.Add(“Running")
ListBox1.Items.Add("Cycling")
ListBox1.Items.Add("Tennis")
End Sub

Private Sub Form1_MouseLeave() Handles ListBox1.MouseClick


MsgBox(ListBox1.SelectedItem.ToString() +
" is good for you.")
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

'ComboBox1 control does not have a MouseClick event handler


Private Sub cmbBox_MouseLeave() Handles ComboBox1.SelectedIndexChanged
MsgBox(ComboBox1.SelectedItem.ToString() + " is good for you.")
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

entry str = " "

• Allows the user to select or If CheckBox1.Checked = True Then


str &= CheckBox1.Text
deselect 1 or more in any str &= " "
End If
group
• Checked Property (stores If CheckBox2.Checked = True Then
str &= CheckBox2.Text

current state of the Check str &= " "


End If
Box)
• Checked = True If CheckBox3.Checked = True Then
str &= CheckBox3.Text
• Unchecked = False str &= " "
End If
• CheckChanged Event If str <> Nothing Then
MsgBox(str + vbLf + "Thank you")
• Occurs when user clicks on End If
one of the Check Boxes End Sub

13
Radio Button (rad)
Sub radio1() Handles RadioButton1.CheckedChanged
Me.ForeColor = Color.Red
End Sub

Sub radio2() Handles RadioButton2.CheckedChanged


Me.ForeColor = Color.Green
End Sub

Sub radio3() Handles RadioButton3.CheckedChanged


Me.ForeColor = Color.Blue
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

Using mouse to drag a Multiple selected controls,


selection box around observe selection handles.
multiple controls
18
What Can be Done with Multiple
Selected Controls?
•Use Format Menu or Layout Toolbar to
•Align them to each other
•Make same size
•Modify the spacing between them
•Move them as a group
•Set their common properties

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

txtBox1.Location = New Point(20, 70) .Text = "Type here"


.Location = New Point(20, 70)
txtBox1.Width = 300
.Width = 300
End Sub
End With
End Sub

33
Concatenation
•Think of it as "glueing" text strings together
•Example: txtFName contains First Name
txtLName contains Last Name

lblFullName.Text = txtFName.Text & " " &


txtLName.Text

lblNote.Text = "Today's weather is " &


txtWeather.Text & "."

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

Sub closeDemo() Handles Button2.Click


Form2.Close()
End Sub

40

You might also like