Sunteți pe pagina 1din 15

To begin Visual Basic follow the sequence:

Start –Programs-MS Visual Studio-MS Visual Studio 6.0

Then the start window will be displayed. There are three buttons
New, Existing and Recent. At present we are interested to open a
project which already exists. We may click on existing or recent.

Recent: Vb keep track of recently used projects. We may select a


project simply by double clicking. To open a new file see design
window with three sub-windows. The left one is tool box window,
middle form window and last one is project window. If tool box
window or project window are not available then click on tool box
button.

1
There are three button in project explorer window view code, view
object (to see the form), toggle folders.

On a form we are to put controls. Each control has properties-


name properties is identification of control for further reference.
To get properties of the control select it and press F4.

We will have to change our properties as per our need. A label is a


control on which is used to display some on it. In the caption
properties nothing can be input from key board on it at run time.
But can be change at design time or programmatically at run
time.

Text Box Control: It’s a control on which data can be input at


run time. Data on it is its text property. When a form is loaded
blinking cursor will be positioned to the control. Having ability of
positioning cursor and minimum tab index value automatically
assigned by Vb. But we can change as per our need. If tab index
is pressed then blinking cursor will move on which cursor may
take positioned and has next tab index value.

Enable property: It determines whether blinking cursor will be


allowed to take positioned on the control. If it is a true value then
allow the same and false does not.

Important note: Any property of a control can be change


programmatically.

Event: When a control is used there may be several actions


automatically targeted by Vb, these are called event. Most event
come directly from user at the key board and mouse running
application within windows and some are system event when
windows recognize that the user. Triggered and event and the
event is not a system event. Windows passes that the event to
the application. User may check the various events triggered and
write code accordingly. If the system is not properly tackled by

2
routine (event procedure) then event goes unhandled. A few
events are clicked, double clicked, key press, key down etc.

Click Event: Click event of the control occurs when mouse is


clicked on the control. To open view code window with event
procedure related to particular control double click on it. By
default the space for one event procedure will be opened. If the
event procedure is not we want which is displayed. Then click on
the right drop down arrow and select event we want, from the list
of event procedure.

An Example: Suppose there are five labels we want that if a label


is clicked, its caption will be displayed on another label and
another text box. The form will look like the following:

All the controls have colors, front size 14 and bold. The code
window will look like the following:

Private Sub Label1_Click ()

Label6.Caption=Label1.Caption

Text Messege.Text=Label2.Caption
3
End Sub

Write the event procedure which will print Fahrenheit Degree to


Centigrade Degree on the labels when centigrade degree is
entered in the text box.

 Write the code for change event of the Text Box. The code
will be the following:

Private Sub Text Messege_Change()

If (Val (TextMessege, Text))>0 then

Label1.Caption (Val (TextMessege.Text)*9/5)+32

End If

End Sub

In change event the problem is the rational value is change. The


event procedure will run automatically. Sometimes we want that
if data entry is not over there will be no change in the desired
value then change will not be suitable. In this case we should take
an event to tell computer that my data typing is over.

Private Sub Text1_Key Down (key code as integer, shift


as integer)

If Key Code= VbKeyReturn

Command Button: Command button is a control that is


associated with a command the control is typically used to start
an event procedure that perform an action such as closing a form,
moving to a different record, printing a report to take an action on
some other control and so on. At run time when command button
is clicked, the command associated with the click event of the
button executed. Another way of choosing a command by
pressing the enter key on it. When its default property is set to
true then pressing enter key the action of click event of the

4
command will be executed. When command button is selected
and its cancel property is set to true. The command button can be
chosen by pressing ESC key.

To change the color of the command button first change its


style property to graphical then changes the color.

The use of command group control is to create a set of


command button so that the button on the group can be
manipulated individual or as a group.

The use of caption property to specify the text that appears


on the command button. The use of picture property is to specify
a picture of command button.

Access Key or Hot Key: It’s a key board key corresponding to


the underlined or highlighted letters in a menu or dialogue box or
caption of the control. Pressing the Alt+Access key to activate
the event procedure in other word we can say when an access
key is pressed while holding down the Alt key, it allows the user
to take some action such as open a menu, carry out a command
or move to an object etc. to define Hot Key place an ampersand
sign (&) before the letter to be used.

Form Load Statement: Load event occur when a form is loaded.


For startup form the event occur when an application is started as
a result of form load statement or as the result of the reference to
an unload form properties on control the event occur.

Syntax of Form Load Statement: when we use load event


procedure to include initialization code for a form. For example-
the code that specifies the default settings for control indicates
contents to be loaded into combo box on the list box controls and
initializes form label variable. The load event occurs after
initialization of the event.

