[go: up one dir, main page]

0% found this document useful (0 votes)
35 views13 pages

Cit2457 VB Unit 4

Uploaded by

animelab186
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)
35 views13 pages

Cit2457 VB Unit 4

Uploaded by

animelab186
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/ 13

UNIT 4

Control: A control is a tool you use to create objects on a Visual Basic form. We can select
controls from the toolbox and use the mouse to draw objects on a form. Most of the controls
to create user interface elements, such as command buttons, image boxes, and list boxes.

Object : An object is a type of user interface element you create on a Visual Basic form by
using a toolbox control. (In fact, in Visual Basic, the form itself is also an object.) We can
move, resize, and customize objects by setting object properties and customize Visual Basic
objects by using event procedures that are fine-tuned for different conditions in a program.

Property: A property is a value or characteristic held by a Visual Basic object, such as


Caption or
Fore Color. Properties can be set at design time by using the Properties window or at run time
by using statements in the program code. In code, the format for setting a property is:
Object. Property = Value
Where Object is the name of the object you’re customizing. Property is the characteristic you
want to change. Value is the new property setting.
For example,
Command1.Caption = "Hello"
Could be used in the program code to set the Caption property of the Command1 object to
“Hello”.

Toolbox Controls, controls are the design tools you


use to build the user interface of a Visual Basic
program. After you place controls on a form, they
become objects that you can customize with property
settings and program code.

LABEL
Label, the simplest control in the Visual Basic
toolbox, displays formatted text on a user interface
form. It is used to display static text, titles and
screen output from operations.
Typical uses for the Label control include:
 Help text
 Program splash screen headings
 Formatted output, such as names, times, and
dates
 Descriptive labels for other objects, including
text boxes and list boxes.
A label is a control use to display text that a user can't
edit directly. The text of a label box can be changed at
run-time in response to events.

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 specifed 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 EVENTS
Click - Event triggered when user clicks on a label.
DblClick - Event triggered when user double-clicks on a label.

Creating Labels on a Form


• To create a label control on a form, we surely refer to the toolbox window to select the label
icon, shown as capital letter “A”. When the label control is selected, the label can be placed
on a form by creating a rectangle with the mouse, which is held left button clicked. Once the
left button is released, the label of size as the rectangle created is placed on the form.

Creating Labels in Code


• You can also set label properties with program code, as shown in the following. The
program codes below, when command1 button is clicked, will set the caption of label1 as
“Welcome” and label2 as “Please enter your name below:”
Private Sub Command1_Click ()
Label1.Caption = "Welcome"
Label2.Caption = "Please enter your name below:"
End Sub

TEXTBOX
The text box is the standard control for accepting input from the user as well as to display
the output. It can handle string (text) and numeric data but not images or pictures. Strings in
a text box can be converted to a numeric data by using the function Val(text). The data typed
in is in the Text property of the control.

This control performs two functions:


• Displaying output (such as operating instructions or the contents of a file) on a form.
• Receiving text (such as names and phone numbers) as user input.

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).
Text - Displayed text.

Text Box Events:


Change - Triggered every time the Text property changes.
LostFocus - Triggered when the user leaves the text box. This is a good
place to
examine the contents of a text box after editing.
KeyPress - Triggered whenever a key is pressed. Used for key trapping, as
seen in last class.

Text Box Methods:


SetFocus - Places the cursor in a specified text box.
FRAME CONTROLS
 Frame controls are similar to Label controls in that they can serve as captions for
those controls that don't have their own.
 Frame controls can also (and often do) behave as
containers and host other controls.
 In most cases, you only need to drop a Frame control on
a form and set its Caption property.
 If you want to create a borderless frame, you can set its
BorderStyle property to 0-None.
 Controls that are contained in the Frame control are said
to be child controls.

COMMAND BUTTON
A Command Button control is used to create buttons with a variety of uses on a form. It
displays an illusion that the button is pressed when the user click on it. The Command button
is the most basic way to get user input while a program is running. By clicking a command
button, the user requests that a specific action be taken in the program.

Here are some type of command buttons in a program:


OK - Accepts a list of options and indicates that the user is ready to
proceed.
Cancel - Discards a list of options.
Quit - Exits an open dialog box or program.

Command Button Properties:


