Professional Documents
Culture Documents
Lecture 4+5
Lecture 4+5
Lecture 4+5
Form1.Backcolor=RGB(255,255,0) or Form1.Backcolor=VbYellow
Here are some of the common colors and the corresponding VBcolor
(Qbcolor (vbcode)) and RGB codes. You can always experiment with other
combinations, but remember the maximum number for each color is 255
and the minimum number is 0.
Example (1): The following is another program that allows the user to
enter the RGB codes into three different textboxes and when clicks the
display color button, the background color of the form will change
according to the RGB codes. So, this program allows users to change the
color properties of the form at run time.
Example (2): Design a form shown in figure below, with four commands
button and one label. Enter “Name” in label1 from property window. When
click on Command1 (Bold) or Command2 (Italic) or Command3
(Underline), the properties of label1 are changed. So when click on
Command4 (Normal) replace the default properties of text are entered in
Label1.
Solution:
Solution:
We can use named constants in place of integers for the second argument
to make the programs more readable. In fact, VB6 will automatically shows
up a list of named constants where you can select one of them.
TestMsg is a variable that holds values that are returned by the MsgBox ( )
function. The values are determined by the type of buttons being clicked
by the users. It has to be declared as Integer data type in the procedure or
in the general declaration section. Table below shows the values, the
corresponding named constant and buttons.
If TestMsg = 2 Then MsgBox "You have clicked the Cancel button": Exit Sub
End Sub
Note: The statement “Exit Sub” is defined to stop the program without close the form
window. While the statement “End” is stopped the program return to IDE window.
For example:
End Sub
5. Control Structures
In this chapter, you will learn how to write VB6 code that can make
decision when it process input from the users, and control the program
flow in the process. Decision making process is an important part of
• If …..Then
• If – Then –Else
• Select Case
This is the simplest control structure which ask the computer to perform a
certain action specified by the VB expression if the condition is true.
However, when the condition is false, no action will be performed. The
general format for the (If- Then) statement is
Example (1): Write a program to enter the value of two variables (X and
Y). Find and print the maximum value for two variables. Design form
window and select all the control objects are used.
Example (2): Used (If-Then Goto) condition to write a program for the
previous Example (1)
Solution(1): Solution(2):
Dim X ,Y , Max Dim X ,Y , Max
X =Val (Text1.Text ) X =Val (Text1.Text )
Y =Val (Text2.Text) Y =Val (Text2.Text)
Max=X If X> Y Then Max=X : Goto 10
If X> Y Then Text3.Text= Cstr (Max): Exit Sub Max=Y
Max=Y 10 Text3.Text= Cstr (Max)
Text3.Text= Cstr (Max) End Sub
End Sub
Note: The statement Exit Sub used to stop the program without return to
the project window.
VB Expression
End If
For example:
Dim X ,Y , Max
Max=X
If X< Y Then
Max=Y
EndIf
End Sub
If condition Then
VB expression
Else VB expression
End If
For example:
If X> Y Then
Max=X
Else
Max=Y
EndIf
End Sub
If there are more than two alternative choices, using just If – Then - Else
statement will not be enough. In order to provide more choices, we can
use If...Then...Else statement inside If...Then...Else structures. The general
format for the Nested If...Then.. Else statement is