Unload Statement: Unload statement unloads a form. The


syntax is unload object (form name) unload me statement
5
removes the active window. Thus ending the program and
returning the control to the operating system or cell (if the
program is compiled) or the VB development environment here
me refers the form that will be unloaded (active window).

Unloading a form may be necessary in some cases where


memory used is needed or something else closing else say for
ending the program or when it is needed to restart properties of
the control of the form to their original values, when form is
unloaded. All control placed on form, at run time are no longer
accessible. However at any run time change to those controls and
their properties are lost when the form is reloaded.

Note: When the form is unloaded only the display component is


unloaded. The code is associated with the form module remains in
memory.

Unload Event: Unload event occur when is about to be removed


the event is triggered by user closing the form using the
command button on the control menu or an unload statement the
syntax is Private Sub Form Name_Unload(Cancel as
integer)

Timer Control: It’s a control that can execute some code


associated with it at regular interval of time by causing a timer
event to occur the syntax of the timer event is Private Sub
Timer Control Name_Timer(). The timer control is invisible to
user and useful background processing when a timer control
elapses, VB generate generally timer event.

Example- Suppose we are to learn to print current time on the


caption of a form then we write timer event procedure for the
timer control as follows.

Private Sub Timer1_Timer()

Form Name. Caption= “Time Now”(blank)Time

6
We write time interval for the timer in form load event as
Timer1.Interval =1000

Control Array: A group of control that share common names,


types and events procedure. Each control has an unique index.
When a control in the array organizes an event it calls the events
procedure for the group and passes the index as an argument
allowing user code to determine which control recognizes the
event.

Advantage: Advantage of control array is that the same code for


event procedure is applicable for all the members of the array.

Example- Suppose you have five labels with name lbl and we
want right click event for the elements of the array. Such that
caption of some another label with name output will be same as
the label at which mouse was clicked. The code will be following.

Private Sub Lbl_clicked(index as integer)

Output.Caption=Lbl(index).Caption

End Sub

If-Then-Else Statement: Visual Basic procedure can’t test


conditions and then depending on the result of that test perform
different operations. The decision structure that VB supports
include If-Then, If-Then,……………, If-Then-Else and Select
Case.

If-Then: Use an If-Then structure is to execute one or more


statement conditionally. Statement can be used either in single
line syntax or multiple line block syntax.

If conditions Then

Statements satisfying the conditions

…………………………………………………………

7
…………………………………………………………

End If

The conditions are usually a comparison, but it can be any


expression that evaluates to a numeric value. VB interprets this
value true or false. A zero numeric value is false and any non zero
numeric value is considered. True if the condition is true. VB
executes all the statements following the keyword. You can use
either single line or multiple line syntax to execute just one
statement conditionally. These following examples are equivalent.

1. If I > 100 Then I=100

2. If Memory Data < Now Memory Data = Now

3. If I > 100 Then

I = 100

End If

Notice the single line form of If-Then statement does not use an
End If statement. If you want to execute more than one line of
code then condition is true. You must use multiple line block.

If-Then-End If syntax:

1. If i>100 Then

i=100

MsgBox “1 is greater than 100. So Mod 100”

2. If any date<now Then

Any date = now

Timer1.Enable=False ‘Disable Timer Control’

End If

8
3. If-Then-Else

Use an If-Then-Else block to define several blocks of statements,


on of which will execute.

If condition Then

[statement block_1]

Else If condition2 Then

[statement block_2]

Else

[statement block_n]

End If

Definition of Function: A function is a self contained program


segment that carries out some specific well define task. When a
function will carry out its intended action then it is accessed from
some other portion of the program generally.

Functi Syntax Description


ons
Cstr Cstr(express Returns an expression that has been
ion) converted to a variant or a string.

Expression may be any valid expression. The data in the


expression determines what return according to the following
table is if expression is Boolean then the string will return true or
false. If the expression is date then the string is short date format.

Null- A run time error

Empty- A zero length string (“ ”)

9
Error- A string containing the word error followed by the
error number

Other Numeric- A string containing the number

Example- Dim Str As String, Rate As Double

Str = “The price of the material”

Rate = “12.75”

MsgBox Str + “ ” + Cstr(Rate) Or_

MsgBox Str + “ ” + &Rate

Functi Syntax Description


ons
Len Len(string/v Returns a long integer of memory bytes
ar name) required to hold its argument. If the
argument is string then the function
returns a number of characters in the
string.

Note: If string is null then null is number, if the varname is variant


then it is treated as string.
Example- Dim A As String, B As Double, C As Long
A = “ABCD”

B = 1234567.897

C = 123

Then Len(A) will return 4, Len(B) will return 8, Len(C) will return 4.