Appearance - Selects 3-D or flat appearance.
Caption - String to be displayed on button.
Font - Sets font type, style, size.

Command Button Events:


Click - Event triggered when button is selected either by clicking on it or
by pressing the access key.

Changing Command Button Properties


You can change command button properties (like those of all objects) in two ways:
1) By adjusting property settings in the Properties window.
2) By changing properties with program code.

OPTION BUTTON
Option buttons, also called radio buttons, are typically used in a group of two or more. At
any one time, only one button in the group can be "on". Clicking an option button turns it
"on" and turns all other buttons in the group "off".
The Option Button control satisfies these requirements.
 It displays a list of choices on a form and requires the user to pick one (only one) item
from a list of choices.
 It returns true (1) or false (0) value.

Creating Option Buttons


You create an option button on a form by clicking OptionButton in
the toolbox and then drawing on the form.

Properties of Option Buttons


 The common properties like caption, name, enabled etc., are applicable for option
button.
 The important property of option button is value. If we set value as true, it becomes
selected else the option button becomes not selected.
Grouping Option Buttons
 If you want to create a group of option buttons that work together, you must first
create a frame for the buttons.
 Next, place your option buttons inside the frame so that they can be processed as a
group in your program code and moved as a group along with the frame.

CHECKBOX
The CheckBox control is similar to the option button, except that a list of choices can be
made using check boxes where you cannot choose more than one selection using an
OptionButton. By ticking the CheckBox the value is set to True.

Properties

If you want to display the control in a checked state, we set its Value property to 1-Checked
right
in the Properties window, we set a grayed state with 2-Grayed and value = 0 for Unchecked

Events
 The only important event for CheckBox controls is the Click event, which fires
when either the user or the code changes the state of the control.
 You usually write code in a CheckBox control's Click event when it affects the state
of other controls.

For example, the program will change the background color of the form to red when the
check
box is unchecked and it will change to yellow when the check box is checked.

Private Sub Check1_Click()


If Check1.Value = vbChecked Then
Form1.BackColor = vbRed
End If
If Check1.Value = vbUnchecked Then
Form1.BackColor = vbYellow
End If
End Sub

LIST BOX
When you want a user to choose a single response from a long list of choices, you use the
ListBox control. (Scroll bars appear if the list is longer than the list box.)
.
Adding items to a List
It is possible to populate (fill) the list at design time or run time

Design Time : To add items to a list at design time, click on List


property in the property box
and then add the items. Press CTRL+ENTER after adding each item as
shown below.
Run Time : The AddItem method is used to add items to a list at run time. The AddItem
method
uses the following syntax.
Object.AddItemitem, Index
The item argument is a string that represents the text to add to the list The index argument is
an integer that indicates where in the list to add the new item. Not giving the index is not a
problem, because by default the index is assigned.

Removing Items from a List


The RemoveItem method is used to remove an item from a list. The syntax for this is given
below.
Object.RemoveItem index

Properties of List Box


Property/Method Description
Index Specifies the Control array index
List String array. Contains the strings displayed in the drop-down
list. Starting array index is 0.
ListCount Integer. Contains the number of drop-down list items
ListIndex Integer. Contains the index of the selected ListBox item. If
an item is not selected, ListIndex is -1
Sorted Boolean. Specifies whether the ListBox's items are sorted or
not.

Methods of List Box


AddItem Add an item to the ListBox
Clear Removes all items from the ListBox
RemoveItem Removes the specified item from the ListBox
SetFocus Transfers focus to the ListBox

Event Procedures
Change Called when text in ListBox is changed
DropDown Called when the ListBox drop-down list is displayed
GotFocus Called when ListBox receives the focus
LostFocus Called when ListBox loses it focus

Sorting the List


The Sorted property is set to True to enable a list to appear in alphanumeric order and False
to display the list items in the order which they are added to the list.

COMBOBOX
A ComboBox combines the features of a TextBox and a ListBox. This enables the user to
select either by typing text into the ComboBox or by selecting an item from the list.

There are three types of ComboBox styles.


 Dropdown Combo (style 0)
 Simple Combo (style 1)
 Dropdown List (style 2)

The Simple Combo box displays an edit area with an attached list box always visible
immediately below the edit area. A simple combo box displays the contents of its list all the
time. The user can select an item from the list or type an item in the edit box portion of the
combo box. A scroll bar is displayed beside the list if there are too many items to be
displayed in the list box area.

