You are on page 1of 42

Unit I

Tool Box Controls


Tool Box Controls Picture Box
Pointer
Label Text Box
Frame Command Button
Check Box Option Button
Combo Box List Box
Horizontal Scroll Bar Vertical Scroll Bar
Drive List Box
Timer
Directory List Box File List Box
Shapes Lines
Image Box Data Tool
Object Linking Embedding
CONTROLS IN VISUAL BASIC

Define Controls in Visual Basic.

• A control is a object that can be drawn on a Form object to enable or enhance


user interaction with an application.
• The toolbox provides a set of standard controls.
• Each control has its own properties and it will react for a set of events.
How will you create controls in Visual Basic ?
Method 1
• A control in its default size and shape is created in the middle of a form by double
clicking on the tool in the tool box.
• The control thus created can be resized and moved to the desired position.
Method 2
• To draw a control on a form at a specific location with a specific size:
- Move the mouse pointer to the tool. Select a control by clicking it.
- Move the mouse pointer to the form where its shape is changed.
- Hold the mouse button down and drag the mouse to create the
control with the desired size.
How will you delete a control ?
- Select the control and choose EDIT>>Delete
or
- Select the control and press the delete key from keyboard.
Command Button
Pointer Picture Box
Label Text Box
Frame Command Button
Check Box
Option Button
Combo Box List Box
Horizontal Scroll Bar Vertical Scroll Bar
Timer Drive List Box
Directory List Box File List Box
Shapes Lines
Image Box Data Tool
Object Linking Embedding
Command Button
• Buttons in Visual Basic called "Command Buttons".
• It is the most widely used control.
• It is used to begin, interrupt, or end a particular
process.
• To add a button, click on the Command Button icon in
the Toolbox.
Command Button Properties:
Name Name property is the one by which the control is identified and it is referred as

the object name of that control.


Appearance Selects 3-D or flat appearance.
Caption String to be displayed on button.
Default Allows selection of button with Enter key (only one button on a form can have
this property True).
Font Sets font type, style, size.
BackColor Specifies the command button's background color.
Height Holds the height of the command button .
Width Holds the width of the command button .
ToolTipText Holds the text that appears as a ToolTip at runtime.
Visible Determines whether the command button appears or is hidden from the user.
Command Button Events:
• Click Triggered when a command button is clicked.
• DblClick Triggered when a command button is double-
clicked.
Sample Application

Private Sub Command1_Click()


Print "This is Command1"
End Sub

Private Sub Command2_Click()


Print "This is Command2"
End Sub
Text Box
Pointer Picture Box
Label Text Box
Frame Command Button
Check Box
Option Button
Combo Box List Box
Horizontal Scroll Bar Vertical Scroll Bar
Timer Drive List Box
Directory List Box File List Box
Shapes Lines
Image Box Data Tool
Object Linking Embedding
Text Box Contd….
A text box is used to display information entered at design time, by a user at runtime, or assigned within
code. The displayed text may be edited.
Text Box Properties:
• Appearance Selects 3-D or flat appearance.
• BorderStyle Determines type of border.
• Font Sets font type, style, size.
• MaxLength Limits the length of displayed text (0 value indicates unlimited length).
• MultiLine Specifies whether text box displays single line or multiple lines.
• PasswordChar Hides text with a single character.
• ScrollBars Specifies type of displayed scroll bar(s).
• SelLength Length of selected text (run-time only).
• SelStart Starting position of selected text (run-time only).
• SelText Selected text (run-time only).
• Tag Stores a string expression.
• Text Displayed text.
Tool Box Control- Text Box Contd….

Text Box Events:


• Change Triggered every time the Text property changes.
• LostFocus Triggered when the user leaves the text box.
• KeyPress Triggered whenever a key is pressed.
Sample Application – Addition of Two numbers

Private Sub Command1_Click()


Dim sum as integer
Sum = Val(Text1.Text) +Val(Text2.Text)
Text3.text = sum
End sub
Label Box
Pointer Picture Box
Label Text Box
Frame Command Button
Check Box Option Button
Combo Box List Box
Horizontal Scroll Bar Vertical Scroll Bar
Drive List Box
Timer
Directory List Box File List Box
Shapes Lines
Image Box Data Tool
Object Linking Embedding
Label Box
The label box is used to provide instructions to the users and also it can be used
to display outputs.
Label Properties:
Alignment Aligns caption within border.
Appearance Selects 3-D or flat appearance.
AutoSize If True, the label is resized to fit the text specified by
the caption property. If False, the label will remain the
size defined at design time and the text may be clipped.
BorderStyle Determines type of border.
Caption String to be displayed in box.
Font Sets font type, style, size.
Label Box
Label Events:
• Click Event triggered when user clicks on a label button.
• DblClick Event triggered when user double-clicks on a label
button.

