vb chap 04
TRANSCRIPT
-
8/3/2019 VB chap 04
1/12
KHEC II/II Visual Programming (BEG275CO) - 1 -
CHAPTER FOUR
4.0 FOUNDMENTAL VISUAL PROGRAMMING TOOLS AND CONTTOLS:
4.1 THE COMMAND BUTTON CONTROL, LABEL CONTORL:COMMAND BUTTON CONTROL:
A commandbutton performs a tack when the user clicks on the button. A commandbutton control is used for
begin, interrupt or end a process. The most common event for a commandbutton control is the click event.Within the commandbutton control some of the commonly used properties are cancel, caption, default, font,
name, tooltip, text, method is setfocus and events are click, mousedown, mousemove.
Basic properties of command button are:
Appearance: select 3D or flat appearance.
Cancel: allows selection of button with ESC key (only one button on a form can have this property true).
Caption: strings to be displayed on the 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
Events:
Click: click event triggered when a button is selected either by clicking on it or by pressing the access key.
LABEL CONTROL:
Label is a graphical control used to display text. Label displays read only text i.e. the user can't edit the text
directly during the run time of the program. The property which is set in a label control is the caption propertywhich can be set either at design time or run time. At run time programmers can set the caption property to
provide instructions or additional help to the user by associating with textboxes, listboxes, comboboxes etc.
For e.g. place a label control on the form and code the following:Private sub form_load()
form1.caption="starting the VB"label1.caption="use of label control"label1.alignment=2
label1.font="arial black"
End sub
4.2 THE TEXT BOX CONTROL, SCROLL BAR CONTROL:TEXT BOX CONTROL:
Text boxes are commonly used for accepting users input or for entering data. Text boxes can be used in
conjunction with a data control to display information from a database. It is also used to set up database queries
or to edit records in database. So, textbox control is a small text editor that provides all the basic text editing
facilities i.e. inserting and selecting text, scrolling text if it does not fit in the control's area etc. Multiple lines oftext can be displayed by using the multiline property of the textbox control in conjunction with the scrollbars
property.
Basic properties of text box are:
Appearance: select 3D or flat appearance.
Border Style:
Multiline: True/False (true allows to enter more than 1 line to display).
Maxlength: decides maximum length of character to be entered.
Font: sets font type, style, size
Compiled By: Er. SRB
-
8/3/2019 VB chap 04
2/12
KHEC II/II Visual Programming (BEG275CO) - 2 -
Password char: specifies the character displayed in the text box. If the text box's password character property
is set to any character the user can't copy or cut the text.
Text: displays text.
Scroll bar: specifies types of displayed scroll
Locked: used to prevent users from editing text box contents. If locked property is set to true then it allowsusers to scroll and high light text in a text box. Without allowing changes with the locked property set to
true, copy command will work in a text box but cut and paste command won't.
Password char: this property displays the symbol such as * or # for representing the no. of character inputtedin the text box.
Events:
Change: change event is generated each time a new character is typed or deleted.
For e.g. Place a label and text control on the form and code the following:
Private sub form_load()form1.caption="its a text"
label1.caption="enter the name:"
label1.alignment=2text1.text="my name is rocky"
End sub
SCROLL BAR CONTROL:
A scroll bar control is completely an independent object that exit without reference to any other control. The
horizontal and vertical scroll bars are identical as they share the same properties, event and methods. It is givenan 'hsp' prefix for Hscroll and 'vsb' for Vscroll.
Properties:
Large change: integer amount the property value changes by when the area between the scroll box and scrollarrow is clicked.
Small change: integer amount the property value changes by when the scroll arrow is pressed.
Max/min: integer the largest/smallest value the property value can have.
Value: the position of the scroll box within the scrollbar.
For e.g. draw a label control on the form and set its name property to lblscroll and its caption property to visualbasic. And also set the borderstyle of the label to 1. Then, draw Hscrollbar and name it as hsbscroll and
Vscrollbar and name it as vsbxcroll from project properties. Then double click on Hscrollbar-hsbscroll and code
followingPrivate sub hsbscroll_change()
Lbltext.fontsize=25
End sub
Now double click on Vscrollbar-vscroll control and code the following
Private sub vsbscroll_change()
Lbltext.fontbold=trueEnd sub
Q) Design an interface that contains one text box, one horizontal bar and one vertical scroll bar. Now WAP tochange the color of text box while user moves the scroll bar.
Compiled By: Er. SRB
-
8/3/2019 VB chap 04
3/12
KHEC II/II Visual Programming (BEG275CO) - 3 -
Source code:
Private sub form_load()Hscroll1.max=255
Vscroll1.max=255
Hscroll1.min=0Vscroll1.min=0
Private sub Hscroll1_change()
Text1.backcolor=RGB(hscroll1.value, vscroll1.value, 0)End sub
Private sub Vscroll1_change()
Text1.backcolor=RGB(hscroll1.value, vscroll1.value, 0)End sub
Q) in case of three horizontal scroll bar: the design and code is written below:
Private sub form_load()Hscroll1.max=255
Hscroll2.max=255
Hscroll3.max=255Hscroll1.min=0
Hscroll2.min=0
Hscroll3.min=0
Private sub Hscroll1_change()
Text1.backcolor=RGB(hscroll1.value, Hscroll2.value, Hscroll3.value)End sub
Private sub Hscroll2_change()
Text1.backcolor=RGB(hscroll1.value, Hscroll2.value, Hscroll3.value)End sub
Private sub Hscroll3_change()
Text1.backcolor=RGB(hscroll1.value, Hscroll2.value, Hscroll3.value)End sub
Compiled By: Er. SRB
-
8/3/2019 VB chap 04
4/12
KHEC II/II Visual Programming (BEG275CO) - 4 -
4.3 THE TIMER CONTROL:
The timer control always remains hidden at run time. It can execute code at regular intervals by causing a timerevent. Timer control is given a 'tmr' prefix.
Some properties of timer are given below
Descriptionproperty
The interval property is measured in milliseconds and is represented by a value from 1 to 65535interval
The enabled property determines if the timer control will invoke the timer event in the time
specified by the interval property.
enabled
For e.g. Draw the following design in VB
Change the text box's property for password char to * and timer interval to 15000 and enabled to true.
Then code the following
Private sub timer1_timer()
Msgbox("sorry, your time is up")
End sub
Private sub command1_click()If text1.text="secret" then
Timer1.enabled=false
Msgbox("welcome")Else
Msgbox("sorry")
End ifEnd sub
4.4 FILE SYSTEM COTROL (DRIVE LIST BOX, DIRECTORY LIST BOX AND FILE LIST BOX):DRIVELISTBOX:
Drive list box is normally used in conjunction with the DirList control and filelist controls. It is used to displaya list of all the valid drives in a dropdown combo box in a user's system. Drive list box control is given a 'drv'
prefix.
Properties:
Drive: contains the name of the currently selected drive.
DIRLISTBOX:Dir list box is normally used in conjunction with the Drivelist box control and filelist control to display
directories and paths. Dirlist box control is given 'dir' prefix. So it displays an ordered, hierarchical list of theuser's disk directories and sub directories.
Property:
Path: contains the current directory path.
FILE LIST BOX:
The filelist box comes at the end of the drive-directory-file chain. This control is used to display a list of files in
the directory specified by the path. Filelist box control is given a fil prefix.
Compiled By: Er. SRB
-
8/3/2019 VB chap 04
5/12
KHEC II/II Visual Programming (BEG275CO) - 5 -
Properties:
File name: contains the currently selected file name.
Path: contains the current path directory.
Pattern: contains a string that determines which file will be displayed.
For e.g. draw the following interface in VB and code accordingly.
Private sub cmdfind_click()File1.Pattern = Text1.Text
End sub
Then code in drivelist box as:
Private sub Drive1_change ()
Dir1.Path = Drive1.DriveEnd sub
And lastly on dirlist box as:
Private sub Dir1_change ()
File1.path =Dir1.path
End sub
4.5 IMAGE BOX AND PICTURE BOX CONTROL:PICTURE BOX:
Picture box allows to place graphics information on the form. It is best suited for dynamic environments. It is
located at the top layer of the form display. It provides methods for drawing at runtime and is much more
flexible then image control but it uses more resources.
Properties: Autosize: when it is true, then box adjusts its size to fit the displayed graphics i.e. if the autosize property is
true, the control is resized to the dimensions of the image it contains. If it is false, only the part of the imagethat can fit on the control is displayed.
Picture: Establishes the graphics file to display in the picture box.
For e.g.Picture1.picture=loadpicture(path)
IMAGE BOX:
Compiled By: Er. SRB
-
8/3/2019 VB chap 04
6/12
KHEC II/II Visual Programming (BEG275CO) - 6 -
It is designed specially for displaying images and not for creating new images or manipulating them. It is good
for displaying graphics and uses fewer resources than picture box controls.
Property:
Stretch: if it is set to true value then the image is resized to fill the area of the imagebox control. If it is false,then the imagebox control behaves like a picutebox with its autosize property set to true.
Q) WAP which load the picture in picture box control and display the picture in a certain definite interval oftime.
First of all design the following interface containing a text box, timer, list box, filelist box, and a picture box as
shown bleow
Dim i, j As IntegerPrivate Sub Form_Load()
Timer1.Enabled = True
Timer1.Interval = 2000File1.Path = "c:\picture"
File1.Pattern = "*.jpg"For i = 0 To 10
List1.AddItem File1.List(i)Next i
File1.Visible = False
End Sub
Private Sub Timer1_Timer()
If j = i Then j = 0File1.FileName = List1.List(j)
Picture1.Picture = LoadPicture(File1.Path & "\" & File1.FileName)
Text1.Text = File1.Path & "\" & File1.FileNamej = j + 1End Sub
4.6 ADVANCE GRAPHICS FEATURE (SHAPE, LINE CONTROLS & SOME GRAPHICAL
METHODS):SHAPE CONTROL:
It is a graphical control used to create the predefined shapes on the form, frames or picture boxes.
Some predefined shapes are listed belowRectangle-0
Square-1
Compiled By: Er. SRB
-
8/3/2019 VB chap 04
7/12
KHEC II/II Visual Programming (BEG275CO) - 7 -
Oval-2
Circle-3Rounded Rectangle-4
Rounded Square-5
Properties:
Shape: 0 to 5
Backcolor: determines the background color of the shape.
Backstyle: determines whether the background is transparent or opaque.
Bordercolor: determines the color or shapes outline.
Borderwidth: determines the width of the shape border line.
Fill color: defines the interior color of the shape.
Fill Style: determines the interior pattern of the shape.
LINE CONTROL:
This control creates simple straight line segments like horizontal, vertical and diagonal of various width and
color. User can draw lines from selecting the line control from the tool box during design time and can also
draw at run time as explained below.For e.g.:
Place a command button on the form and change its caption to clear, and then code the following
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Form1.CurrentX = X
Form1.CurrentY = YEnd Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
Line (Form1.CurrentX, Form1.CurrentY)-(X, Y)End If
End Sub
Private Sub Cmdclr_Click()Form1.Cls
End Sub
This application will allow the user to freely draw different lines according to the movement of the mouse and
on clicking on clear button the lines are deleted from the form.
4.7 LIST BOXES AND COMBO BOXES:LIST BOXES:
A list box control displays a list from which the user can select one or more items. The user cant edit the data
in list box directly. List box is associated with a vertical scroll when the list of data is too long for the listbox.
Properties:
Selcount: reports the no. of selected items in a list.
Multiselect: if 0 then no multiple selection, if 1 then multiple selection and if 2 then group selection.
Sorted: True/False
Text: text of most recently selected item.
Place a list box in the form then
Compiled By: Er. SRB
-
8/3/2019 VB chap 04
8/12
KHEC II/II Visual Programming (BEG275CO) - 8 -
i) To add item on list, the additem method is used. Its syntax is as follows
list1.additem item.indexThe item parameter is the string to be added to the list, index is its order. The first items order in the list is zero
The index argument is optional; if it is omitted then string is appended to the bottom of the list. E.g.:
Code the following in the forms load event as
Private sub form_load()Lstsubj.additem VB
Lstsubj.additem TOCLstsubj.additem CLstsubj.additem COD
End sub
ii) To remove an item from the list, specify item position (index) in the list and supply it to removeitem method
Its syntax is as follows.
List1.removeitem indexe.g. lstsubj.removeitem 1
iii) Clear: clear method removes all the items from the control. Its syntax is as follows
list1.cleariv) Listcount: this is the no. of item in the list. The item in the list can be accessed with an index value, which
goes from 0 to listcount-1.
v) List (): this is an array that holds the lists item. The element list (0) holds the first element of the list and so
on up to list (listcount-1), holds the last item.
vi) List index: this is the index of the selected item in the list. If multiple items are selected list index is the
index of the most recently selected item. This property can be used to access specific elements in the list or
delete specific items. List index of the text in list is 1 when it is selected otherwise it is negative value.
E.g. list1.removeitem list1.listindex
The example below show the different operations in the listbox.
Private Sub Cmdsearch1_Click()
Dim i, c, f As Integer
Dim a As String
c = List1.ListCountf = 0
Compiled By: Er. SRB
-
8/3/2019 VB chap 04
9/12
KHEC II/II Visual Programming (BEG275CO) - 9 -
For i = 0 To c - 1
a = Trim(Text1.Text)If (a = List1.List(i)) Then
Text2.Text = a
f = 1
End IfNext i
If (f = 0) ThenMsgBox "no matching"End If
End Sub
Private Sub Cmdadd_Click()
List1.AddItem Text3.Text
Text3.Text = ""Text3.SetFocus
End Sub
Private Sub cmdremove3_Click()
If (List1.ListIndex > 0) ThenList1.RemoveItem List1.ListIndex
Else
MsgBox "no item selected"End If
End Sub
Private Sub Cmdclear4_Click()List1.Clear
End Sub
COMBO BOXES:
The name combo box comes from combination box. The combo box control combines the features of atextbox control and a listbox control. Users can enter information in the textbox portion of the control or selectan item from the listbox portion. A combo box control can also be used to create a drop-down list box.
There are three types of combo boxes according to their style value.
i) Dropdown combo if style=0: it is similar to textbox with a list attached. Users can either type theirown value into the text box part or select an existing one from the list box part.
ii) Simple combo if style-=1: user can enter their own value in the text box.
iii) Drop down list if style=2: in this case list is not displayed until the user clicks the down arrow. Usercan only select from the list of option.
Properties:
List: arrays of items in the list box portion. Listcount : no. of items in the list.
Sorted: true/false
Style: 0,1 or 2
Text: text of most recently selected item.
Place a combobox on the form and code as
Private sub form_load()
Cbosubj.additem=VB Compiled By: Er. SRB
-
8/3/2019 VB chap 04
10/12
KHEC II/II Visual Programming (BEG275CO) - 10 -
Cbosubj.additem=TOC
Cbosubj.additem=CEnd sub
This example the various operations like adding an item, removing and clearing the combo box as shown
below.
Private Sub Command1_Click()
If Trim(Text1.Text) "" ThenCombo1.AddItem Text1.Text
Else
MsgBox "nothing is selected"
End IfText1.Text = ""
Text1.SetFocus
End Sub
Private Sub Command2_Click()
If Combo1.Text "" Then
Combo1.RemoveItem (Combo1.ListIndex)Else
MsgBox "no text to remove"End If
End Sub
Private Sub Command3_Click()
If Combo1.Text "" ThenCombo1.Clear
End If
End Sub
4.8 OPTION BUTTONS ANS CHECK BOXES:CHECK BOXES:
This control provides a way to make choices from a list of potential candidates. Some, all or none of the choices
in a group may be selected. The checkbox control can be turned on or off by the user. It is used for presenting a
single option or multiple options to the user of which any one or all can be selected. The checkbox can be in one
of the three states as on, off and grayed (dimmed). A grayed check is neither on nor off though the user canchange its setting.
Design the following interface and code accordingly:
Compiled By: Er. SRB
-
8/3/2019 VB chap 04
11/12
KHEC II/II Visual Programming (BEG275CO) - 11 -
Private sub form_load()
Txtname.text=visual programming
End sub
Private sub chkbold_click()
If chkbold.value=1 then
Txtname.fontbold=trueElse
Txtname.fontbold=false
End ifEnd sub
Private sub chkitallic_click()
If chkitallic.value=1 thenTxtname.fontitallic=true
Else
Txtname.fontitallic=falseEnd if
End sub
Private sub chksize_click()If chksize.value=1 then
Txtname.fontsize=12
ElseTxtname.fontsiz=8
End if
End subPrivate sub cmdext_click()
Unload me
End sub
OPTION / RADIO BUTTONS:
This control provides the capability to make a mutually exclusive choice among a group of potential candidatechoices i.e. it allows the user to select one and only one option from a group of options. Usually option buttons
are grouped together with a frame control.
Design the following interface and code accordingly to see the output.
Compiled By: Er. SRB
-
8/3/2019 VB chap 04
12/12
KHEC II/II Visual Programming (BEG275CO) - 12 -
Private sub option1_click()
Text1.text=version 5.0
End subPrivate sub option2_click()
Text1.text=version 6.0
End sub
4.9 GROUPING OF CONTROLS: FRAMES:
Frame provides a way of grouping related controls on a form and hence when the frame is moved the other
controls move too. This control appears as a box with a label at upper left.Properties:
Caption: the entered text in caption appears in the upper left corner of the frame.
Appearance: this property controls whether the border is shown as a single line, single color border whichgives the control a flat look or 3D effect.
Borderstyle: this property determines whether the border around the frame is displayed or not.
DIFFERENCE BETWEEN LABEL AND TEXTBOX
You can use both the controls to display the output, but the biggest difference is that you can use textboxto take inputs from the user whereas in label, you cant.
Eg; label1.caption=some text
Eg: text1.text=some text
DIFFERENCE BETWEEN LIST BOX AND COMBO BOX
List Box Combo Box
Occupies more space and shows more than one
value
We can select multiple items We can use check boxes within the list box
It does not contain text box
It is not classified as types.
Occupies less space and shows only one value
for visiblility.
Multiple select is not possible Cant use check boxes within the combo box
It is the combination of list box and text box
Combo box is of three types (1. Simple combo
2. Drop down 3. Drop down list
Compiled By: Er. SRB