Visual Objects

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 58

Visual objects

• The objects have associated properties,


methods and events.
• A property is a named attribute of a
programming object. Properties define the
characteristics of an object such as Size, Color
etc. or sometimes the way in which it behaves.
For example, a TextBox accepts properties
such as Enabled, Font, MultiLine, Text, Visible,
Width, etc.
Textbox properties(txt)
• Enabled property allows the TextBox to be enabled or disabled
at run time depending on the condition set to True or False.
• Font property sets a particular font in the TextBox.
• MultiLine property allows the TextBox to accept and display
multiple lines at run time.
• Text property of the TextBox control sets a particular text in
the control.
• Visible property is used to hide the object at run time.
• Width property sets the TextBox to the desired width at
design time.
• MaxLength -Limits the length of displayed text (0 value
indicates unlimited length).
• PasswordChar -Hides text with a single character.
• 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.


Method
• A method is an action that can be performed
on objects.
• a method is a connected or built-in procedure,
a block of code that can be invoked to impart
some action on a particular object
Textbox methods
• The TextBox control has other associated
methods such as Refresh, SetFocus, etc.
• The Refresh method enforces a complete
repaint of the control or a Form. For example,
Text1.Refresh refreshes the TextBox.
• The Setfocus method moves the focus on the
control. For Example Text1.SetFocus sets the
focus to TextBox control Text1.
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
Textbox events
• The TextBox control supports various events
such as Change, Click, MouseMove and many
more that will be listed in the Properties
dropdown list in the code window for the
Textbox control.
Textbox events
• The code entered in the Change event fires
when there is a change in the contents of the
Textbox
• The Click event fires when the TextBox control
is clicked.
• The MouseMove event fires when the mouse
is moved over the TextBox
Form(frm)
• Visual Basic Form is the container for all the
controls that make up the user interface.
Every window you see in a running visual basic
application is a form, thus the terms form and
window describe the same entity.
Form properties
• Appearance:- selects 3-D or flat appearance

• Back color: sets the form background color

• Border style: Sets the form border to be fixed


or sizeable

• Caption: sets the form window title


Form methods
• A method is just a block of code that you can
call
• Cls: clears all graphics and text from forms but
does not clear any objects
• Print: prints text string on the form
• Show: Used to display a form.
• Hide Method Used to hide a form without
closing it (the form is still loaded in memory).
• Unload Method Completely unloads the form
from memory. After this, the form cannot be
displayed until it is reloaded.
• Load Method Loads a form into memory
without displaying it.
• Move Method Moves a form to a new location
on the screen.
Form events
• Activate: is triggered when the form becomes
the active window
• Click: Triggered when the user clicks on the
form
• DBLCLICK: Triggered when users double clicks
on the form
• Load: occurs when form is loaded. It helps the
users to initialize variables and set any runtime
properties
Listbox (lst)
• ListBox and ComboBox controls present a set
of choices that are displayed vertically in a
column.
• If the number of items exceed the value that
be displayed, scroll bars will automatically
appear on the control. These scroll bars can be
scrolled up and down or left to right through
the list.
List box properties
• Enabled By setting this property to True or
False user can decide whether user can interact
with this control or not
• Index Specifies the Control array index
• List String array. Contains the strings displayed
in the drop-down list. Starting array index is 0.
• List Count Integer. Contains the number of
drop-down list items
List box properties
• Style Integer. Specifies the style of the ComboBox's
appearance
• Tab Stop Boolean. Specifies whether ComboBox
receives the focus or not.
• Text String. Specifies the selected item in the
ComboBox
• Tooltip Index String. Specifies what text is displayed
as the ComboBox's tool tip
• Visible Boolean. Specifies whether ComboBox is
visible or not at run time
Listbox Methods
• AddItem Add an item to the ComboBox
• Clear Removes all items from the ComboBox
• RemoveItem Removes the specified item from
the ComboBox
• SetFocus Transfers focus to the ComboBox
List box properties
• ListIndex Integer. Contains the index of the selected
ComboBox item. If an item is not selected, ListIndex is -1
• Locked Boolean. Specifies whether user can type or not in
the ComboBox
• MousePointer Integer. Specifies the shape of the mouse
pointer when over the area of the ComboBox
• NewIndex Integer. Index of the last item added to the
ComboBox. If the ComboBox does not contain any items ,
NewIndex is -1
• Sorted Boolean. Specifies whether the ComboBox's items
are sorted or not.
List box event procedures
• Change Called when text in ComboBox is
changed
• DropDown Called when the ComboBox drop-
down list is displayed
• GotFocus Called when ComboBox receives the
focus
• LostFocus Called when ComboBox loses it
focus
Adding items to a List