.
Sample Application – Addition of Two numbers

Private Sub Command1_Click()


Dim sum as integer
Sum = Val(Text1.Text) +Val(Text2.Text)
Text3.text = sum
End sub

Here Number1, Number2 and Sum are


label boxes.
Picture Box
• The Picture Box is one of the controls that is used to handle graphics.
Methods to load picture in Picture Box
1. We can load a picture at design phase by clicking on the picture property in
the properties window and select the picture from the selected folder.
2. We can also load the picture at runtime using the LoadPicture method.
• For example, the statement will load the picture grape.gif into the picture box.
Picture1.Picture=LoadPicture ("C:\VBprogram\Images\grape.gif“)

• The image in the picture box is not resizable.


Picture Box Contd…
Picture Box Properties
AutoSize If True, box adjusts its size to fit the displayed graphic.
Font Sets the font size, style, and size of any printing done
Picture Establishes the graphics file to display in the picture box.

Picture Box Events:


• Click Triggered when a picture box is clicked.
• DblClick Triggered when a picture box is double-clicked.
Image Box
• The Image Box is another control that handles images and pictures.
• It functions almost identically to the picture box.
• However, there is one major difference, the image in an Image Box is
stretchable, which means it can be resized. This feature is not available in the
Picture Box.
Methods to Load Picture in the image box
• We can load a picture at design phase by clicking on the picture property in
the properties window and select the picture from the selected folder.
• We can also use the LoadPicture method to load the picture.
For example, the statement loads the picture grape.gif into the image box.
Image1.Picture=LoadPicture ("C:\VBprogram\Images\grape.gif")
Image Box
Image Box Properties:
• Picture Establishes the graphics file to display in the image box.
• Stretch If False, the image box resizes itself to fit the graphic.
If True, the graphic resizes to fit the control area.

Image Box Events:


• Click Triggered when a image box is clicked.
• DblClick Triggered when a image box is double-clicked.
The List Box
• The function of the List Box is to present a list of items where the user can click and select the
items from the list.
• In order to add items to the list, we can use the AddItem method.
Following example adds 4 items to the list box
Example 3.4
Private Sub Form_Load ( )
List1.AddItem “B.COM (CS)"
List1.AddItem “B.COM (A&F)"
List1.AddItem “B.COM (CA)"
List1.AddItem “B.COM (G)"
End Sub
• The items in the list box can be identified by the ListIndex property, the value of the ListIndex
for the first item is 0, the second item has a ListIndex 1, and the third item has a ListIndex 2
and so on
The List Box contd…
List Box Properties:
Appearance Selects 3-D or flat appearance.
List Array of items in list box.
ListCount Number of items in list.
ListIndex The number of the selected item in list. If no item is selected, ListIndex = -1.
Sorted True – Alphabetical order, False – as we entered
List Box Events:
Click Event triggered when item in list is clicked.
DblClick Event triggered when item in list is double-clicked.
List Box Methods:
AddItem Allows you to insert item in list.
Clear Removes all items from list box.
RemoveItem Removes selected item from list box
The Combo Box
• The function of the Combo Box is also to present a list of items where the user
can click and select the items from the list.
• However, the user needs to click on the small arrowhead on the right of the
combo box to see the items which are presented in a drop-down list.
• In order to add items to the list, you can also use the AddItem method.
Following example adds 4 items to the list box
Example
Private Sub Form_Load ( )
Combo1.AddItem “B.COM (CS)"
Combo1.AddItem “B.COM (A&F)"
Combo1.AddItem “B.COM (CA)"
Combo1.AddItem “B.COM (G)"
End Sub
The Combo Box contd…
COMBO Box Properties:
Appearance Selects 3-D or flat appearance.
List Array of items in list box.
ListCount Number of items in list.
ListIndex The number of the selected item in list. If no item is selected, ListIndex = -1.
Sorted True – Alphabetical order, False – as we entered
COMBO Box Events:
Click Event triggered when item in list is clicked.
DblClick Event triggered when item in list is double-clicked.
COMBO Box Methods:
AddItem Allows you to insert item in list.
Clear Removes all items from list box.
RemoveItem Removes selected item from list box
Example - List Box and Combo box
Example - List Box and Combo box
The Check Box
• The Check Box control lets the user select or unselect an option.
• When the Check Box is checked, its value is set to 1 and when it is unchecked, the
value is set to 0.
• You can include the statements Check1.Value=1 to mark the Check Box and
Check1.Value=0 to unmark the Check Box.
Example
Private Sub Command1_Click()
If Check1.Value = 1 And Check2.Value = 0 Then
MsgBox “You have selected Ooty as your tourist spot”
ElseIf
Check2.Value = 1 And Check1.Value = 0 Then
MsgBox " You have selected Munnar as your tourist spot "
Else MsgBox “You have selected Ooty and Munnar as tourist spot"
End If
End Sub
The Check Box Contd….
Check Box Properties:
Caption Identifying text next to box.
Font Sets font type, style, size.
Value Indicates if unchecked (0), checked (1)

Check Box Events:


Click Triggered when a box is clicked. Value property is
automatically changed by Visual Basic.
The Option Box
• The Option Box control also lets the user select one of the choices.
• Only one Option Box can be selected at one time.
• When an option box is selected, its value is set to “True” and when it is
unselected; its value is set to “False”.
Example
Private Sub Command1_Click()
If Option1.Value = 1 Then
MsgBox “You are male”
ElseIf
Option2.Value = 1 Then
MsgBox " You are female”
Else
MsgBox “You have not selected gender, pls select"
End If
The Option Box contd…
Option Button Properties:
Caption Identifying text next to button.
Font Sets font type, style, size.
Value Indicates if selected (True) or not (False).

Option Button Events:


Click Triggered when a button is clicked. Value
property
is automatically changed by Visual Basic.
Check Box and Option Button - Examples
Frame Control
• Frames provide a way of grouping related controls on a form
• To group controls in a frame, first draw the frame. Then, the
associated controls must be drawn inside the frame. This allows us
to move the frame and controls together.
• Option buttons within a frame work as a group, independently of
option buttons in other frames.

Frame Properties:
Caption Title information at top of frame.
Font Sets font type, style, size.
Frame Control
Message Box ( ) Function
• Displays a message in a dialog box and wait for the user to click a button, and
returns an integer indicating which button the user clicked.
Syntax :
MsgBox ( Prompt [,icons+buttons ] [,title ] )
memory_variable = MsgBox ( prompt [, icons+ buttons] [,title] )
• Prompt : String expressions displayed as the message in the dialog box.
• Icons + Buttons : Numeric expression that is the sum of values specifying the number and type of
buttons and icon to display.
• Title : String expression displayed in the title bar of the dialog box.
Message Box () Function Contd…
ICONS
Message Box () Function Contd…
Buttons
Constant Value Description
vbOkOnly 0 Display OK button only
vbOkCancel 1 Display OK and Cancel
vbAbortRetryIgnore2 Display Abort, Retry and Ignore
vbYesNoCancel 3 Display Yes, No and Cancel buttons
vbYesNo 4 Display Yes and No buttons
vbRetryCancel 5 Display Retry and Cancel
Message Box Contd…
Return Values
Constant Value Description
vbOk 1 Ok Button
vbCancel 2 Cancel Button
vbAbort 3 Abort Button
vbRetry 4 Retry Button
vbIgnore 5 Ignore Button
vbYes 6 Yes Button
vbNo 7 No Button
Message Box () Function Contd…
• MsgBox("Click to Test", vbYesNoCancel + vbExclamation, "TestMessage")
Message Box () Function Contd…
• MsgBox ("No names is selected", vbInformation, "Error“)

• answer = MsgBox("Are you sure you want to delete " ,vbCritical + vbYesNo, "Warning")

• MsgBox ("Selected name was deleted", vbInformation, "Delete Confirm“)

• answer = MsgBox("Do you want to quit?", vbExclamation + vbYesNo, "Confirm")

• MsgBox "Action canceled", vbInformation, "Confirm“


The InputBox( ) Function
• An InputBox( ) function will display a message box where the user can enter a value or a message in the
form of text.
• The format is
myMessage=InputBox(Prompt, Title, default_text, x-position, y-position)

myMessage is a variant data type but typically it is declared as string, which accept the message input by
the users. The arguments are explained as follows:

• Prompt - The message displayed normally as a question asked.


• Title - The title of the Input Box.
• default-text - The default text that appears in the input field.
• x-position and y-position - the position or the coordinate of the input box.
The InputBox( ) Function contd...
Example:
userMsg = InputBox("What is your message?", "Message Entry Form", "Enter
your messge here", 500, 700)

You might also like