0% found this document useful (0 votes)
51 views25 pages

Unit2-1 - Merged File

The document discusses key programming concepts in Visual Basic, including modules, data types, variables, operators, and procedures. It covers: 1) Code is organized into modules like form modules, standard modules, and class modules. Standard modules contain common code shared across forms. 2) Visual Basic supports various data types like numeric, string, date, and boolean. Variables must be declared with a data type like integer or string. 3) Variables are declared using Dim and can be public or private. Operators allow mathematical operations and comparisons. 4) Procedures like subs, functions, and properties allow breaking up code into reusable blocks. Subs and functions can take arguments. Functions return values

Uploaded by

abhinavsaha7d2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views25 pages

Unit2-1 - Merged File

The document discusses key programming concepts in Visual Basic, including modules, data types, variables, operators, and procedures. It covers: 1) Code is organized into modules like form modules, standard modules, and class modules. Standard modules contain common code shared across forms. 2) Visual Basic supports various data types like numeric, string, date, and boolean. Variables must be declared with a data type like integer or string. 3) Variables are declared using Dim and can be public or private. Operators allow mathematical operations and comparisons. 4) Procedures like subs, functions, and properties allow breaking up code into reusable blocks. Subs and functions can take arguments. Functions return values

Uploaded by

abhinavsaha7d2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

(Unit-2)

Declaring variables, data types, operators, and procedures


Visual Basic uses building blocks such as Variables, Data Types, Procedures, Functions and Control Structures in its programming environment.
This section concentrates on the programming fundamentals of Visual Basic with the blocks specified.

Modules
Code in Visual Basic is stored in the form of modules. The three kind of modules are Form Modules, Standard Modules and Class Modules. A
simple application may contain a single Form, and the code resides in that Form module itself. As the application grows, additional Forms are
added and there may be a common code to be executed in several Forms. To avoid the duplication of code, a separate module containing a
procedure is created that implements the common code. This is a standard Module.
Class module (.CLS filename extension) are the foundation of the object oriented programming in Visual Basic. New objects can be created by
writing code in class modules. Each module can contain:

Data types
By default Visual Basic variables are of variant data types. The variant data type can store numeric, date/time or string data. When a variable is
declared, a data type is supplied for it that determines the kind of data they can store. The fundamental data types in Visual Basic including
variant are integer, long, single, double, string, currency, byte and boolean. Visual Basic supports a vast array of data types. Each data type has
limits to the kind of information and the minimum and maximum values it can hold. In addition, some types can interchange with some other
types. A list of Visual Basic's simple data types are given below.
1. Numeric
Byte Store integer values in the range of 0 – 255
Integer Store integer values in the range of (-32,768) - (+ 32,767)
Long Store integer values in the range of (- 2,147,483,468) - (+ 2,147,483,468)
Single Store floating point value in the range of (-3.4x10-38) - (+ 3.4x1038)
Double Store large floating value which exceeding the single data type value
Currency store monetary values. It supports 4 digits to the right of decimal point and 15 digits to the left
2. String
Use to store alphanumeric values. A variable length string can store approximately 4 billion characters
3. Date
Use to store date and time values. A variable declared as date type can store both date and time values and it can store date values 01/01/0100
up to 12/31/9999
4. Boolean
Boolean data types hold either a true or false value. These are not stored as numeric values and cannot be used as such. Values are internally
stored as -1 (True) and 0 (False) and any non-zero value is considered as true.
5. Variant
Stores any type of data and is the default Visual Basic data type. In Visual Basic if we declare a variable without any data type by default the data
type is assigned as default.

Declaration of variables

Variable - the memory location name where you store your value into the memory is called variables. Variable may be declared
implicit and explicit mode as follows:-

Implicit declaration – no need to declare variable before using them is called implicit declaration.
Explicit declaration - requires the variable type declaration before using them. It is a good practice whose work on the project as
a fresher’s.

In the explicit declaration the “option explicit” statement must be written at the top line of the code window otherwise implicit will
be applicable automatically in the code window.

Declaring variables

Syntax

Dim variable name as <data type>

Dim is a keyword used to declare variable into visual basic.

Full form of Dim is “declare in memory”

Variables may be private or public it depends on scope of variable

For example

Dim operand1 as integer,operand2 as integer

Or

Dim operand1, operand2 as integer (integer type variable)

