You are on page 1of 30

Visual Basics …

[Lecture 4 ]

Bhumika Shah
{Lecturer, Computer Engineering Department }
Command Button…
 A 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.
 In Visual Basic terms, clicking a command

button creates an event, which must be


processed in your program.
 Few commonly found command buttons…
1. OK :: Accepts a list of options and indicates that
the user is ready to proceed.
2. Cancel :: Discards a list of options.
3. Quit :: Exits an open dialog box or program
Command Button Properties…
 Appearance :: Selects 3-D or flat appearance.
 Cancel :: Allows selection of button with Esc key (only
one button on a form can have this property True).
 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.

 Command Button Events:

 Click Event :: triggered when button is selected by


clicking on it.
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.
Sample application ::
A sample Application for ….
[1] calculating average of two numbers…
[2] clearing all fields…
[3] to exit application …
DATA INPUT CONTROLS….
 Option Button ::

 To receive user input in a Visual Basic program, you


can use controls that:
1) Present a range of acceptable choices.
2) Receive input in an acceptable format.

 The Option Button control satisfies these


requirements.
 It displays a list of choices on a form and
 requires the user to pick one (but only one) item from
a list of choices.
 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. (To do this, use Frame, a Visual Basic
toolbox control.)
 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.
Option buttons, grouped by a frame ::
CODE ….
Private Sub cmdenter_Click()
If Option1.Value = True Then
lblres = Option1.Caption
End If
If Option2.Value = True Then
lblres = Option2.Caption
End If
End Sub
Checkbox….
 The Checkbox control is similar to the OptionButton
control, except that Checkbox displays a list of
choices and gives the user the option to pick
multiple items (or none at all) from a list of choices.

 create a frame for the buttons with Frame, a toolbox


control.
 Then, you can place your check boxes inside the
frame so that they can be processed as a group in
your program code or moved as a group along with
the frame.
ListBox…
 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.)
Setting List Box Properties….
 In a Visual Basic program, you can define list box
characteristics in the Properties window or by using
program code, so that users can add, remove, or
sort list box items while the program runs..

 You add choices to a list box with the AddItem


method, which is typically placed in the Form_Load
event procedure.
sample code….
Private Sub Form_Load()
List1.AddItem “Extra hard disk”
List1.AddItem “Printer”
List1.AddItem “Satellite dish”
End Sub
Sample application ….
ComboBox…
Private Sub Form_Load()
Combo1.AddItem “U.S. Dollars”
Combo1.AddItem “Check”
Combo1.AddItem “English Pounds”

End Sub
Event Driven Programming
Conditional Expressions…
 Comparison Operators ::
 a simple conditional expression:

Price < 100


1) If the Price variable contains a value that is less than 100, the
expression evaluates to True.
2) If Price contains a value that is greater than or equal to 100, it
evaluates to False.
 Comparison Operator :: Meaning
 = Equal to
 < > Not equal to
 > Greater than
 < Less than
 > = Greater than or equal to
 < = Less than or equal to
 Conditional Expression Result
1) 10 < > 20 :: True (10 is not equal to 20)
2) Score < 20 ::True if Score is less than 20;
otherwise, False
3) Score = Label1.Caption :: True if the
Caption property of the Label1 object
contains the same value as the Score
variable; otherwise, False
4) Text1.Text = “Bill” ::True if the word Bill is
in the first text box; otherwise, False
If – Then Decision Structures…
 When you use conditional expressions in a
special block of statements known as a
decision structure, you can control the order
in which statements are executed.
 With an If...Then decision structure, your

program can evaluate a condition in the


program and take a course of action based on
the result.
Single-condition Structures..
 In its simplest form, an If...Then decision structure
is written on a single line:
If condition Then statement
where:
• condition is a conditional expression.
• statement is a valid Visual Basic program

statement.
 For example,

If Score >35 Then


Label1.Caption = “Pass “
End if
 Good Day…

You might also like