
New
to myExercises:
New
to myNotes:
|
Order
this book today from

|
|
Title:
Microsoft Visual Basic 2005:
RELOADED, Second Edition
Author:
Diane Zak
Click
the image below to order your copy. |
|

|
|
|

| |
[ Up ] [ GUI Standards ] [ Access Files ] [ Accumulators, Counters & Averages ] [ Arrays ] [ Collections ] [ Constants & Enumerators ] [ Crystal Reports ] [ Data Types ] [ Flowchart Symbols ] [ Functions ] [ Keywords ] [ Methods ] [ Operators ] [ Repetition Structures ] [ Selection Structures ] [ Sub Procedures ] [ Toolbox Controls ] [ Variables ] [ Windows Forms ]
Introduction
Collection
Types
Controls Collection
Properties
* Count
* Item
User-defined custom Collection
Declaration
Properties
* Count
* Item
Methods
* Add
* Remove
Introduction
Collections
are useful for managing groups of items that are actively created at
runtime. Collections
are a group of control objects that are treated as one unit. Collections have
index numbers and/or keys to identify them. Collections can be resized and
items can be added or removed during runtime, unlike arrays.
Back
to Top
Collection
Types
Controls
Collection
|
-
|
This
collection is automatically created by Visual Basic.
|
|
-
|
All
the control objects on a windows form belongs to the controls
collection. Every control object that you place on your windows form
is automatically added and indexed in the controls collection.
|
| - |
Panels
and groupboxes are included in the controls collection, but the control
objects contained within panels and groupboxes are not included in the
controls collection. |
| - |
Each
object control in the controls collection is automatically given a unique
index number. |
|
-
|
The
controls collection starts with index number 0 (zero).
|
|
-
|
For
every control object that you add to the windows form it assumes index 0
in the controls collection, and the previous object controls' index number
moves up.
|
|
-
|
The
controls collection works on a last-in-first-out basis, meaning that the
last control you added will have an index number of 0, the first control
that you added will have the last index number, e.g 9.
|
|
-
|
The object control
with an index of 0 will be the first control object in the controls
collection.
|
Back
to Top
Properties
The
controls collection has two properties, nl:
|
Item |
Refers
to the object control in the controls collection |
|
Count
|
Counts
the object controls in the controls collection |
Item
is the default property for collections, therefore you can ommit "Item"
in your code, e.g. myObject = myList(0) instead
of myObject = myList.Item(0). It is
however recommended to include "Item"
as it makes code easier to understand.
Back
to Top
Syntax
|
|
|
Controls.[Item])(index)
|
|
|
|
Controls
|
Refers
to the controls collection
|
|
Item
|
Typing
Item is
optional; it refers to the Item property
|
|
Index
|
refers
to the index number of the object control in the controls collection
|
|
|
|
|
|
|
Controls.Count
[value]
|
|
|
|
Controls
|
Refers
to the controls collection
|
|
Count
|
Counts the number of
object controls in the controls collection |
|
value
|
Is
used with For Each...Next statements and shows how far down the index
should be counted |
|
|
|
Back
to Top User-defined
custom Collections
|
-
|
These
collections are created by the programmer
|
|
-
|
These
collections are used to group related control objects together |
| - |
Indexing
starts at 1, unlike the controls collection that starts at index 0 |
| - |
Items
can be added before or after existing items in the custom collection |
|
-
|
Items
in the collection are referred to using the item's index number and/or key |
|
-
|
When
you remove items from the custom collection you never use the item's name,
unlike when you add items to the custom collection |
|
-
|
Removing
items is done by using the item's index or key, but because index numbers
change it is good practice to assign a key to each item in the collection |
|
-
|
If
you try to remove an object that is not contained in a collection, you
will NOT receive an error, the instruction will simply be ignored. |
Back
to Top
Declaration
|
|
|
Dim|Private|Public
CollectionName As New Collection()
|
|
|
|
Dim|Private|Public
|
The scope of your collection
|
|
CollectionName
|
The name you have given the collection
|
|
As
|
Keyword that shows the birth of a new collection
|
|
New
|
Keyword that shows a new instance of the collection class
is born
|
|
Collection
|
The class type
|
|
|
|
Back
to Top
Properties
The
user-defined custom collection has two properties, nl:
|
Item |
Refers
to the object control in the controls collection |
|
Count
|
Counts
the object controls in the controls collection |
Item
is the default property for collections, therefore you can ommit "Item"
in your code, e.g. myObject = myList(0) instead
of myObject = myList.Item(0). It is
however recommended to include "Item"
as it makes code easier to understand.
Back
to Top
Syntax
|
|
|
CollectionName.Item(object.property)
|
|
|
|
CollectionName
|
The name you have given your collection
|
|
Item
|
Is the Item property of your collection
|
|
object
|
Is the object type in your collection or the object
variable
|
|
property
|
Is the property of the object type in your collection or
the object variable
|
|
|
|
|
|
|
CollectionName.Count
|
|
|
|
CollectionName
|
The name you have given your collection
|
|
Count
|
Is the Count property of your collection
|
|
|
|
|
|
|
|
|
|
Back
to Top
Methods
The
user-defined custom collection has two methods, nl:
|
Add |
To
add new items to the collection |
|
Remove
|
To
remove existing items from the collection |
|
RemoveAt
|
To
remove existing items at a particular index |
Back
to Top
Syntax
|
|
|
CollectionName.Add(object[,key])
|
|
|
|
CollectionName
|
The name you have given your collection
|
|
Add
|
The Add method to add a control object to the collection
|
|
object
|
The name of the control object you wish to add to the
collection
|
|
key
|
Is optional; is a word you assign to the object in order to
reference it
|
|
|
|
|
|
|
CollectionName.Remove(key|index)
|
|
|
|
CollectionName
|
The name you have given your collection
|
|
Remove
|
The Remove method to remove a control object from the
collection
|
|
key
|
The keyword you assigned to the control object to reference
it
|
|
index
|
The index number that is currently assigned to the control
object
|
|
|
|
|
|
|
CollectionName.RemoveAt(index)
|
|
|
|
CollectionName
|
The name you have given your collection
|
|
Remove
|
The Remove method to remove a control object from the
collection
|
|
index
|
The index number that is currently assigned to the control
object
|
|
|
|
|