Dim result as single (floating type variable)

Dim temp as string (String type variable)

Or

Public op1 as integer,temp as single (it will be accessible into other module also)

User define data type

This is a name under which different types of variables may be declared. All variables are accessed by declaring that types of
variable and dot sign.
Type and end type keywords are used to declare the user define type data types.

For example

Private type product

Op1 as single
Op2 as single

End type

Dim table as product

Table.op1 = 10
Table.op2 = 10

Operators in Visual Basic

Arithmetical Operators

Operators Description Example Result

+ Add 5+5 10

- Substract 10-5 5

/ Divide 25/5 5

\ Integer Division 20\3 6

* Multiply 5*4 20

^ Exponent (power of) 3^3 27

Mod Remainder of division 20 Mod 6 2

& String concatenation "Teena"&" "&"Jain" "Teena Jain"

Relational Operators

Operators Description Example Result

> Greater than 10>8 True

< Less than 10<8 False

>= Greater than or equal to 20>=10 True

<= Less than or equal to 10<=20 True

<> Not Equal to 5<>4 True

= Equal to 5=7 False

Logical Operators

Operators Description

OR Operation will be true if either of the operands is true

AND Operation will be true only if both the operands are true

Visual Basic offers different types of procedures to execute small sections of coding in applications. Visual Basic programs can be broken into
smaller logical components called Procedures.

Sub Procedures
A sub procedure can be placed in standard, class and form modules. Each time the procedure is called, the statements between Sub and End
Sub are executed. The syntax for a sub procedure is as follows:

[Private Public] [Static] Sub Procedurename [( arglist)]

[ statements]

End Sub

arglist is a list of argument names separated by commas. Each argument acts like a variable in the procedure. There are two types of Sub
Procedures namely general procedures and event procedures.

Event Procedures

An event procedure is a procedure block that contains the control's actual name, an underscore(_), and the event name. The following syntax
represents the event procedure for a Form_Load event.
Private Sub Form_Load()

....statement block..

End Sub

Event Procedures acquire the declarations as Private by default.

General Procedures

A general procedure is declared when several event procedures perform the same actions. It is a good programming practice to write common
statements in a separate procedure (general procedure) and then call them in the event procedure.

We can also create a new procedure in the current module by typing Sub ProcedureName, Function ProcedureName, or Property
ProcedureName in the Code window. A Function procedure returns a value and a Sub Procedure does not return a value.

Function Procedures
Functions are like sub procedures, except they return a value to the calling procedure. They are especially useful for taking one or more pieces of
data, called arguments and performing some tasks with them. Then the functions returns a value that indicates the results of the tasks complete
within the function.
The following function procedure calculates the third side or hypotenuse of a right triangle, where A and B are the other two sides. It takes two
arguments A and B (of data type Double) and finally returns the results.

Function sum(A As Double, B As Double) As Double

sum = A + B

End Function

The above function procedure is written in the general declarations section of the Code window. A function can also be written by selecting
the Add Procedure dialog box from the Tools menu and by choosing the required scope and type.

Property Procedures
A property procedure is used to create and manipulate custom properties. It is used to create read only properties for Forms, Standard modules
and Class modules.Visual Basic provides three kind of property procedures-Property Let procedure that sets the value of a property, Property Get
procedure that returns the value of a property, and Property Set procedure that sets the references to an object.
Control structures

Control Statements are used to control the flow of program's execution. Visual Basic
supports control structures such as if... Then, if...Then ...Else, Select...Case, and Loop
structures such as Do While...Loop, While...Wend, For...Next etc method.

If...Then selection structure

The If...Then selection structure performs an indicated action only when the condition is
True; otherwise the action is skipped.

Syntax of the If...Then selection

If <condition> Then
statement
End If

e.g.: If average>75 Then


txtGrade.Text = "A"
End If

If...Then...Else selection structure

The If...Then...Else selection structure allows the programmer to specify that a different
action is to be performed when the condition is True than when the condition is False.

Syntax of the If...Then...Else selection

If <condition > Then


statements
Else
statements
End If

e.g.: If average>50 Then


txtGrade.Text = "Pass"
Else
txtGrade.Text = "Fail"
End If

Nested If...Then...Else selection structure

Nested If...Then...Else selection structures test for multiple cases by