Functi Syntax Description


ons
InStr InStr([start Returns a variant (long
position],string, integer) specifying position of
substring,[compare]) the first occurrence of one
substring.

10
Note: Items within third bracket are optional
Example- Dim Star As String
Star = “XXpXXpXXPXXP”
Here we want to search first occurrence of P**. If first position is
omitted it is assumed one.
We write-2>InStr(Star “p”)

Will return 9

Function Syntax Description


s
Left Left(String, Returns the left most substring of the
Function Length) length from the string

Example- Dim name as string


Name = “Minhajul Haque”
Then Left(name,8) ‘will return Minhajul only
The above can be done by another way

Dim I as integer

I = InStr(name, “ ”)-1

Now Str = Left(name,i) ‘ will return Minhajul only

Function Syntax Description


s
Right Right(string, Returns right most length
Function length) character of string

Example- Dim Name as string


Name = “ Minhajul Haque”
Then right(Name,5) ‘will return Haque only
The above thing can be written as follows.
I = Length(Name)-InStr(Name, “ ”)
Name = Right(Name, I)

Function Syntax Description


s
Trim Trim(string/varna Returns the string removing

11
Function me) leading and trailing blanks

Example- Name= “ABCD (blank)(blank)(blank) DEF”

Find first substring before blank second substring after blank.

Example- Dim Name as string. Full name may contain name,


middle name, and surname. But we are to separate them.

Example- File name contain Drive, directory path, file name and
extension like D:\abc\def\pqr.qrs

Functi Syntax Description


ons
Chr Chr(number) Returns a character
corresponding to a code value
Ex-Chr(65)=A
Asc Asc (string char) Returns the numeric equivalent
of a character. EX-Asc(“A)=65
Space Space(number) Returns a string consisting of
number of space.
Ex- Dim str as string
Str=space(4)+ “ABCD”
Will store 4 blanks….ABCD
String String(number, char) Returns a string consisting of a
single character repeated a
number of times.
Ex- MsgBox String(5, “x”)
Will return xxxxx
Str Str Comp (string1, Returns a value indicating the
Comp string2,[compare]) result of comparison of this
string.

Note: By Dim statement we can declare a string type of data. It


can store a string ranging from 0 to approximately 63K (char).
There are two types of string –

1. Variable length: It is declared simple as Dim variable name


as string.
12
2. Fixed length string: It is declared by specifying the length of
a string in the declaration.

Ex- Dim str as string *30

Declare a variable with name str = “ABCDE” then 25 space will


be padded after E.

Ex- Dim s1 as str *10, s2 as str *10

s1= “ABCD”

s2= “XYZ”

s3= s1&s2

Then s3 will contain ABCD (blank) (blank) (blank) (blank)


(blank) (blank)XYZ(blank) (blank) (blank) (blank) (blank) (blank)
(blank) and Len(s3) will result 20.

Another Example of Select Case: Select case allows for range


of choice “To”. The range determines which case body for doing
this. You may order the possibilities sequential. Suppose the
marks of a student stored in TextBox.

You can have several options to write some remarks on another


label as follows.

Select Case Val (Textres.Text)

Case 0 To 59

Lblres.Caption = “Back to”

Case 60 To 69

Lblres.Caption = “Get Help”

Case 70 To 79

Lblres.Caption = “Study harder”

13
Case 80 To 89

Lblres.Caption = “Great”

Case Else

Lblres.Caption = “Perfect”

Note: You can combine various forms of case expression into a


single select case statement like-

Case 101,102,201 to 205 is y300

If the statement means that if the expression in the select case


statement is 101, 102, 201 to 202, 203, 204, 208 or more than
300. The body of this case will be executed.

Ex- we want that only digit (-) minus ,(.)dot will be allowed to
enter in a text box in the key press event of the text box. We may
write-

Select Case Key ASCII:

Case 45 To 46 (for ‘-’ and ‘.’)

Case 48 To 57 (allow digits)

Case 8 (allow back space)

Case Else

Key ASC II = 0

Beep

End Select

Looping: Looping means repetition of codes to be executed until


a certain pre-determined condition it met VB supports loops
through a series of statement is called looping statement or body
of the loop. It a series of one or more statement that to be

14
executed until some conditions is satisfied or in other words we
can say the loop can be repeated a block of codes.

Do Loop: Do loop comes in several formats.

a. Do While Condition

b. Block of one or more statements

c. Loop

Ex- Dim I as integer, Sum as integer

I=1

Sum = 0

Do While I <= 10

Sum =Sum + I

I=I+1

Loop

It will calculate the first ten natural numbers, to display the sum
LblSum.Caption = Sum.

15

S-ar putea să vă placă și