You are on page 1of 6

c 


  
     
    
          
                 
!  "#     $!    %  $ 

Taking different actions when a condition is true and when it is false

Decision Structures
If...Then...Else Construction
If...Then...Else constructions allow you to test for one or more conditions and run one or more statements
depending on each condition. You can test conditions and take actions in the following ways:

Ô un one or more statements if a condition is True


Ô un one or more statements if a condition is !alse
Ô un some statements if a condition is True and others if it is !alse
Ô =est an additional condition if a prior condition is !alse

=he control structure that offers all these possibilities is the Y 
     . You can use a
single-line version if you have just one test and one statement to run. If you have a more complex set of
conditions and actions, you can use the multiple-line version.

elect...Case Construction
=he elect...Case construction allows you to evaluate an expression once and run different sets of statements
based on different possible values. For more information, see
  
     .

Try...Catch...!inally Construction
Try...Catch...!inally constructions allow you to run a set of statements under an environment that retains
control if any of your statements causes an exception. You can take different actions for different exceptions.
You can optionally specify a block of code that is to run before you exit the entire Try...Catch...!inally
construction, no matter what happens. For more information, see   
     .
ëoop tructures
iisual Basic loop structures allow you to run one or more lines of code repetitively. You can repeat the
statements in a loop structure until a condition is True, until a condition is !alse, a specified number of times,
or once for each element in a collection.

=he following illustration shows a loop structure that runs a set of statements until a condition becomes true.

Running a set of statements until a condition becomes true

While ëoops
=he While...End While construction runs a set of statements as long as the condition specified in the While
statement is True. For more information, see Œ Œ
     .

ro ëoops
=he ro...ëoop construction allows you to test a condition at either the beginning or the end of a loop structure.
You can also specify whether to repeat the loop while the condition remains True or until it becomes True. For
more information, see r  
     .

!or ëoops
=he !or...Next construction performs the loop a set number of times. It uses a loop control variable, also called
a 2 , to keep track of the repetitions. You specify the starting and ending values for this counter, and you
can optionally specify the amount by which it increases from one repetition to the next. For more information,
see  
     .

!or Each ëoops


=he !or Each...Next construction runs a set of statements once for each element in a collection. You specify the
loop control variable, but you do not have to determine starting or ending values for it. For more information,
pommon pontrols
= =
= e text box is t e standard control for accepting input from t e user as well as to display t e
output. It can andle string (text) and numeric data but not images or pictures. String in a text
box can be converted to a numeric data by using t e function Val(text). = e following example
illustrates a simple program t at processes t e input from t e user.

=  

= e label is a very useful control for Visual Basic, as it is not only used to provide instructions
and guides to t e users, it can also be used to display outputs. One of its most important
properties is paption. Using t e syntax label.paption, it can display text and numeric data . You
can c ange its caption in t e properties window and also at runtime.

=    
= e command button is one of t e most important controls as it is used to execute commands.
It displays an illusion t at t e button is pressed w en t e user click on it. = e most common
event associated wit t e command button is t e plick event, and t e syntax for t e procedure
is
Private Sub pommand1_plick ()
Statements
End Sub

= 
= e Picture Box is one of t e controls t at is used to andle grap ics. You can load a picture at
design p ase by clicking on t e picture item in t e properties window and select t e picture
from t e selected folder. You can also load t e picture at runtime using t e LoadPicture
met od. For example, t e statement will load t e picture grape.gif into t e picture box.
Picture1.Picture=LoadPicture ("pVB programImagesgrape.gif")

=  
= e Image Box is anot er control t at andles images and pictures. It functions almost
identically to t e picture box. However, t ere is one major difference, t e image in an Image
Box is stretc able, w ic means it can be resized. = is feature is not available in t e Picture
Box. Similar to t e Picture Box, it can also use t e LoadPicture met od to load t e picture. For
example, t e statement loads t e picture grape.gif into t e image box.

= 
= e function of t e List Box is to present a list of items w ere t e user can click and select t e
items from t e list. In order to add items to t e list, we can use t e AddItem met od. For
example, if you wis to add a number of items to list box 1, you can key in t e following
statements
=   
= e function of t e pombo Box is also to present a list of items w ere t e user can click and
select t e items from t e list. However, t e user needs to click on t e small arrow ead on t e
rig t of t e combo box to see t e items w ic are presented in a drop-down list. In order to
add items to t e list, you can also use t e AddItem met od.