placing If...Then...Else selection structures inside If...Then...Else structures.

Syntax of the Nested If...Then...Else selection structure

You can use Nested If either of the methods as shown above

Method 1

If < condition 1 > Then


statements
ElseIf < condition 2 > Then
statements
ElseIf < condition 3 > Then
statements
Else
Statements
End If

Method 2

If < condition 1 > Then


statements
Else
If < condition 2 > Then
statements
Else
If < condition 3 > Then
statements
Else
Statements
End If
End If
EndIf

e.g.: Assume you have to find the grade using nested if and display in a text box

If average > 75 Then


txtGrade.Text = "A"
ElseIf average > 65 Then
txtGrade.Text = "B"
ElseIf average > 55 Then
txtGrade.text = "C"
ElseIf average > 45 Then
txtGrade.Text = "S"
Else
txtGrade.Text = "F"
End If

Select...Case selection structure

Select...Case structure is an alternative to If...Then...ElseIf for selectively executing a single


block of statements from among multiple block of statements. Select...case is more
convenient to use than the If...Else...End If. The following program block illustrate the
working of Select...Case.

Syntax of the Select...Case selection structure

Select Case Index


Case 0
Statements
Case 1
Statements
End Select

e.g.: Assume you have to find the grade using select...case and display in the text box

Dim average as Integer


average = txtAverage.Text
Select Case average
Case 100 To 75
txtGrade.Text ="A"
Case 74 To 65
txtGrade.Text ="B"
Case 64 To 55
txtGrade.Text ="C"
Case 54 To 45
txtGrade.Text ="S"
Case 44 To 0
txtGrade.Text ="F"
Case Else
MsgBox "Invalid average marks"
End Select

A repetition structure allows the programmer to that an action is to be repeated until given
condition is true.

Do While... Loop Statement

The Do While...Loop is used to execute statements until a certain condition is met. The
following Do Loop counts from 1 to 100.

Dim number As Integer


number = 1
Do While number <= 100
number = number + 1
Loop

A variable number is initialized to 1 and then the Do While Loop starts. First, the condition is
tested; if condition is True, then the statements are executed. When it gets to the Loop it
goes back to the Do and tests condition again. If condition is False on the first pass, the
statements are never executed.

While... Wend Statement

A While...Wend statement behaves like the Do While...Loop statement. The


following While...Wend counts from 1 to 100

Dim number As Integer

number = 1
While number <=100
number = number + 1
Wend

Do...Loop While Statement

The Do...Loop While statement first executes the statements and then test the condition
after each execution. The following program block illustrates the structure:

Dim number As Long


number = 0
Do
number = number + 1
Loop While number < 201
The programs executes the statements between Do and Loop While structure in any case.
Then it determines whether the counter is less than 501. If so, the program again executes
the statements between Do and Loop While else exits the Loop.

Do Until...Loop Statement

Unlike the Do While...Loop and While...Wend repetition structures, the Do Until...


Loop structure tests a condition for falsity. Statements in the body of a Do Until...Loop are
executed repeatedly as long as the loop-continuation test evaluates to False.

An example for Do Until...Loop statement. The coding is typed inside the click event of the
command button

Dim number As Long


number=0
Do Until number > 1000
number = number + 1
Print number
Loop

Numbers between 1 to 1000 will be displayed on the form as soon as you click on the
command button.

The For...Next Loop

The For...Next Loop is another way to make loops in Visual Basic. For...Next repetition
structure handles all the details of counter-controlled repetition. The following loop counts
the numbers from 1 to 100:

Dim x As Integer
For x = 1 To 50
Print x
Next

In order to count the numbers from 1 yo 50 in steps of 2, the following loop can be used

For x = 1 To 50 Step 2
Print x
Next

The following loop counts numbers as 1, 3, 5, 7..etc

The above coding will display numbers vertically on the form. In order to display numbers
horizontally the following method can be used.

For x = 1 To 50
Print x & Space$ (2);
Next

To increase the space between the numbers increase the value inside the brackets after the
& Space$.

Following example is a For...Next repetition structure which is with the If condition used.

Dim number As Integer


For number = 1 To 10
If number = 4 Then
Print "This is number 4"
Else
Print number
End If
Next

In the output instead of number 4 you will get the "This is number 4".

A For...Next loop condition can be terminated by an Exit For statement. Consider the
following statement block.

