Unit 1
● Collections: Adding, Removing, Counting, Returning items in a
collection, Processing a collection.
● Working with form : Form Properties,Creating ,Adding ,Removing Forms
in Project,Adding Multiple form ,Managing Form at run time ,Hiding &
Showing Forms.Load & Unload Statement ,Drag&Drop Operation
Activate & Deactivate events Form Load event ,
● Program using form
Collection
Collections are objects in Visual Basic that are used to store a group of data
values. Collections are similar to array. But there are some special features
that differentiate the collections from arrays.
We have seen that array elements must be of the same data type. But the
Collection members can be of any data type and you don't need to set the size
of the Collection object. You can easily add items to the collection and it will
grow accordingly.
VB implements a special object called the Collection object that acts as a
container for objects of all types.In other words , Visual Basic Collection is
defined as a group of logically related items.
A Collection an object that contains Zero or more objects of same class.
we use collection to simplify code if we need to perform the same
operation s on all the objects in a collection .
Method an Property
1.Add (Item ,key,before,after)
Method: adds items to the collection
2. Item(Index)
Method: return an item by index or by key
3. Remove (Index)
Method : Delete an item from collection by index or by key
# Count Property: Return the number of items in the collection
Creating a Collection
To use a Collection in your code, you first need to declare and create it.
Example
Dim names As Collection 'Declaring the Collection
Set names = New Collection 'Creating the Collection
Or, replace the above code with this one line code
Dim names As New Collection 'Declaration and creation
The Add method
You can add one item at a time using the Add method.
Example
Dim names As New Collection
names.Add "john"
names.Add "david"
You may use a string key associated with the item. The string key is used to
refer to a Collection item.
Dim names As New Collection
names.Add "John", "one" ' "one" is the string key, used to refer to the item
The Item method
You can refer to a particular item of the collection using the Item method. Here
you may use either the index or key value. The index value starts from 1.
Syntax: Collection_Name.Item(Index) or Collection_Name.Item(Key)
Example:Retrieving a particular item using the index of the item
Private Sub cmdShow_Click()
Dim names As New Collection
names.Add "John", "one"
names.Add "David"
Print names.Item(2) '2 is the index of the item
End Sub
Output: David
Retrieving a particular item using the string key of the item
Private Sub cmdShow_Click()
Dim names As New Collection
names.Add "John", "one"
names.Add "David"
Print names.Item("one")
End Sub
Output
John
The Item method is the default member of the Collection class, so you may
omit it in your code.
Example
Private Sub cmdShow_Click()
Dim names As New Collection
names.Add "John", "one"
names.Add "David"
Print names("one")
Print names(2)
End Sub
Output
John
David
Before and After
You can choose to store the item values exactly where you want using the
Before and After argument of the Add method.
Example:
Private Sub cmdShow_Click()
Dim items As New Collection
items.Add "one"
items.Add "two"
items.Add "three", , 1
For i = 1 To 3
Print items.Item(i)
Next i
End Sub
Output:
three
one
two
Remove:
You can remove a particular item from the Collection using the Remove
method.
Example:Private Sub
Command1_Click()
Dim country As New
Collection
country.Add "USA"
country.Add "UK"
country.Add "Japan", "j"
country.Remove (1)
country.Remove ("j")
End Sub
Count:
Example
Private Sub cmdCount_Click()
Dim country As New Collection
country.Add "USA"
country.Add "India"
country.Add "Japan", "j"
Dim n As Integer
n = country.Count
Print n
End Sub
Output
3
Delete all items
Use a Do While loop to delete all item from the Collection object.
Example
Private Sub cmdDeleteAll_Click()
Dim country As New Collection
country.Add "Bangladesh"
country.Add "Australia"
country.Add "Russia", "j"
Do While (country.Count > 0)
country.Remove 1
Loop
Print country.Count
End Sub
Output
0
Types of collection
System Collection
1. Forms collection
2. Controls collection
The Forms Collection is also a group of items---however, instead of each item
representing a Control on a form, each item in the Forms Collection points to a
loaded form in the Visual Basic application.
The Controls Collection is a Collection containing items where each item represents
a control on a form. Each Form has its own Controls Collection, and Visual Basic
automatically takes care of updating the Controls Collection for each form placed
on the form.
Control Collection
The control collection is an array that contain all the control in a form .The
Control collection contain references to all the control on that form .Each
member of the collection points to different object on that form . Ex: text
box.combo box,list view.
Syntax Object.control(index)
Here object refers to the form object which contain all the controls
Example:
Private Sub CmdProcess_click()
Dim P1 as Control
For Each P1 in Controls
if Type of P1 is Text Box Then
If P1 .text=” “ Then
Msgbox” All Fields Must be Entered”
Exit if
Exit if
Next P1
End sub
Control Collection
Here's a form in which I have placed an OptionButton, CheckBox, Command
Button and ListBox on the form.
By my count, that's a total of four Controls that I've placed on the form. Let's
run this program now, then pause it and use the Immediate Window to
examine the Controls Collection by typing the following statement into the
Immediate Window. Count is the one and only property of the Collection
Object, and as you can see, this Collection---the Controls Collection---has a
total of four items.
Forms
Visual Basic Form is the container for all the controls that make up the user
interface. Visual Studio creates a default form for you when you create a
Windows Forms Application. Every form will have title bar on which the
form's caption is displayed and there will be buttons to close, maximize and
minimize the form shown below − If you click the icon on the top left corner,
it opens the control menu, which contains the various commands to control
the form like to move control from one place to another place, to maximize
or minimize the form or to close the form .
Creating New Form
To add a new form to a project .Select add form from the project menu or click
on the form button on the toolbar
Step for adding a new form to a project
Step1: Select add form from project menu.
step 2: In the dialog box ,select the new tab and indicate the type of Form you
want (form .about,splash)
Step 3: Click on Open
Setting the Start-Up Form
A typical application has more than a single Form. When an application runs the
main Form is loaded. By setting the Project properties you can control which
Form is to be displayed in the Start-Up of the application.
By default, Visual Basic suggests the name of the first Form created when the
project started.
Loading and Unloading Forms
The Load Statement :- When you work with multiple form you may also want to
use the load and unload statement although you can explicitly load a form in most
cases.The Load is not necessary when you show a form the load is done
automatically.The only time you will code a load statement is when you want to
load a from but not display it until later.
The Load statement has the following syntax :
Load FormName
And the Unload statement has the following syntax :
Unload FormName
The Unload Statement :- To remove a form from the screen ,you can hide it or
unload it ,hiding a form removes its from the screen but the form still remains in
memory and user system resources,If you have many forms and you are sure
that you will not need a from again it is best to unload it rather than to hide it or
we can say that unload the form from the memory and does not display the form
on screen .
Showing and Hiding Forms
Show method is used to Show a Form. If the Form is loaded but invisible, the
Show method is used to bring the Form on Top every other window. If the Form
is not loaded, the Show method loads it and then displays it.
Syntax of the Show method of the Form
FormName.Show mode
The FormName variable is the Form's name, and the optional argument mode
determines whether the Form will be Modal or not. It can have one of the
following syntax :
* 0-Modeless (default)
* 1-Modal
Modeless Forms are the normal Forms. Modeless Forms interact with the user
and the user allowed to switch to any other Form of the application. If you do
not specify the optional mode argument, by default the mode is set to modeless.
The Modal Forms takes the total control of the application where user cannot
switch to any other Forms in the application unless the Form is closed. A modal
Form, thus, must have a Close button or some means to close the Form in order
to return to the Form where the Modal Form was loaded.
Hiding Forms
The Hide method is used to hide a Form. The following is the syntax of the
Hide Method.
FormName.Hide
To hide a Form from within its own code, the following code can be used.
Me.Hide
You must understand that the Forms that are hidden are not unloaded ; they
remains in the memory and can be displayed instantly with the Show Method.
When a Form is hidden, you can still access its properties and code. For
instance, you can change the settings of its Control Properties or call any
Public functions in the Form.
Activate, Deactivate events
The Activate event occurs when an object becomes the active window. The
Deactivate event occurs when an object is no longer the active window.
Syntax
Private Sub object_Activate( )
Private Sub object_Deactivate( )
The object placeholder represents an object expression that evaluates to an
object in the Applies To list.
An object can become active by using the Show method in code.
The Activate event can occur only when an object is visible. A UserForm loaded
with Load isn't visible unless you use the Show method.
The Activate and Deactivate events occur only when you move the focus within
an application. Moving the focus to or from an object in another application
doesn't trigger either event.
The Deactivate event doesn't occur when unloading an object.
' Activate event for UserForm1
Private Sub UserForm_Activate()
UserForm1.Caption = "Click my client area"
End Sub
' Click event for UserForm1
Private Sub UserForm_Click()
Load UserForm2
UserForm2.StartUpPosition = 3
UserForm2.Show
End Sub
' Deactivate event for UserForm1
Private Sub UserForm_Deactivate()
UserForm1.Caption = "I just lost the focus!"
UserForm2.Caption = "Focus just left UserForm1 and came to me"
End Sub
The Basic Concepts of Drag and
Drop
A drag-and-drop operation involves a source object and a target object. The source
object can be any Visual Basic control, and the target object can be any control or
the form itself.
The operation has three parts:
i. The drag operation begins when the user presses the mouse button as the
pointer is over a control that has been enabled as a drag-and-drop source.
ii. The operation continues as the pointer is moved, with the left mouse button
still down, over other controls or the form itself. A control receives
notification, via the Drag Over event, that it is being "dragged over" and can
signal whether the data can be dropped on it by changing the appearance of the
mouse pointer.
iii. The operation ends when the user releases the mouse button. A control is
notified by the Drag Drop event that it has been dropped on.
Properties of Drag & Drop
Controls have two properties that are related to drag-and-drop:
i. DragMode. Set to vbManual (value = 0, the default) for manual drag-
and-drop, which requires use of the Drag method to initiate a drag-
and-drop operation.
ii. DragIcon. Specifies the mouse pointer that is displayed while the
control is being dragged. The default setting displays an arrow with a
rectangle. For a custom mouse icon, set this property at design-time to
a .ICO or .CUR file, or at run-time use the LoadPicture function to
load an .ICO file.
Controls have two events that are related to drag-and-drop. DragOver is used to
detect when an object is dragged over a control, and DragDrop is used to detect
when an object is dropped on a control:
Target_DragOver (source As Control, x As Single, y As Single, State As Integer)
target_DragDrop(Source as Control,X as Single,Y as single)
Target identifies the target object (the one being dragged over or dropped on). It
can be a form, an MDI form, or a control.
Source identifies the source control (where the drag-drop operation began).
X and y gives the horizontal and vertical position of the mouse pointer with
respect to object. These values are always expressed according to the object's
coordinate system
State specifies the relationship between the mouse pointer and the target, as
follows:
i. A value of 0 indicates the pointer just entered the target.
ii. A value of 1 indicates the pointer is leaving the target.
iii. A value of 2 indicates that the pointer is moving within the target.
Example1 For Drag & Drop Operation
Form1 has the following details Since both source & target are controls that are part
of the control array, the event procedures will have an additional argument that
specifies the Index property of the control within the control array.
the Private Sub Imagedrop_DragDrop(Index As Integer, Source As Control, X As
Single, Y As Single)
If TypeOf Source Is Image Then
Imagedrop(Index).Picture = Source. Picture
End If
End Sub
In the above drag Drop event Type of Operator checks ,type of source control
whether it is a text box control or Label, or a Form itself
In the above Drag Drop event it checks whether source is a Image control only
then it's dropped on to the target.
Below example explains Mouse move event of the source control
Private Sub Imageanimal_MouseMove(Index As Integer, Button As Integer, Shift
As Integer, X As Single, Y As Single)
If Button = vbLeftButton Then
Imageanimal(Index).Drag vbBeginDrag
End If
End Sub
When the left mouse button is pressed drag operation is initiated by setting
action to vbBeginDrag, on releasing the mouse button we can end drag
operation by setting the action to VbEndDrag or we can also cancel the
operation by setting the action to
VBCancel. below code explains this.
Private Sub Imageanimal_MouseUp(Index As Integer, Button As Integer,
Shift As Integer, X As Single, Y As Single)
If Button = vbLeftButton Then
Imageanimal(Index).Drag vbEndDrag
End If
End Sub