=  
= e p eck Box control lets t e user selects or unselects an option. W en t e p eck Box is
c ecked, its value is set to 1 and w en it is unc ecked, t e value is set to 0. You can include t e
statements p eck1.Value=1 to mark t e p eck Box and p eck1.Value=0 to unmark t e p eck
Box, as well as use t em to initiate certain actions. For example, t e program will c ange t e
background color of t e form to red w en t e c eck box is unc ecked and it will c ange to blue
w en t e c eck box is c ecked. You will learn about t e conditional statement If͙.= en͙.Elesif
in later lesson. VbRed and vbBlue are color constants and Backpolor is t e background color
property of t e form.

=  
= e Option Box control also lets t e user selects one of t e c oices. However, two or more
Option Boxes must work toget er because as one of t e Option Boxes is selected, t e ot er
Option Boxes will be unselected. In fact, only one Option Box can be selected at one time.
W en an option box is selected, its value is set to ͞=rue͟ and w en it is unselected; its value is
set to ͞False͟. In t e following example, t e s ape control is placed in t e form toget er wit
six Option Boxes. W en t e user clicks on different option boxes, different s apes will appear.
= e values of t e s ape control are 0, 1, and 2,3,4,5 w ic will make it appear as a rectangle, a
square, an oval s ape, a rounded rectangle and a rounded square respectively.

= 
= e Drive ListBox is for displaying a list of drives available in your computer. W en you place
t is control into t e form and run t e program, you will be able to select different drives from
your computer as s own in Figure 3.3

= 
= e Directory List Box is for displaying t e list of directories or folders in a selected drive. W en
you place t is control into t e form and run t e program, you will be able to select different
directories from a selected drive in your computer.

= 

= e File List Box is for displaying t e list of files in a selected directory or folder. W en you place
t is control into t e form and run t e program, you will be able to s own t e list of files in a
selected directory. You can coordinate t e Drive List Box, t e Directory List Box and t e File List
Box to searc for t e files you want. = e procedure will be discussed in later lessons.
 ! ! "#$$%$"&'
(()(*(+( 
, -
preating MDI (Multiple Document Interface) from ʹ parent and c ild form. Menu editor, menu
properties, creating t e pop-up menu, dynamic menus. preating a toolbar.

Îultiple-rocument Interface (ÎrI) Applications


=he multiple-document interface (MDI) allows you to create an application that maintains multiple forms within
a single container form. Applications such as Microsoft Excel and Microsoft Word for Windows have multiple-
document interfaces.

An MDI application allows the user to display multiple documents at the same time, with each document
displayed in its own window. Documents or 2

are contained in a 
 , which provides a
workspace for all the child windows in the application. For example, Microsoft Excel allows you to create and
display multiple-document windows of different types. Each individual window is confined to the area of the
Excel parent window. When you minimize Excel, all of the document windows are minimized as well; only the
parent window's icon appears in the task bar.

A child form is an ordinary form that has its MDIChild property set to =rue. Your application can include many
MDI child forms of similar or different types.

At run time, child forms are displayed within the  2 of the MDI parent form (the area inside the form's
borders and below the title and menu bars). When a child form is minimized, its icon appears within the
workspace of the MDI form instead of on the taskbar, as shown in Figure 6.4.

!igure 6.4 Child forms displayed within the workspace of the ÎrI form
Note Your application can also include standard, non-MDI forms that are not contained in the MDI form. A
typical use of a standard form in an MDI application is to display a modal dialog box.

An MDI form is similar to an ordinary form with one restriction. You can't place a control directly on a MDI form
unless that control has an Align property (such as a picture box control) or has no visible interface (such as a
timer control).

p 
 

Use t e following procedure to create an MDI form and its c ild forms.
= .


1. preate an MDI form.


From t e / menu, c oose 0. .

+An application can ave only one MDI form. If a project already as an MDI form, t e Add
DI Form command on t e Project menu is unavailable.

2. preate t e application's c ild forms.

=o create an MDI c ild form, create a new form (or open an existing one) and set its MDIp ild
property to =rue.

=o learn more about MDI applications, see t e following topics