Advance Visual Basic Unit - 1 (VT)
Advance Visual Basic Unit - 1 (VT)
unit -1
Collections – VB6.0
🔹 What is a Collection?
🔹 Built-in Collections:
🔍 Accessing Items:
🔹 Syntax:
🔹 Points:
🔹 Example:
Array Collection
Count (read-only)
🔹 Syntax:
🔹 Syntax:
Temperature.Remove "Atlanta"
Temperature.Remove 2
🔢 Count Property
🔹 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
🔹 Syntax:
Process item
Next
Next
Dim p As String
Dim i As Integer
Employee.Add p
End Sub
For i = 1 To Employee.Count
Next
End Sub
Employee.Remove i
For i = 1 To Employee.Count
End Sub
For i = 1 To Employee.Count
Print Employee.Item(i)
Next
End Sub
Common Form Properties: BackColor, BorderStyle, Caption, ControlBox, Font, ForeColor, Height,
Width, Left, Top, MaxButton, MinButton.
Adding Forms:
Removing Forms:
# Adding Multiple Forms (Hiding & Showing Forms) – Short & Easy (form methods)
What is a Method?
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.
Example: MsgBox.
Modeless Forms:
Other Methods
Refresh:
Redraws an object.
Syntax: FormName.Refresh
Manipulating Forms:
o
o
o
Sequence of Form Events
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.
Hide vs Unload:
Hide: Keeps data, can show again.
Unload: Data lost, must load again.
🔹 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:
🧩 Events:
DragOver - Hover
DragDrop - Release
Method: object.Drag
Actions: vbBeginDrag, vbCancel, vbEndDrag
Form_Load Event
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
o Occurs when the form loses focus to another form in the same application.
Syntax:
Activating a Form:
Click form, Show method, or SetFocus.
Activate/Deactivate Flow:
Activate → GotFocus, LostFocus → Deactivate.
Example:
Text1.SetFocus
End Sub
Form_Load:
Active Event:
Form2.Show
End Sub
Form1.Hide
End Sub
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
c1 = c1 + 1
Print temp
Wend
End Sub
Form1.Cls
End Sub
Load Form2
End Sub
Unload Form2
End Sub
Form 2 Code
Form1.Show
End Sub
Form2.Hide
End Sub
Dim temp
n = InputBox("Enter The Number of elements you want to print on screen", "Input", "Enter Here")
c1 = 1
c1 = c1 + 1
Print temp
Wend
End Sub
Form2.Cls
End Sub
Load Form1
End Sub
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
✅ Form2 Code:
Private Sub Command1_Click()
Form1.Show
Unload Form2
Form2.Hide
End Sub
Yes, this program can be applied to both questions. Let's break it down and explain which parts are
for each question.
Explanation:
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.
End Sub
Load Form2: This loads Form2 at runtime but does not show it.
End Sub
This part of the code allows interaction between the forms, creating and showing forms
dynamically at runtime.
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.
End Sub
Hide hides Form1, which means it's still in memory but not visible.
Unload Me: This unloads the current form, removing it from memory completely.
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.
Form2.Caption = "Form2 is active" ' Change the caption when Form2 is activated
End Sub
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.
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.