0% found this document useful (0 votes)
3 views21 pages

Advance Visual Basic Unit - 1 (VT)

The document provides an overview of collections in Visual Basic 6.0, detailing their properties, methods, and how to manipulate items within them. It compares arrays and collections, explains how to create and manage forms, and outlines various methods for form manipulation and event handling. Additionally, it includes example code demonstrating the addition, counting, and removal of items in collections, as well as managing multiple forms at runtime.

Uploaded by

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

Advance Visual Basic Unit - 1 (VT)

The document provides an overview of collections in Visual Basic 6.0, detailing their properties, methods, and how to manipulate items within them. It compares arrays and collections, explains how to create and manage forms, and outlines various methods for form manipulation and event handling. Additionally, it includes example code demonstrating the addition, counting, and removal of items in collections, as well as managing multiple forms at runtime.

Uploaded by

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

Advance Visual Basic

unit -1

🧰 Properties and Methods of Collection (With Examples)


define collectrion explain the methods to access or manipulate items to
collection

Collections – VB6.0

🔹 What is a Collection?

 A container object to store and manage other objects.

 Example: A Form’s Controls collection holds all controls on that form.

🔹 Built-in Collections:

 Forms collection – All active forms.

 Controls collection – All controls on a form.

🔍 Accessing Items:

1. By Index – Like an array (starts from 1):


Form1.Controls(1)

2. By Key – Unique string name:


MyCollection("keyName")

✅ Key advantage: Doesn’t change if items are added/removed.

🆕 Declaring & Creating a Collection

 To use the collection, we have to first declare it.

🔹 Syntax:

Dim CollectionName As New Collection

🔹 Points:

 Dim → Declares the collection

 New → Creates a new collection

 CollectionName → Any name you choose

🔹 Example:

Dim employee As New Collection


🔄 Arrays vs Collections in VB6.0

Array Collection

Stores similar type items Stores similar or different types

Fixed size Dynamic size

Faster performance Slower than arrays

Access using index only Access using index or key

No built-in methods Has built-in methods

Dim arr(5) As Integer Dim col As New Collection

📌 Use Arrays When:


 ✅ Fixed size
 ✅ Need fast access
 ✅ Only one data type (e.g., all Integers)

📌 Use Collections When:


 ✅ Need dynamic size
 ✅ Use keys to access items
 ✅ Store different data types

⚙️Collection – Properties & Methods

Property / Method Description

Add Adds an item to the collection

Count (read-only)

Item Returns an item by index or key

Remove Deletes an item by index or key

➕ Adding Items to a Collection

 adds new item to the collection.

🔹 Syntax:

Collection.Add item [, key, before, after]


🔹 Meaning:

 Collection → your collection name

 Add → method to insert item

 item → item to add

 Key (optional) → unique identifier

 Before / After (optional) → set item position (use only one)

 🔹 1. Adding Item to a Collection


 Temperature.Add 78, "San Francisco"

Removing Items from a Collection

 delete an item from a collection.

🔹 Syntax:

CollectionName.Remove (index | Key)

🔹 2. Removing Item by Key

Temperature.Remove "Atlanta"

🔹 Removing Item by Index

Temperature.Remove 2

🔢 Count Property

 Returns the total number of items in a collection.

🔢 Counting a Collection in VB6


🔹 Syntax:
CollectionName.Count
🔹 Example:
n = Temperature.Count
🔹 Loop using Count:
For i = 1 To Temperature.Count
Print Temperature.Item(i)
Next i
Returning Items in a Collection (VB6)
Item Method

🔹 Syntax:
CollectionName.Item(index) ' Access by index
CollectionName.Item("key") ' Access by key
✅ Can also be written as:
CollectionName(index)
CollectionName("key")

🔹 Examples:
 Returning item using key:
T1 = Temperature("Atlanta")
' OR
T1 = Temperature.Item("Atlanta")
 Returning item using index:
T1 = Temperature(3)
' OR
T1 = Temperature.Item(3)

🔄 Processing a Collection

 For Each…Next loop repeats actions for each item in a collection

 Use: Unknown size


 No: Counter needed

🔹 Syntax:

For Each item In Collection

Process item

Next

For Each city In Temperature


Print city

Next

Program to Demonstrate Collection Methods

Dim Employee As New Collection

Dim p As String

Dim i As Integer

' Add items to the collection

Private Sub cmd_add_Click()