Dim x As Integer
For x = 1 To 10
Print x
If x = 5 Then
Print "The program exited at x=5"
Exit For
End If
Next

The preceding code increments the value of x by 1 until it reaches the condition x = 5.
The Exit For statement is executed and it terminates the For...Next loop. The Following
statement block containing Do...While loop is terminated using Exit Do statement.

Dim x As Integer
Do While x < 10
Print x
x=x+1
If x = 5 Then
Print "The program is exited at x=5"
Exit Do
End If
Loop

With...End With statement

When properties are set for objects or methods are called, a lot of coding is included that
acts on the same object. It is easier to read the code by implementing the With...End
With statement. Multiple properties can be set and multiple methods can be called by using
the With...End With statement. The code is executed more quickly and efficiently as the
object is evaluated only once. The concept can be clearly understood with following
example.

With Text1
.Font.Size = 14
.Font.Bold = True
.ForeColor = vbRed
.Height = 230
.Text = "Hello World"
End With
Examples

Program to calculate the power a^b

Dim ANS As String


Private Sub Cmdpower_Click()
Dim a, b, p As Integer

a = Val(Text1.Text)
b = Val(Text2.Text)
p=1
While b >= 1
p=p*a
b=b-1
Wend
MsgBox "the value of p is = " & Str(p)
End Sub

Program to calculate the factorial value of given number

Private Sub Cmdfactorial_Click()


Dim x As Integer
Dim f As Integer
f=1
x = Val(Text4.Text)
Do While x >= 1
f=f*x
x=x-1
Loop
MsgBox "The factorial value is :- " & Str(f)
End Sub

Program to print the multiplication table as follows


2*1 = 2
2*2 = 4

Private Sub Cmdtable_Click()


Dim stnumber, endnumber, count As Integer
Dim product As Integer
stnumber = Val(Text3.Text)
endnumber = Val(Text5.Text)
count = 1
List1.Clear
Do
product = stnumber * count
List1.AddItem Str(stnumber) & "X" & Str(count) & "=" & Str(product)
count = count + 1
Loop While count <= endnumber
End Sub

Quiz program
Private Sub Option1_Click(Index As Integer)
Select Case Index
Case 0
ANS = "NARENDRA MODI"
Case 1
ANS = "WRONG ANSWER"
Case 2
ANS = "WRONG ANSWER"
Case 3
ANS = "WRONG ANSWER"
End Select
End Sub
Private Sub Text6_KeyPress(KeyAscii As Integer)
Dim count As Integer
count = 100
List2.Clear
If KeyAscii = 13 Then 'enter key ascii value
For start = Val(Text6.Text) To count
List2.AddItem start
Next start
End If
End Sub
Visual Basic offers different types of procedures to execute small sections of coding in
applications. Visual Basic programs can be broken into smaller logical components
called Procedures. Procedures are useful for condensing repeated operations such as
the frequently used calculations, text and control manipulation etc.

The benefits of using procedures in programming are:

It is easier to debug a program a program with procedures, which breaks a program into
discrete logical limits.

Procedures may be used at multiple point in the same program as well another
program.

A Procedure can be Sub, Function or Property Procedure.

Sub Procedures

A sub procedure can be placed in standard, class and form modules. Each time the
procedure is called, the statements between Sub and End Sub are executed. The
syntax for a sub procedure is as follows:

[Private|Public][Static] Sub Procedurename[( arglist)]

[ statements]

End Sub

arglist is a list of argument names separated by commas. Each argument acts like a
variable in the procedure. There are two types of Sub Procedures namely general
procedures and event procedures.

Event Procedures

An event procedure is a procedure block that contains the control's actual name, an
underscore(_), and the event name. The following syntax represents the event
procedure for a Form_Load event.

Private Sub Form_Load()


....statement block..
End Sub

Event Procedures acquire the declarations as Private by default.

General Procedures
A general procedure is declared when several event procedures perform the same
actions. It is a good programming practice to write common statements in a separate
procedure (general procedure) and then call them in the event procedure.

In order to add General procedure:

 The Code window is opened for the module to which the procedure is to be
added.
 The Add Procedure option is chosen from the Tools menu, which opens an Add
Procedure dialog box as shown in the figure given below.
 The name of the procedure is typed in the Name textbox
 Under Type, Sub is selected to create a Sub procedure, Function to create a
