VB One Two
VB One Two
REGISTER NO :
NAME :
CLASS :
1
NASC-B.SC(INFORMATION TECHNOLOGY)
Unit:1 INTRODUCTION TO VB 15 hours
Getting Started with VB6, Programming Environment, working with Forms, Developing
an application, Variables, Data types and Modules, procedures and control structures,
arrays. Working with Controls: Creating and using controls, working with control
arrays.
Unit:2 MENUS IN VB 15 hours
Menus, Mouse events and Dialog boxes: Mouse events, Dialog boxes, MDI and Flex grid:
MDI, Using the Flex grid control.
Text Book(s)
1 Visual Basic 6.0 Programming, Content Development Group, TMH, 8th
reprint, 2007. (Unit I to Unit IV)
2 Programming with Visual Basic 6.0, Mohammed Azam, Vikas Publishing
House, Fourth Reprint, 2006. (Unit V)
3
Reference Books
1 Gray Cornell (2003), ”Visual Basic 6 from ground up” TMH, New Delhi,
1st Edition,
Deitel and Deitel, T.R.Nieto (1998), “Visual Basic 6 - How to Program”,
2
Pearson Education. First Edition.
2
NASC-B.SC(INFORMATION TECHNOLOGY)
Unit:1 INTRODUCTION TO VB 15 hours
Once you install VB6, you'll be greeted with the Integrated Development
Environment (IDE), as shown below. This is where you design forms, write
code, and run your application.
3
NASC-B.SC(INFORMATION TECHNOLOGY)
#1 Visual Basic Programming Environment:
2. Code Editor:
3. Toolbox:
4
NASC-B.SC(INFORMATION TECHNOLOGY)
4. Form Designer:
5. Solution Explorer:
6. Properties Window:
5
NASC-B.SC(INFORMATION TECHNOLOGY)
7. Error List / Output Window:
8. Debugger:
10.Compiler:
1. Open Visual Studio and create a new Windows Forms App (.NET
Framework).
6
NASC-B.SC(INFORMATION TECHNOLOGY)
4. Run the program using the Start button.
#2 DEVELOPING AN APPLICATION:
4. Name your project (e.g., MyFirstVBApp), choose the location, and click
Create.
• Drag and drop controls (buttons, labels, textboxes, etc.) onto the form.
Example Layout:
7
NASC-B.SC(INFORMATION TECHNOLOGY)
4. Write the Code (Event Handling)
Double-click the button to open the code editor. Then write your Visual
Basic code inside the button’s click event:
userName = txtName.Text
End Sub
In this code:
8
NASC-B.SC(INFORMATION TECHNOLOGY)
• Test the button to see the greeting based on input.
• When you're done, go to Build > Build Solution to compile the app.
Step Description
9
NASC-B.SC(INFORMATION TECHNOLOGY)
Step Description
In VB6, forms are the main building blocks for creating the user interface
(UI) of your application. They are essentially windows that users interact
with. When you create a new project in VB6, the default form (Form1) is
automatically created for you.
• Select Add Form. This will allow you to add additional forms to your
project (e.g., Form2, Form3, etc.).
2. Form Properties
Forms in VB6 come with various properties that you can configure to
customize the appearance and behavior of the form. These properties are
accessed through the Properties window.
• Name:
o The Name property is the identifier of the form in your code. For
example, if the Name of the form is Form1, you can refer to it in
the code as Form1.
• Caption:
o The Caption property sets the text that appears in the title bar
of the form when it is running. This is often used to display the
name of the application or the current state of the window.
o The Icon property allows you to set the small image that
appears in the upper-left corner of the window (next to the form
caption).
o You can assign your own icon (e.g., .ico files) for a personalized
appearance.
• BackColor:
• MDIChild:
Once your form is created, you’ll often need to add various controls to
interact with the user. These controls are usually added via the Toolbox
window in VB6. The Toolbox contains a variety of commonly used controls
such as:
• TextBox:
11
NASC-B.SC(INFORMATION TECHNOLOGY)
• Label:
o You could use labels to describe what a user needs to do, such
as "Enter your name" or "Age (in years)".
• CommandButton:
• ComboBox:
o You can populate the ComboBox with a list of items via its
AddItem method or through the List property.
Once you’ve added controls to your form, you can write code to handle user
actions or system events (such as button clicks, form loads, or text
changes). VB6 uses a simple event-driven model, meaning that actions like
button clicks or text changes trigger specific code that you write.
End Sub
To write code that should run when the form is first loaded, you use the
Form_Load event:
End Sub
Event Description
13
NASC-B.SC(INFORMATION TECHNOLOGY)
Event Description
Example:
End Sub
#4 VARIABLES
Variables in VB6 are used to store and manipulate data during the
execution of a program. They are essential for performing calculations,
storing user inputs, tracking states, etc.
1. Declaring Variables
Syntax:
Example:
14
NASC-B.SC(INFORMATION TECHNOLOGY)
SCOPE OF VARIABLES
1. Local Variables
Definition:
A local variable is declared inside a procedure or function and can only be
accessed within that procedure. Once the procedure finishes execution, the
local variable goes out of scope and is destroyed.
Example:
x=5
End Sub
2. Module-Level Variables
Definition:
A module-level variable is declared outside any specific procedure, typically
at the top of a form or module, using the Private keyword. It is accessible to
all procedures within the same form or module but not outside it.
Example:
total = 5
End Sub
15
NASC-B.SC(INFORMATION TECHNOLOGY)
MsgBox total ' Displaying updated value
End Sub
3. Global Variables
Definition:
A global variable is declared using the Public keyword in a standard module
(not in a form or class). It can be accessed from any part of the project,
including different forms or modules.
Example:
' In Form1
End Sub
' In Form2
End Sub
• Lifetime: The global variable exists for the duration of the entire
program and retains its value between form loads and other events.
16
NASC-B.SC(INFORMATION TECHNOLOGY)
4. Constants
Definition:
A constant is a variable-like value that cannot be changed after it is defined.
Constants are typically used for values that remain the same throughout the
program, such as mathematical values (like Pi) or application-specific
settings (like a version number).
Syntax:
Examples:
radius = 5
End Sub
In this example, PI is a constant, so its value (3.14159) will remain the same
throughout the program and cannot be changed.
17
NASC-B.SC(INFORMATION TECHNOLOGY)
5. Variable Visibility in Different Types of Modules
1. Form-Level (Module-Level):
Variables declared at the top of a form are accessible only within that
form (e.g., Private). They cannot be accessed by other forms unless
passed explicitly.
Example:
Key Points:
In VB6, data types define what kind of data a variable can hold. The choice
of data type has a significant impact on the memory usage, performance,
and accuracy of your program. The correct data type ensures that your
program works efficiently, avoids errors, and helps prevent unexpected
behavior due to type mismatches.
1. Memory Efficiency:
Different data types require different amounts of memory. For
example, an Integer only uses 2 bytes, while a Double takes 8 bytes.
18
NASC-B.SC(INFORMATION TECHNOLOGY)
By choosing the appropriate data type, you can minimize the memory
usage of your program.
2. Type Safety:
VB6 will catch errors like trying to store a letter in a variable that's
meant to hold numbers. This can help prevent bugs early in
development by enforcing constraints on what can be assigned to a
variable.
3. Performance:
Choosing the right data type allows the program to perform optimally.
If you use a larger data type when a smaller one will suffice, it can
waste memory and slow down performance.
Data
Description Example
Type
High-precision decimal
Decimal Dim price As Decimal = 19.99D
numbers
19
NASC-B.SC(INFORMATION TECHNOLOGY)
#6 MODULES in VB
In Visual Basic, a module serves as a container for code that can be shared
across different parts of the application. It is a file with a .bas extension (for
standard modules) that contains global variables, constants, procedures, and
functions. Modules can be used for logic that doesn't depend on the specific
form or user interface elements.
Key Features:
• Constants: Constants that are used across the entire application can
be defined here.
Example:
Module MyModule
' Constant
20
NASC-B.SC(INFORMATION TECHNOLOGY)
' Procedure (Sub)
Sub ShowMessage()
End Sub
' Function
Counter = Counter + 1
Return Counter
End Function
End Module
A Class Module defines a custom object, with its own properties, methods,
and events. You can think of it as defining your own type of object with
certain behavior and state. Class modules are used when you need to model
real-world entities or need encapsulation for data and behavior.
21
NASC-B.SC(INFORMATION TECHNOLOGY)
Key Features:
• Properties: Variables that belong to the object and represent its state.
• Events: Allows the class to notify other parts of the application when
certain actions occur.
#7 PROCEDURES in VB
In VB6 (Visual Basic 6.0), procedures are blocks of code that perform
specific tasks, and they help to modularize and organize your program.
Procedures can be called to execute their instructions from anywhere in
your code.
1. Sub Procedures
A Sub Procedure (or simply a Sub) is a block of code that performs a specific
task but does not return a value. It is used to execute operations, like
22
NASC-B.SC(INFORMATION TECHNOLOGY)
displaying a message, updating a variable, or manipulating a control,
without providing a result to the caller.
Sub SubProcedureName()
…..
End Sub
MsgBox message
End Sub
2. General Procedures
23
NASC-B.SC(INFORMATION TECHNOLOGY)
or a Function, and the term "general" simply refers to it being a reusable
piece of code that can be placed in Standard Modules or Class Modules.
A general procedure can be used for general tasks that are not tied to any
specific form or control.
Sub GeneralSub()
End Sub
GeneralFunction = 10
End Function
3. Function Procedures
24
NASC-B.SC(INFORMATION TECHNOLOGY)
Syntax for Function Procedure:
Function FunctionName() As
DataType
…….
End Function
End Function
You can call a function and assign its return value to a variable.
result = AddNumbers(5, 3)
The AddNumbers function adds two numbers and returns the result, which
is then displayed in a message box.
4. Property Procedures
Property Procedures are used in Class Modules to define how you can get or
set the value of an object’s property. Property procedures are a special type
of function and subroutine that enable you to encapsulate the access to an
object’s data.
#8 CONTROL STRUCTURES in VB
1.1. If...Then...Else
Syntax:
If condition Then
Else
End If
Example:
26
NASC-B.SC(INFORMATION TECHNOLOGY)
age = 18
Else
End If
If you don’t need an Else part, you can use a simplified If...Then statement.
Syntax:
If condition Then
End If
Example:
number = 5
End If
1.3. ElseIf
You can use ElseIf to check multiple conditions within the same If block.
Syntax:
27
NASC-B.SC(INFORMATION TECHNOLOGY)
If condition1 Then
Else
Example:
score = 85
Else
MsgBox "Fail"
End If
Here, the program checks for multiple conditions and provides a grade
based on the score.
2. Select Case
Syntax:
28
NASC-B.SC(INFORMATION TECHNOLOGY)
Select Case expression
Case value1
Case value2
Case Else
End Select
Example:
day = 3
Case 1
MsgBox "Monday"
Case 2
MsgBox "Tuesday"
Case 3
MsgBox "Wednesday"
Case 4
MsgBox "Thursday"
Case Else
End Select
In this example, Select Case checks the value of day and displays the
corresponding message for the day of the week.
3. Looping Structures
29
NASC-B.SC(INFORMATION TECHNOLOGY)
Loops are used to execute a block of code repeatedly as long as a certain
condition is met.
The For...Next loop is used when you know in advance how many times you
want to repeat a block of code.
Syntax:
Next counter
Example:
For i = 1 To 5
Next i
In this example, the loop runs 5 times and displays a message box with the
current iteration number.
Syntax:
Next item
Example:
names(0) = "John"
names(1) = "Jane"
30
NASC-B.SC(INFORMATION TECHNOLOGY)
names(2) = "Paul"
MsgBox name
Next name
Here, the loop iterates over the names array and displays each name in a
message box.
3.3. Do...Loop
The Do...Loop allows you to repeat code while a condition is true or until a
condition becomes true. You can control the loop with the While or Until
condition.
Syntax:
Do
Example:
counter = 1
Do
counter = counter + 1
31
NASC-B.SC(INFORMATION TECHNOLOGY)
ARRAYS
In VB6, arrays are used to store multiple values of the same type in a
single variable. Arrays are particularly useful when you need to work with a
list of similar data types, such as a list of numbers or strings, and you want
to access them by index.
1. Fixed size Arrays: These have a fixed size, which is defined at the time
of declaration and cannot be changed.
2. Dynamic Arrays: These can change size during runtime using the
ReDim statement.
A static array has a predefined size, which is set at the time of declaration
and cannot be changed during execution.
Syntax:
Where:
• DataType is the type of data the array will hold (e.g., Integer, String,
etc.).
Example:
numbers(0) = 10
32
NASC-B.SC(INFORMATION TECHNOLOGY)
numbers(1) = 20
numbers(2) = 30
numbers(3) = 40
numbers(4) = 50
In this example:
2.Dynamic Arrays
A dynamic array does not have a fixed size at the time of declaration. You
can use the ReDim statement to define the array's size later, during
runtime. Additionally, you can change the size of the array using ReDim
whenever necessary.
ReDim arrayName(newSize)
Example:
numbers(0) = 10
numbers(1) = 20
numbers(2) = 30
numbers(3) = 40
33
NASC-B.SC(INFORMATION TECHNOLOGY)
numbers(4) = 50
You can also use the Preserve keyword to retain the values of the array
when resizing it:
You can access individual elements of an array using the index (starting
from 0). The general syntax is:
SYNTAX
arrayName(index)
numbers(0) = 10
numbers(1) = 20
numbers(2) = 30
numbers(3) = 40
numbers(4) = 50
3.Multidimensional Arrays
34
NASC-B.SC(INFORMATION TECHNOLOGY)
A 2D array is essentially an array of arrays. You can think of it as a table
with rows and columns.
Syntax:
Example:
matrix(0, 0) = 1
matrix(0, 1) = 2
matrix(0, 2) = 3
matrix(0, 3) = 4
matrix(1, 0) = 5
matrix(1, 1) = 6
matrix(1, 2) = 7
matrix(1, 3) = 8
matrix(2, 0) = 9
matrix(2, 1) = 10
matrix(2, 2) = 11
matrix(2, 3) = 12
35
NASC-B.SC(INFORMATION TECHNOLOGY)
How to Create and Use Controls in Visual Basic
2. Set Properties
• Visual Studio will open the Code Editor and create an event
procedure.
Design:
Add:
Code:
36
NASC-B.SC(INFORMATION TECHNOLOGY)
Private Sub btnGreet_Click(sender As Object, e As EventArgs) Handles
btnGreet.Click
End Sub
When the button is clicked, it reads text from txtName and shows a greeting
in lblGreeting.
Control Purpose
Label Control?
Key Properties
37
NASC-B.SC(INFORMATION TECHNOLOGY)
Property Description
TextBox control:
Key Properties
• Text: The current text displayed; you can get or set it in code or via
the Properties window .
38
NASC-B.SC(INFORMATION TECHNOLOGY)
• PasswordChar (Char): Masks entered text, e.g.,
TextBox1.PasswordChar = "*",.
Button Control?
Property Description
39
NASC-B.SC(INFORMATION TECHNOLOGY)
Control Array (VB6 Style)
A Control Array in Visual Basic 6 (VB6) is a set of controls of the same type
(e.g., multiple TextBox, CommandButton, Label, etc.) that:
This makes your code more efficient and maintainable, especially when
handling groups of similar controls.
1. At Design Time
4. Click Yes.
• Command1(0)
• Command1(1)
A runtime control array means that additional controls are added to the
array while the program is running, instead of placing all of them on the
form at design time.
To save design time when the number of controls needed isn't known
in advance
1. At least one control of the desired type (e.g., TextBox) must be added
to the form at design time.
40
NASC-B.SC(INFORMATION TECHNOLOGY)
The Load Statement
Load Text1(1)
• By default, loaded controls are not visible — you must set .Visible =
True.
Load Text1(1)
Text1(1).Visible = True
Text1(1).Left = Text1(0).Left
This code:
• Makes it visible
Event Handling
All controls in a control array share the same event procedures, with the
Index used to differentiate them.
Example:
End Sub
41
NASC-B.SC(INFORMATION TECHNOLOGY)
EXPECTED QUESTIONS:
1 marks
1. Which of the following is the default extension for a Visual Basic 6 project
file?
A) .vbp
B) .exe
C) .vb6
D) .vbg
Answer: A) .vbp
2. Which of the following is used to declare a variable in VB6?
A) Var
B) Dim
C) Declare
D) Set
Answer: B) Dim
3. What type of control is used to display a message to the user in VB6?
A) TextBox
B) Label
C) Button
D) ComboBox
Answer: B) Label
4. Which of the following data types is used to store decimal numbers in
VB6?
A) Integer
B) Single
C) String
D) Boolean
Answer: B) Single
5. Which control property allows you to change the text displayed on a
button in VB6?
A) Name
B) Text
C) Caption
D) Font
Answer: C) Caption
6. What is the purpose of an Option Explicit statement in VB6?
A) It declares all variables.
B) It forces the use of error handling.
C) It specifies the default data type.
D) It disables the use of MsgBox.
Answer: A) It declares all variables.
5 MARKS
42
NASC-B.SC(INFORMATION TECHNOLOGY)
3. Write a short note on control arrays. How are they created and
accessed?
8 MARKS
2. Write a VB6 program that takes input from the user through a form
and calculates the factorial of a number using a loop.
4. Explain how arrays are declared and used in VB6. Write a program to
find the largest number in an array of integers entered by the user.
43
NASC-B.SC(INFORMATION TECHNOLOGY)
Unit:2 MENUS IN VB 15
hours
#1 Menus,#2 Mouse events and #3 Dialog boxes: Mouse events, Dialog boxes, MDI
and Flex grid: #4 MDI, #5 Using the Flex grid control.
INTRODUCTION:
#1 Menu:
In VB6, menus are part of the main menu bar that is typically located at the
top of a form. You can use Menu Editor to easily design and manage menus
in your application.
The Menu Editor in VB6 is a tool that allows you to add, arrange, and define
actions for various menus and menu items without needing to manually
code the interface. Here’s a step-by-step guide to creating menus using the
Menu Editor:
1. Launch Visual Basic 6.0 and open your project (or create a new
project).
2. On the Form Designer window, go to the Tools menu at the top of the
screen.
44
NASC-B.SC(INFORMATION TECHNOLOGY)
Step 2: Define Your Main Menu (Caption and Name)
• In the Menu Editor, you’ll need to define main menus first. These are
the primary categories that appear on the top menu bar (e.g., File,
Edit, Help).
1. Caption: This is the text that will appear on the menu bar.
2. Name: This is the identifier used in the code. This name allows you to
reference the menu programmatically, i.e., when writing the code that
responds to user actions like clicks on the menu items.
For example, the name for the File menu might be mnuFile.
Each main menu can have multiple submenus (also known as menu items)
that represent the actions a user can take. For example, the File menu can
contain items like New, Open, and Exit.
1. In the Menu Editor, click on the main menu item (e.g., File) to select
it.
2. To add a submenu item, click on the Insert button (or press the Insert
key), which will allow you to define an item under the selected menu.
45
NASC-B.SC(INFORMATION TECHNOLOGY)
3. You’ll need to specify the caption (what will be displayed on the menu
item) and the name (the identifier that will be used in the code).
• For example:
Example:
File
├── New
├── Open
└── Exit
Here, "File" is the main menu, and "New", "Open", and "Exit" are the
submenu items.
Access keys :
Access key allows you to open a menu by pressing the alt key and then the
designated letter. Access key also allow the user to access menu command
once the menu is open by continuing to hold the alt key and pressing its
designated letter.
46
NASC-B.SC(INFORMATION TECHNOLOGY)
Shortcuts:
#2 MOUSE EVENTS:
MouseClick
Usage: It's commonly used when you want to perform an action when the
user clicks on a control like a button or a label.
47
NASC-B.SC(INFORMATION TECHNOLOGY)
Example:
MessageBox.Show("Button clicked at coordinates: " & e.X & ", " & e.Y)
End Sub
MouseDown
Usage: You can use this event to detect when the user starts interacting
with a control (e.g., starting a drag operation).
Example:
End If
End Sub
MouseUp
Description: This event occurs when a mouse button is released after being
pressed.
Example:
End Sub
MouseMove
Usage: It’s commonly used for real-time tracking of the mouse or updating
the UI dynamically based on the pointer's position.
48
NASC-B.SC(INFORMATION TECHNOLOGY)
Example:
Label1.Text = "Mouse Position: " & e.X & ", " & e.Y
End Sub
MouseEnter
Description: This event occurs when the mouse pointer enters the bounds
of a control.
Usage: This is useful for changing the appearance of a control when the
user hovers over it (e.g., highlighting a button when the user hovers over it).
Example:
Button1.BackColor = Color.AliceBlue
End Sub
MouseLeave
Description: This event occurs when the mouse pointer leaves the bounds
of a control.
Usage: It's commonly used to reset the control's appearance when the
mouse leaves the control (e.g., unhighlighting a button).
Example:
Button1.BackColor = Color.White
End Sub
MouseHover
Usage: It's commonly used for showing tooltips or initiating custom actions
when a user pauses over a control.
49
NASC-B.SC(INFORMATION TECHNOLOGY)
Example:
End Sub
MouseWheel
Description: This event is triggered when the scroll wheel on the mouse is
moved (up or down).
Example:
Else
End If
End Sub
MouseEventArgs Class
• Clicks: Returns the number of times the mouse button has been
clicked (useful for double-click detection).
50
NASC-B.SC(INFORMATION TECHNOLOGY)
• Delta: The amount the mouse wheel has moved. The value is positive
when the wheel is scrolled up and negative when it is scrolled down.
3. Write Code Inside the Event Handler: Define the functionality you
want to happen when the event occurs. This can include any
interaction with the form, updating the UI, or invoking custom logic
based on mouse position or actions.
#3 DIALOG BOX
Dialog boxes are part of the Graphical User Interface (GUI) in VB.NET and
are used for interactive communication between the program and the
user.
1. MessageBox
51
NASC-B.SC(INFORMATION TECHNOLOGY)
2. Common Dialog Boxes
52
NASC-B.SC(INFORMATION TECHNOLOGY)
3. Custom Dialog Box
Parameters:
53
NASC-B.SC(INFORMATION TECHNOLOGY)
• Caption – Title of the box
OpenFileDialog – Example
End If
# 4 MDI forms
• All child forms are confined within the parent window and cannot
move outside of it.
54
NASC-B.SC(INFORMATION TECHNOLOGY)
Types of Forms in MDI Applications
o These are regular forms, but with the MDIChild property set to
True.
Steps:
3. Show the child form from the MDI form using code like:
Load frmChild
frmChild.Show
End Sub
Property/Method Description
55
NASC-B.SC(INFORMATION TECHNOLOGY)
Property/Method Description
Example:
MDIForm1.Arrange vbCascade
# 5 FlexGrid control:
To use it:
Project → Components → Microsoft FlexGrid Control 6.0 (SP6)
Property Description
56
NASC-B.SC(INFORMATION TECHNOLOGY)
Property Description
With MSFlexGrid1
.Rows = 5
.Cols = 3
.TextMatrix(0, 0) = "ID"
.TextMatrix(0, 1) = "Name"
.TextMatrix(0, 2) = "Age"
.TextMatrix(1, 0) = "101"
.TextMatrix(1, 1) = "Alice"
.TextMatrix(1, 2) = "25"
.TextMatrix(2, 0) = "102"
.TextMatrix(2, 1) = "Bob"
.TextMatrix(2, 2) = "30"
End With
End Sub
57
NASC-B.SC(INFORMATION TECHNOLOGY)
OUTPUT:
4. Common Uses
QUESTIONS
1 marks
58
NASC-B.SC(INFORMATION TECHNOLOGY)
Which property of the FlexGrid control is used to set the number of rows in
the grid?
A) RowCount
B) ColumnCount
C) Row
D) Cells
Answer: A) RowCount
Which dialog box is used to allow the user to select a file in VB6?
A) ColorDialog
B) FileOpenDialog
C) FileDialog
D) CommonDialog
Answer: C) FileDialog
Which event is triggered when the mouse pointer is moved over a control in
VB6?
A) MouseClick
B) MouseMove
C) MouseEnter
D) MouseDown
Answer: B) MouseMove
59
NASC-B.SC(INFORMATION TECHNOLOGY)
D) Form
Answer: D) Form
Which property of the MDI form defines whether a form can be maximized?
A) Maximized
B) WindowState
C) FormStyle
D) AutoResize
Answer: B) WindowState
5 marks:
3. What is an MDI form? How does it differ from a standard form in VB6?
5. How are menus created in VB6? Write steps and an example to create
a simple menu.
1 .Describe the process of creating a menu in VB6 using the Menu Editor.
Write a program that implements a menu with submenus and shortcut
keys.
60
NASC-B.SC(INFORMATION TECHNOLOGY)