p = InputBox("Enter the employee name to add")

Employee.Add p

' Alternatively, you can add hardcoded names like this:

' Employee.Add "Reema"

' Employee.Add "Sonia"

' Employee.Add "Rahul"

End Sub

' Count the items in the collection

Private Sub cmd_count_Click()

For i = 1 To Employee.Count

Next

Print i - 1 ' Display the count of items in the collection

End Sub

' Remove an item from the collection by index

Private Sub cmd_remove_Click()

i = InputBox("Enter the index to remove")

Employee.Remove i

For i = 1 To Employee.Count

Print Employee.Item(i) ' Display remaining items after removal


Next

End Sub

' Display all items in the collection

Private Sub Command1_Click()

For i = 1 To Employee.Count

Print Employee.Item(i)

Next

End Sub

 cmd_add_Click: Add employee


 cmd_count_Click: Show count
 cmd_remove_Click: Remove by index, show rest
 Command1_Click: Show all employees

 ✅ cmd_add_Click – Adds entered name (e.g., Raj)


✅ cmd_count_Click – Shows count (e.g., 3)
✅ cmd_remove_Click – Removes by index, shows remaining (e.g., Raj, Aman)
✅ Command1_Click – Shows all names line by line (e.g., Raj, Simran, Aman)

Working with Forms in VB

 Title Bar: Shows caption and window buttons.


Control Menu Options:

 Restore, Move, Size, Minimize, Maximize, Close

Customization Properties (Very Short):

 Min/Max Button: False, Control Menu: False, Caption: Empty

BorderStyle Property in VB6:


0 - vbBSNone (No border),
1 - vbFixedSingle (Fixed single),
2 - vbSizable (Resizable),
3 - vbFixedDouble (Fixed dialog),
4 - vbFixedToolWindow (Fixed tool,
5 - vbSizableToolWindow (Resizable tool,

Common Form Properties: BackColor, BorderStyle, Caption, ControlBox, Font, ForeColor, Height,
Width, Left, Top, MaxButton, MinButton.

Creating, Adding & Removing Forms in VB

Adding Forms:

 From Project Menu: Go to Project > Add Form.


 From Project Explorer: Right-click in Project Explorer > Add > Choose form

Removing Forms:

 From Project Menu: Go to Project > Remove Form.

 From Project Explorer: Right-click on form > Remove Form.

# Adding Multiple Forms (Hiding & Showing Forms) – Short & Easy (form methods)

 Apps often use multiple forms to manage different tasks.

 Forms created at design-time; shown/hidden using Show/Hide.

What is a Method?

 Method: Built-in function that performs an action on an object.

Short and Easy Explanation of Methods

 Hide Method
Hides the form without unloading it. Form remains in memory.
Syntax: Form_Name.Hide

 SetFocus Method
Moves focus to a control
Syntax: ControlName.SetFocus

 Print Method
Prints text on an object
Syntax: ObjectName.Print

 Move Method
Moves an object to a new position .
Syntax: Object.Move Left, Top, Width, Height

 Cls Method
Clears content on a Form drawn using graphics methods
Syntax: Object.Cls

 Show Method
Displays a form on the screen. If not loaded, it loads and then shows it.
Syntax: FormName.Show [mode]

o mode:

 0 - Modeless (default)

 1 - Modal

Modal Forms:
 Blocks other actions.

 Requires user input before other actions.

 Example: MsgBox.

 Syntax: FormName.Show vbModal.

Modeless Forms:

 Allows interaction with other forms.

 Example: MDI child forms.

 Syntax: FormName.Show vbModeless.

Other Methods

 Refresh:
Redraws an object.
Syntax: FormName.Refresh

Setting the Start-up Form

1. Project → Properties → Select Form → OK

Managing Forms at Runtime

 Manipulating Forms:

 Create form using: Dim F As New Form1

o
o

o
Sequence of Form Events

1. Load, Activated, Paint, Deactivate, FormClosing, FormClosed

Load & Unload Statements

 Load:
Loads the form into memory but does not display it.
Syntax: Load FormName

 Unload:
Removes the form from memory
Syntax: Unload FormName

Differences

1. Load vs Show:

o Load: Loads the form into memory without displaying it, for faster reuse or
background use.

o Show: Displays a form on the screen. If not loaded, it loads and then shows it.

2. Unload vs Hide:

o Unload: Completely removes the form from memory and terminates its resources.

o Hide: Makes the form invisible.

Hide vs Unload:
Hide: Keeps data, can show again.
Unload: Data lost, must load again.

✅ Drag & Drop in VB

🔹 Steps:

1. Drag - Move
2. DragOver - Detect
3. Drop - Release

🔧 Properties:

 DragMode:

 vbManual - User-controlled
Automatic - VB-controlled
o DragIcon - Custom drag icon.
Automatic vs Manual Drag:

Feature Automatic Drag Manual Drag

Handling VB handles it Handled by code

Ease of Use Easy Needs coding

Mode vbAutomatic vbManual + Drag

Control Less control More control

🧩 Events:

 DragOver - Hover
DragDrop - Release

Method: object.Drag
Actions: vbBeginDrag, vbCancel, vbEndDrag

VB6 Drag & Drop (Very Short):


• Forms support drag.
• Label1: Manual, Label2: Auto.
• Code: Label1.Drag vbBeginDrag, Label2.Caption = Source.Caption
• Result: Drag → Label2 shows Label1 text.

Form_Load Event

 Occurs: When the form is loaded into memory.

 Purpose: Used for initialization tasks.

 Timing: Happens before the form is visually displayed and before data connections are
established.

Limitations:

 Printing to the form or setting focus to a control causes errors since the form and controls
are not fully loaded.

Solution:
Perform such actions in the Activate

Example:
Private Sub Form_Load()

Text1.SetFocus

End Sub

Activate & Deactivate Events

 Activate Event (Easy):


• Triggers when form gets focus
• Happens via user
• Form must be visible
 Deactivate Event:

o Occurs when the form loses focus to another form in the same application.

Syntax:

Private Sub object_Activate()

Private Sub object_Deactivate()

Activating a Form:
Click form, Show method, or SetFocus.

Activate/Deactivate Flow:
Activate → GotFocus, LostFocus → Deactivate.

Example:

Private Sub Form_Activate()

Text1.SetFocus

End Sub

Differences between Form_Load & Active Event

 Form_Load:

o not for setting the focus to controls or manipulating data-bound controls.

 Active Event:

o Fires after Form_Load and repeats.

o Better for setting the focus to controls or manipulating data-bound controls.

# Example Using Forms (Multiple Forms)


Form 1 Code

Private Sub Command1_Click()

Form2.Show

End Sub

Private Sub Command2_Click()

Form1.Hide

End Sub

Private Sub Command3_Click()

Dim n, c1 As Integer

Dim temp

n = InputBox("Enter The Number of elements you want to print on screen", "Input", "Enter Here")
c1 = 1

While (c1 <= n)

temp = InputBox("Enter The Element To Print", "Data")

c1 = c1 + 1

Print temp

Wend

End Sub

Private Sub Command4_Click()

Form1.Cls

End Sub

Private Sub Command5_Click()

Load Form2

End Sub

Private Sub Command6_Click()

Unload Form2

End Sub

Form 2 Code

Private Sub Command1_Click()

Form1.Show

End Sub

Private Sub Command2_Click()

Form2.Hide

End Sub

Private Sub Command3_Click()


Dim n, c1 As Integer

Dim temp

n = InputBox("Enter The Number of elements you want to print on screen", "Input", "Enter Here")

c1 = 1

While (c1 <= n)

temp = InputBox("Enter The Element To Print", "Data")

c1 = c1 + 1

Print temp

Wend

End Sub

Private Sub Command4_Click()

Form2.Cls

End Sub

Private Sub Command5_Click()

Load Form1

End Sub

Private Sub Command6_Click()

Unload Form1

End Sub

Here is your cleaned-up VB6 code (without comments), and yes, this
code is correct for:
✅ Q1: Create Forms at Runtime
✅ Q2: Add and Use Multiple Forms in VB6

✅ Form1 Code:
Private Sub Command1_Click()
Form2.Show
Unload Form1
End Sub

Private Sub Cmd_hide_Click()


Form1.Hide
End Sub

Private Sub Cmd_unload_Click()


Unload Me
End Sub

Private Sub Cmd_show_Click()


Form2.Show
End Sub

Private Sub Cmd_load_Click()


Load Form2
End Sub

Private Sub Cmd_move_Click()


Form1.Move 100, 400, 500, 1000
End Sub

✅ Form2 Code:
Private Sub Command1_Click()
Form1.Show
Unload Form2
Form2.Hide
End Sub

Private Sub Form_Activate()


Form2.Caption = "Form2 is active"
End Sub
Private Sub Form_Deactivate()
Form2.Caption = "Form2 is inactive"
End Sub

✅ The code meets both questions perfectly:


 It shows how to create, load, show, hide, unload, and move forms.
 It demonstrates interaction between multiple forms at runtime.
Explanation:
Form1:
1. Command1_Click: Shows Form2 and unloads Form1.
2. Cmd_hide_Click: Hides Form1 without unloading it (still keeps it in
memory).
3. Cmd_unload_Click: Unloads Form1, removing it from memory
completely.
4. Cmd_show_Click: Shows Form2 (if it is already loaded).
5. Cmd_load_Click: Loads Form2 into memory but doesn't show it.
6. Cmd_move_Click: Moves Form1 to a new position and resizes it.
Form2:
1. Command1_Click: Shows Form1 and unloads Form2.
2. form_activate: Triggers when Form2 becomes active and updates
the caption.
3. form_deactivate: Triggers when Form2 loses focus and updates
the caption.
Summary:
 This program demonstrates how to create forms dynamically,
show/hide forms, move forms, unload forms, and handle
activation and deactivation events for multiple forms in VB6.
 It provides a basic framework for managing forms at runtime and
allows for interaction between multiple forms.

Yes, this program can be applied to both questions. Let's break it down and explain which parts are
for each question.

Explanation:

1. Well-Documented Program to Create Forms at Run Time (1st Question)


This part demonstrates how to create and manage forms at runtime, which is a key concept in VB6.
In the code:

Code for Form1:

Private Sub Command1_Click()

Form2.Show ' Show Form2 when Command1 on Form1 is clicked

Unload Form1 ' Unload Form1 to close it

End Sub

 Form2.Show: This shows Form2 when the user clicks the button Command1 on Form1.

 Unload Form1: This unloads Form1 after displaying Form2, essentially closing it.

Private Sub Cmd_load_Click()

Load Form2 ' Load Form2 into memory without showing it

End Sub

 Load Form2: This loads Form2 at runtime but does not show it.

Code for Form2:

Private Sub Command1_Click()

Form1.Show ' Shows Form1 from Form2

Unload Form2 ' Unload Form2 after showing Form1

Form2.Hide ' Hide Form2, if needed

End Sub

 This part of the code allows interaction between the forms, creating and showing forms
dynamically at runtime.

2. Explanation of Code for Adding Multiple Forms in VB6 (2nd Question)

This part covers how multiple forms can be added and managed using VB6. It includes methods for
showing, hiding, unloading, and moving forms, and also managing events for multiple forms.

Code for Form1:

Private Sub Cmd_hide_Click()

Form1.Hide ' Hide Form1 without unloading it

End Sub

 Hide hides Form1, which means it's still in memory but not visible.

Private Sub Cmd_unload_Click()

Unload Me ' Unload the current form (Form1)


End Sub

 Unload Me: This unloads the current form, removing it from memory completely.

Private Sub Cmd_move_Click()

Form1.Move 100, 400, 500, 1000 ' Move Form1 to a new position and size

End Sub

 Move: This moves Form1 to a new position and changes its size.

Code for Form2:

Private Sub form_activate()

Form2.Caption = "Form2 is active" ' Change the caption when Form2 is activated

End Sub

Private Sub form_deactivate()

Form2.Caption = "Form2 is inactive" ' Change the caption when Form2 is deactivated

End Sub

 Form_Activate and Form_Deactivate: These events are triggered when Form2 becomes
active or loses focus.

Which Part is for Each Question?

First Question (Create Forms at Runtime):

 Form1 Code (Command1_Click, Cmd_load_Click): Demonstrates the creation and showing


of forms at runtime.

 Form2 Code (Command1_Click): Demonstrates the interaction between multiple forms


dynamically at runtime.

Second Question (Adding Multiple Forms):

 Form1 Code (Cmd_hide_Click, Cmd_unload_Click, Cmd_move_Click): Covers showing,


hiding, moving, and unloading forms.

 Form2 Code (form_activate, form_deactivate): Demonstrates handling activation and


deactivation events for multiple forms.

Conclusion:

 The program answers both questions effectively by explaining how to create forms
dynamically at runtime and how to manage multiple forms, including hiding, showing,
moving, and unloading forms.

You might also like