Function procedure or Property to create a Property procedure.
 Under Scope, Public is selected to create a procedure that can be invoked
outside the module, or Private to create a procedure that can be invoked only
from within the module.

We can also create a new procedure in the current module by typing Sub
ProcedureName, Function ProcedureName, or Property ProcedureName in the Code
window. A Function procedure returns a value and a Sub Procedure does not return a
value.

Function Procedures

Functions are like sub procedures, except they return a value to the calling procedure.
They are especially useful for taking one or more pieces of data, called arguments and
performing some tasks with them. Then the functions returns a value that indicates the
results of the tasks complete within the function.

The following function procedure finds the largest value where A and B are the two
arguments that accept the passing values inside the function. and finally returns the
results.
Syntax of function

[public]/[private]/[static] function functionname(argument list) as return type

<statements>

End function

For example

Private Function largest (A As Double, B As Double) As Double

If a>b then

Largest = a

Else

Largest = b

endif

End Function

The above function procedure is written in the general declarations section of the Code
window. A function can also be written by selecting the Add Procedure dialog box from
the Tools menu and by choosing the required scope and type.

Types of arguments

We have two types of arguments in visual basic used into the function which are as
follows

1. Byval type arguments – it is used when you have required to call the function
through call by value method. In this method the formal parameter into the
function definition just hold the valued passed into the called program. Byval type
arguments get copied value and return them as it is to the calling program.

2. Byref type arguments – it is used in the exchanging values through the function.
Byref type arguments get copied value with their reference or addresses and
returns the exchanged values to the calling program.
We have two types of methods to call the function with their arguments which are as
follows-

1. Call by value method: - it has the byval type arguments into the function. When
call it into the calling program and pass the actual parameter into function then
the formal parameters get copied and return value as it is to the calling program.

2. Call by reference method :- it has the byref type arguments into the function.
When call it into the calling program and pass the actual parameter into function
then the formal reference type parameters get copied the value with memory
address and return them as it is to the calling program. If you exchange any two
types of variable’s value then it becomes exchanged.

Example call by value method

Private function largest(byval a as integer,byval b as integer) as integer

If a>b then

L=a
Else
L=b
End if
Largest = l ‘returning statement

End function

Example call by reference method

Private function swap(byref a as integer,byref b as integer)

t=a
a=b
b=t

End function
Property Procedures

A property procedure is used to create and manipulate custom properties. It is used to


create read only properties for Forms, Standard modules and Class modules.Visual
Basic provides three kind of property procedures-Property Let procedure that sets the
value of a property, Property Get procedure that returns the value of a property, and
Property Set procedure that sets the references to an object.
Error Trapping
The process to find and caught the errors during the execution of program is
called error trapping.

Types of errors

Errors that occur in your program are of three types. They are syntax errors,
runtime errors and logical errors. Syntax errors are errors that occur because of
improperly written statements. Main causes for syntax errors are incorrectly
typed words, unmatched if statements etc. Among all, syntax errors are the
easiest to detect and rectify. Logical errors are errors in the logic of the program.
That means the entire program runs, but it doesn’t produce the results that are
required. Logical errors are very difficult to detect and remove. Sometimes days
may be passed but the errors may not be detected. So logical errors could cause
nightmares. The task of finding out the errors becomes difficult proportionate to
the size of the program.

Runtime errors are the errors that occur during the execution of the program and
cause program to be terminated abruptly. A few examples of runtime errors are
Division by Zero, File not found and Disk not ready. A good program should not be
terminated abruptly. So we have to take control when a runtime error occurs and
handle the errors .

Verify Error Handling Setting

Before you can use error handling, you need to understand the Error Trapping
setting. VB6/VBA lets you to determine how it should behave when errors are
encountered. From the IDE, look under the Tools Options setting.
Make sure error trapping is not set to "Break On All Errors". That setting will
cause your code to stop on every error, even errors you are properly handling with "On Error
Resume Next".

"Break on Unhandled Errors" works in most cases but is problematic while


debugging class modules. During development, if Error Trapping is set to "Break
on Unhandled Errors" and an error occurs in a class module, the debugger stops
on the line calling the class rather than the offending line in the class. This makes
finding and fixing the problem a real pain.