a) 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
b) 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.
• Private Sub Form_Load()
• List1.AddItem "Kenya"
• List1.AddItem "Uganda"
• List1.AddItem "Rwanda"
• List1.AddItem "TZ"

• List1.AddItem 3

• Label1.Caption = List1.ListCount
• End Sub
example
• Private Sub Form_Load()
• list1.AddItem 1
list1.AddItem 2
list1.AddItem 3
list1.AddItem 4
list1.AddItem 5
list1.AddItem 6
• End Sub
Adding items to a listbox using textbox
• Private Sub Command1_Click()
• List1.AddItem Text1.Text
• Text1.Text = ""
• End Sub
Working with listbox

This program will add one item at a time


as the user enters an item into the TextBox
and click the Add button. In this program, you
insert a TextBox and a ListBox into the
Form. The function of the TextBox is to let the
user enter an item one at a time and
add it to the Listbox. The method to add an item
to the ListBox is Add. The output
interface is shown in Figure below.
Removing items from listbox
• Private Sub Command2_Click()
• If List1.ListIndex > -1 Then
• List1.RemoveItem List1.ListIndex
• End If
• End Sub
Clearing the list
• Private Sub Command3_Click()
• List1.Clear
• End Sub
• Example2: Write a program in VB including
one Textbox, one Listbox and one
Command, showing that when inputting three
colors names from Textbox to the
Listbox and select any color name in the
Listbox then the Form background color will
change.
Command for adding items into the list
• Private Sub Command1_Click()
List1.AddItem Text1.Text
Text1.Text = ""
End Sub
Selecting items from the list
• Private Sub List1_Click()
• If List1.Text = "red" Then
• Form1.BackColor = vbRed
• End If
• If List1.Text = "blue" Then
• Form1.BackColor = vbBlue
• End If
Listbox
exercise
Check box(chk)
• 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 Option Button.
• When you place a CheckBox control on a form,
all you have to do, usually, is set its Caption
property to a descriptive string.
• If you want to display the control in a checked
state, you set its Value property to 1-Checked
right in the Properties window, and you set a
grayed state with 2-Grayed.
Value property
• Value property of checkbox indicates whether
checkbox is currently checked or unchecked.
The valid values are; 0 is Unchecked (default),
1 is Checked and 2 is Grayed (dimmed).
• Private Sub cmdShow_Click()
• If Check1.Value = 1 Then
• Print "Checked !“
• ElseIf Check1.Value = 0
• Then Print "Unchecked !"
• End If
• End Sub
Program with multiple checkboxes
• Private Sub Command1_Click()
• If Check1.Value = 1 Then
• Form1.BackColor = vbBlue
• End If
• If Check2.Value = 1 Then
• Form1.BackColor = vbYellow
• End If
• If Check3.Value = 1 Then
• Form1.BackColor = vbBlue
• End If
• If Check4.Value = 1 Then
• Print "You are safe"
• End If
Option button(opt)
• Option Button controls are also known as
radio buttons because of their shape.
• Options buttons are used to allow user to
select one from a set of mutually exclusive
options. You can have more than one set of
option button but in that case each set of
option button should be placed inside a
separate Frame control.
• Anytime you click on a button in the group, it
switches to a selected state and all the other
controls in the group become unselected.
• You set an Option Button control's Caption
property to a meaningful string, and if you want
you can change its Alignment property to make
the control right aligned.
• If the control is the one in its group that's in the
selected state, you also set its Value property to
True. (The OptionButton's Value property is a
Boolean value because only two states are
possible.) Value is the default property for this
contro
• A group of Option Button controls is often
hosted in a Frame control. This is necessary
when there are other groups of Option Button
controls on the form.
• As far as Visual Basic is concerned, all the
Option Button controls on a form's surface
belong to the same group of mutually
exclusive selections, even if the controls are
placed at the opposite corners of the window.
• Private Sub optRed_Click()
• Form1.BackColor = vbRed
• End Sub