The Dropdown Combo box first appears as only an edit area with a down arrow button at
the right. The list portion stays hidden until the user clicks the down-arrow button to drop
down the list portion. The user can either select a value from the list or type a value in the
edit area.

The Dropdown list combo box turns the combo box into a Dropdown list box. At run time ,
the control looks like the Dropdown combo box. The user could click the down arrow to view
the list. The difference between Dropdown combo & Dropdown list combo is that the edit
area in the Dropdown list combo is disabled. The user can only select an item and cannot type
anything in the edit area.

Difference Between List Box and Combo Box

List Box Combo Box


The listbox control is used to display a list of Combobox control is used to display a drop-
items to a user. down list of items. It is a combination of a text
box in which a user enter an items, and a drop-
downlist from which the user selects an item.
ListBox u can select multiple items Combo box u can select only one item.
Occupies more space but shows more than one Occupies less space but shows only one value for
value visibility
we can use checkboxes with in the listbox. can't use checkboxes within combo boxes
We can select multiple items Multiple select is not possible

HORIZONTAL & VERTICAL SCROLL BAR


The ScrollBar is a commonly used control, which enables the user to select a value by
positioning it at the desired location.
 It represents a set of values.
 The Min and Max property represents the minimum and maximum value.
 The value property of the ScrollBar represents its current value that may be any
integer between minimum and maximum values assigned.
 The HScrollBar and the VScrollBar controls are perfectly identical, apart from their
different orientation.
 SmallChange is the variation in value you get when clicking on the scroll bar's
arrows, and LargeChange is the variation you get when you click on either side of
the scroll bar indicator.
 The default initial value for those two properties is 1, but you'll probably have to
change LargeChange to a higher value.

eg: Show the current scroll bar's value


Private VScroll1_Change()
Label1.Caption = VScroll1.Value
End Sub

Private VScroll1_Scroll()
Label1.Caption = VScroll1.Value
End Sub

TIMER CONTROL
The Timer control allows you to perform a task at a specified interval or to wait for a
specified length of time.
 To execute a group of statements for a specified period of time, you can use the Timer
control in the Visual Basic toolbox.
 The Timer control is an invisible stopwatch that gives your programs access to the
system clock.
You can use the Timer to:
 Count down from a preset time.
 Delay a program.
 Repeat an action at prescribed intervals.
Objects that you create with the Visual Basic Timer:
 Are accurate to 1 millisecond (1/1000 of a second).
 Aren’t visible at run time.

Setting the Timer Interval


 To set a timer interval, you start by using the timer Interval property.
 Activate the timer by setting the timer Enabled property to True.
 When a timer is enabled, it runs constantly. The program executes the timer event
procedure at the prescribed interval — until the user stops the program (the timer is
disabled, or the Interval property is set to 0).

Example : Create a digital clock using a timer and a simple label object

Set the properties of each object as follows


control properties
Form Caption Digital Clock
Timer Interval 1000
Enabled True
Label Alignment 2-Center

Private Sub Timer1_Timer


Label1.Caption = Time
End Sub

DRIVE LIST BOX


 The Drive ListBox is used to display a list of drives available in your computer.
 When you place this control into the form and run the program, you will be able to
select different drives from your computer.

DIRECTORY LIST BOX


 The Directory List Box is used to display the list of directories or
folders in a selected drive.
 When you place this control into the form and run the program,
you will be able to select different directories from a selected
drive in your computer

FILE LIST BOX


 The File List Box is used to display the list of files in a selected directory or folder.
 When you place this control into the form and run the program, you will be able to a
list of files in a selected directory

PICTURE BOX
The Picture Box controls is used to handle graphics.
 You can load a picture during the designing phase by clicking on
the picture property in the properties window and select the
picture from the selected folder.
 You can also load the picture at runtime using the LoadPicture
method.
 The image in the picturebox is not resizable.

For example,
Picture1.Picture=LoadPicture ("D:\VB\Images\amc.jpg")

IMAGE BOX
The Image Box is another control that handles images and pictures. It functions almost
identically to the picture box.
One major difference between picture and Image control, the image in an Image Box is
stretchable, which means it can be resized. This feature is not available in the Picture Box.