I recommend using "Break in Class Modules" which stops on the actual crashing
line. However, be aware that this does not work if you use raise errors in your
classes via the Err.Raise command. This command actually causes an "error" and
makes your program stop if Error Trapping is set to "Break in Class Modules".

How To Handle Runtime Errors

To handle runtime errors we have to use On Error statement. On Error statement


sends control to the given label whenever a runtime error occurs in any of the
statements given after On Error and before the end of the block (procedure or
function).
The following are the various formats available for On Error statement.

1. On Error Goto statement


2. On error resume next statement

When this statement is executed, any errors that occur in subsequent statements
cause Visual Basic to stop normal line-by-line execution and jump to the
statement labeled as "dbzeroerror".

In Visual Basic, line labels can include text if you want, but each label must be
unique. They are followed by a colon (:), as in the following example :

Example

Handling divides by zero error.

Option Explicit

Private Sub Command1_Click()

Dim x, y As Integer

x = Val(Text1.Text)

y = Val(InputBox("enter number to be divided"))

On Error GoTo handler

MsgBox x / y

Text1.SetFocus
Exit Sub

handler:

MsgBox "number can not be divided by zero"


Text1.SetFocus

MsgBox Err.Number & " " & Err.Description


End Sub
Private Sub Command1_Click()

Dim x, y As Integer

x = Val(Text1.Text)

y = Val(InputBox("enter number to be divided"))

On Error resume next

MsgBox x / y

Text1.SetFocus
Exit Sub
End Sub
Tree view control

A tree view control is used to display information within a tree hierarchy. A tree view is made up
of nodes that are related to each other in some ways. For example, family tree organizational
chart, or windows explorer.

A tree view control displays a hierarchical list of nodes objects m each of which consists of a
label and an optional bitmap. After creating a tree view control you can add remove arrange and
other wise manipulate node objects by the setting propeties and imvoking methods. You can
programmatically expand and collapse the node object to display or hide all child nodes. Three
events the collapse , expand and node check event , also provide programming functionally.

For using it , add it into toolbox window and draw it on the form window.

Adding new item


The items or nodes on a tree view control are members of the nodes collection. The following
expression is used to access the nodes collection , where treeview1 is the control’s name

Treeview1.nodes
Followings ate the methods and properties of the nodes collection.
Methods

1. add method : - to add a new node to the nodes collection


2. clear :- to remove all the nodes from the collection.
3. remove :- to remove a node from the nodes collections.

Properties
1. count :- returns the number of nodes in the nodes collections.
2. Item :- To retrieve a node specified by an index value or a key.

Option Explicit

Private Sub Command1_Click()


TreeView1.Nodes.Add , , "FRUIT", "Fruit"
TreeView1.Nodes.Add , , "COLOR", "Colors"
TreeView1.Nodes.Add , , "SHAPE", "Shapes"
TreeView1.LineStyle = tvwRootLines
End Sub

Private Sub Command2_Click()


TreeView1.Nodes.Add "FRUIT", tvwChild, , "Apples"
TreeView1.Nodes.Add "FRUIT", tvwChild, , "Grapes"
TreeView1.Nodes.Add "FRUIT", tvwChild, , "Pine Apples"
TreeView1.Nodes.Add "FRUIT", tvwChild, , "Cherries"
TreeView1.Nodes.Item("FRUIT").Expanded = True

End Sub

Private Sub Command3_Click()


TreeView1.Nodes.Add "COLOR", tvwChild, , "Red"
TreeView1.Nodes.Add "COLOR", tvwChild, , "Green"
TreeView1.Nodes.Add "COLOR", tvwChild, , "Yellow"
TreeView1.Nodes.Add "COLOR", tvwChild, , "Blue"
TreeView1.Nodes.Item("COLOR").Expanded = True
End Sub

Private Sub Command4_Click()


TreeView1.Nodes.Add "SHAPE", tvwChild, , "Triangle"
TreeView1.Nodes.Add "SHAPE", tvwChild, , "Rectangle"
TreeView1.Nodes.Add "SHAPE", tvwChild, , "Circle"
TreeView1.Nodes.Add "SHAPE", tvwChild, , "Oval"
TreeView1.Nodes.Item("SHAPE").Expanded = True
End Sub
The linestyle determines the appearance of the lines that connect nodes in the tree. Line style can
have two values. 0 (tvwtreelines) and 1(tvwrootlines). If line style is 0 then there will be lines
connecting parents to children and children to each other. If linestyle is 1 then there will also be
lines connecting the root nodes.