• Private Sub optGreen_Click()
• Form1.BackColor = vbGreen
• End Sub

• Private Sub optBlue_Click()
• Form1.BackColor = vbBlue
• End Sub

Using a command button
• Private Sub Command1_Click()

• If Option1.Value = True Then

• Form1.BackColor = vbRed

• ElseIf Option2.Value = True Then

• Form1.BackColor = vbGreen

• Else

• Form1.BackColor = vbBlue

• End If


• Private Sub cmdCheck_Click()
• If optWindows.Value = True Then
• Print "Windows is selected"
• ElseIf optLinux.Value = True Then
• Print "Linux is selected"
• ElseIf optMac.Value = True Then
• Print "Mac is selected"
• End If
• End Sub
Frame control (frm)
• Frame is a container control as the Frame control
is used as a container of other controls.
• The controls that are on the frame or contained
in the frame are said to be the child controls.
Use the Caption property to set the frame's title.
• when you move the frame, all the child controls
also go with it. If you make the frame disabled or
invisible, all the child controls also become
disabled or invisible.
Shape control (shp)
• It can display six basic shapes: Rectangle,
Square, Oval, Circle, Rounded Rectangle, and
Rounded Square. 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).
Shape cont’
• to draw a shape, just click on the shape control
and draw the shape on the form. The default
shape is a rectangle, with the default shape
property set at 0. You can change the shape to
square, oval, circle and rounded rectangle by
changing the shape property's value to 1, 2, 3 ,
4, and 5 respectively.
• Private Sub Form_Load()
• Shape1.Shape = 3
• End Sub
• Private Sub Form_Load()
• List1.AddItem "Rectangle"
• List1.AddItem "Square“
• List1.AddItem "Oval“
• List1.AddItem "Circle"
• List1.AddItem "Rounded Rectangle"
List1.AddItem "Rounded Square“
• End Sub
• Private Sub List1_Click()
• Select Case List1.ListIndex
• Case 0
• Shape1.Shape = 0
• Case 1
• Shape1.Shape = 1
• Case 2
• Shape1.Shape = 2
• Case 3
• Shape1.Shape = 3
• Case 4
• Shape1.Shape = 4
• Case 5
• Shape1.Shape = 5
• End Select
• End Sub
The Label

• The label is a very useful control for Visual


Basic, as it is not only used to provide
instructions and guides to the users, it can
also be used to display outputs.
• You can change its caption in the properties
window and also at runtime.

• Label1.caption=“jane”
The Command Button
• The command button is one of the most
important controls as it is used to execute
commands. It displays an illusion that the
button is pressed when the user click on it.
• The most common event associated with the
command button is the Click event, and the
syntax for the procedure is
• Private Sub Command1_Click ()
• Statements
• End Sub
A Simple Password Cracker
• Private Sub cmd_ShowPass_Click()
Dim yourpassword As String
yourpassword = Txt_Password.Text
MsgBox ("Your password is: " & yourpassword)
End Sub
The PictureBox

• The Picture Box is one of the controls that is


used to handle graphics.
• You can load a picture at design phase by
clicking on the picture item in the properties
window and select the picture from the
selected folder.
• You can also load the picture at runtime using
the LoadPicture method.
• Picture1.Picture=LoadPicture ("C:\VBprogram\
Images\grape.gif")

You might also like