For example
Image1.Picture=LoadPicture ("D:\VB\Images\amc.jpg")

LINE CONTROL
The Line control is a decorative control whose only purpose is let you draw one or more
straight lines at design time, instead of displaying them using a Line graphical method at
run time.

Properties:
BorderColor (the color of the line),
BorderStyle (the same as a form's DrawStyle property),
BorderWidth (the same as a form's DrawWidth property),
Line method at run time is usually better in terms of performance.

SHAPE CONTROL
The Shape control is an extension of the Line control. Shape property is important for this
control. Six basic shapes Rectangle, Square, Oval, Circle, Rounded Rectangle, and
Rounded Square are listed in Shape control..
It supports all the Line control's properties and a few more: BorderStyle (0-Transparent, 1-
Solid), FillColor, and FillStyle (the same as a form's properties with the same names).
Coding for Font Dialog box

Private Sub Form_Load()


For i = 1 To Screen.FontCount - 1
List1.AddItem Screen.Fonts(i)
Next
For i = 8 To 72 Step 2
List3.AddItem (i)
Next
List2.AddItem "Regular"
List2.AddItem "Italic"
List2.AddItem "Bold"
List2.AddItem "BoldItalic"
End Sub

Private Sub Check1_Click()


If Check1.Value = 1 Then
Label4.FontStrikethru = True
Else
Label4.FontStrikethru = False
End If
End Sub

Private Sub Check2_Click()


If Check2.Value = 1 Then
Label4.FontUnderline = True
Else
Label4.FontUnderline = False
End If
End Sub

Private Sub Command2_Click()


End
End Sub

Private Sub List1_Click()


Text1.Text = List1.Text
Label4.Font = Text1.Text
End Sub

Private Sub List2_Click()


Text2.Text = List2.Text
If List2.ListIndex = 0 Then
Label4.FontItalic = False
Label4.FontBold = False
End If
If List2.ListIndex = 1 Then
Label4.FontItalic = True
Label4.FontBold = False
End If
If List2.ListIndex = 2 Then
Label4.FontBold = True
Label4.FontItalic = False
End If
If List2.ListIndex = 3 Then
Label4.FontBold = True
Label4.FontItalic = True
End If
End Sub

Private Sub List3_Click()


Text3.Text = List3.Text
Label4.FontSize = Val(Text3.Text)
End Sub
Coding for Calculator
Dim a As Double
Dim b As Double
Dim c As Double
Dim op As String

Private Sub Command1_Click(Index As Integer)


Text1.Text = Text1.Text + Command1(Index).Caption
End Sub

Private Sub Command2_Click()


Text1.Text = Clear
End Sub

Private Sub divide_Click(Index As Integer)


a = Val(Text1.Text)
op = "/"
Text1.Text = Clear
End Sub

Private Sub dot_Click(Index As Integer)


If InStr(Text1.Text, ".") Then
Exit Sub
Else
Text1.Text = Text1.Text + "."
End If
End Sub
Private Sub equalto_Click(Index As Integer)
b = Val(Text1.Text)
If op = "+" Then
c=a+b
End If
If op = "-" Then
c=a-b
End If
If op = "x" Then
c=a*b
End If
If op = "/" And b <> "0" Then
c=a/b
End If
If op = "%" Then
c = a Mod b
End If
Text1.Text = c
End Sub

Private Sub minus_Click(Index As Integer)


a = Val(Text1.Text)
op = "-"
Text1.Text = Clear
End Sub

Private Sub mod_Click(Index As Integer)


a = Val(Text1.Text)
op = "%"
Text1.Text = Clear
End Sub

Private Sub multiply_Click(Index As Integer)


a = Val(Text1.Text)
op = "x"
Text1.Text = Clear
End Sub

Private Sub onebyx_Click(Index As Integer)


If Val(Text1.Text) <> 0 Then
Text1.Text = 1 / Val(Text1.Text)
End If
End Sub

Private Sub plus_Click(Index As Integer)


a = Val(Text1.Text)
op = "+"
Text1.Text = Clear
End Sub

Private Sub plusminus_Click(Index As Integer)


Text1.Text = -Val(Text1.Text)
End Sub

Private Sub sqrt_Click(Index As Integer)


a = Sqr(Val(Text1.Text))
Text1.Text = a
End Sub

You might also like