Nodes are not expanded bydefault, you can expand the item node from argument add the first
level items.

List view control

It is similar to the list box control. It displays its items in many forms along with any number of
sub items for each item. It is used to store all the subitems along with the key item.
The basic properties of the list view control can be set through the control’s property pages.
Place an instance of the listview control on a form, right click the control and select properties.
The property page is shown as below
Properties

View : - it determines how the items will be displayed on the control and it can have one of the
values like lvwreport, lvwicon, lvwlist etc.
Arrange : - it determines how the items are arranged on the control and it can have on of the
value like lvwautoleft and lvwautotop

Listitem : - the items of the listview control can be accessed through the listitems property
Count : it returns the number of items in the collection
Item : - it retrieves an item specified by an index value or key
Subitems : -each item in the listview control may also have subitems. It is other field of the
record.
Columnheader : - it is used to define the headers for the listview control in the report mode like
rollno, name, contact no..

Methods
Add : - it is used to add a new item to the listitems collection
Clear : - it removes all the items from the collection
Remove : - it removes an item from the listitems collection

Private Sub Command1_Click()


Dim mitem As ListItem
Set mitem = ListView1.ListItems.Add(, , "Lbits/bca3/2010-01")
mitem.SubItems(1) = "manoj kumar soni"
mitem.SubItems(2) = "SVN Nagar , Kota"
mitem.SubItems(3) = "9828586287"

Set mitem = ListView1.ListItems.Add(, , "Lbits/bca3/2010-02")


mitem.SubItems(1) = "Niraj saxena"
mitem.SubItems(2) = "Dadabari , Kota"
mitem.SubItems(3) = "9828583237"

End Sub

Private Sub Form_Load()


Dim lheader As ColumnHeader

lwidth = ListView1.Width - 5 * Screen.TwipsPerPixelX


Set lheader = ListView1.ColumnHeaders.Add(1, , "Rollno", lwidth / 4)
Set lheader = ListView1.ColumnHeaders.Add(2, , "Name.", lwidth / 4)
Set lheader = ListView1.ColumnHeaders.Add(3, , "Address.", lwidth / 4)
Set lheader = ListView1.ColumnHeaders.Add(4, , "Contact no.", lwidth / 4)

ListView1.View = lvwReport

End Sub

Tab strip control

It is also a part of Microsoft windows common control 6.0 that offer a better and more flexible
user interface. User can set up control’s properties from different portions like it’s properties
page with tab buttons. We can see as below

User can set up properties of tab strip control by the help of property page of tab strip control. A
tab strip control may consist of tab buttons along with properties pages like general, tabs, font,
picture tab buttons. You just create ditto above property page by the help of tab strip control .
You will use tab button property page for creating tab buttons in the tab strip control. It requires
a caption name for each tab button and an index number is allotted to each tab button during
giving caption name.

Pr to set up a text box control’s property by the help of tab strip control.
Option Explicit

Dim selectedtab As Integer

Private Sub Form_Load()


Dim i As Integer
For i = 1 To Frame1.UBound
Frame1(i).Move Frame1(0).Left, Frame1(0).Top, Frame1(0).Width, Frame1(0).Height
Frame1(i).Visible = False
Next i
selectedtab = 1
TabStrip1.SelectedItem = TabStrip1.Tabs(selectedtab)
Frame1(selectedtab - 1).Visible = True
End Sub
Private Sub Option1_Click(Index As Integer)
Select Case Option1(Index).Index
Case 0
Text1.FontName = "Arial"
Case 1
Text1.FontName = "Arial Black"
Case 2
Text1.FontName = "times new roman"
End Select

End Sub

Private Sub Option2_Click(Index As Integer)


Select Case Option2(Index).Index
Case 0
Text1.ForeColor = vbRed
Case 1
Text1.ForeColor = vbGreen
Case 2
Text1.ForeColor = vbBlue
End Select

End Sub

Private Sub TabStrip1_Click()


Frame1(selectedtab - 1).Visible = False
selectedtab = TabStrip1.SelectedItem.Index
Frame1(selectedtab - 1).Visible = True

End Sub

You might also like