Understanding control arrays

Level:
Level1

Written By TheVBProgramer.

CONTROL ARRAYS

 

Similar to arrays of variables, you can group a set of controls together as an array. The following facts apply to control arrays:

 

  • The set of controls that form a control array must be all of the same type (all textboxes, all labels, all option buttons, etc.)

 

  • You set up a control array by naming one or more controls of the same type the same name and set the Index property of each control in the array to a non-negative value (i.e., the controls in the control array are usually indexed from 0 to one less than the number of controls in the array).

 

  • The properties of the controls in the control array can vary: i.e., some members can be visible or not, they can be sized differently, they can have different fonts or colors, etc.

 

  • To refer to a member of a control array, the syntax is:

 

ControlName(Index)[.Property]

 

For example, to refer to the Text property of the first element of an array of textboxes called txtField, you would use:

 

txtField(0).Text

 

  • All the members of the control array share the same event procedure – for example, if you have a control array of 10 textboxes call txtField, indexed 0 to 9, you will not have 10 different GotFocus events – you will just have one that is shared amongst the 10 members. To differentiate which member of the control array is being acted upon, VB will automatically pass an Index parameter to the event procedure. For example, the GotFocus event procedure for the txtField control array might look like this:

 

Private Sub txtField_GotFocus(Index As Integer)

 

txtField(Index).SelStart = 0

txtField(Index).SelLength = Len(txtField(Index).Text)

 

End Sub

 

- or -

 

Private Sub txtField_GotFocus(Index As Integer)

 

With txtField(Index)

.SelStart = 0

.SelLength = Len(.Text)

End With

 

End Sub

 

For events where VB already passes a parameter (for example, the textbox's KeyPress event where VB passes the KeyAscii parameter), VB will add "Index" as the first parameter, followed by the parameters that are usually passed to the event. For example, the procedure header of the KeyPress event of the txtField control array would look like this:

 

Private Sub txtField_KeyPress(Index As Integer, KeyAscii As Integer)

 

 

To build a sample application that uses a control array, perform the following steps:

 

  • Start a new VB project. Place a command button toward the bottom of the form and set its properties as follows:

 

Property Value

(Name) cmdTest

Caption First

 

At this point your form should look like this:

 

 

  • Click the command button once to select it. Then Copy it (press Ctrl-C, or Edit à Copy, or right-click the mouse and choose Copy).

 

  • Click on an open area of the form and Paste (press Ctrl-V, or Edit à Paste, or right-click the mouse and choose Paste). The following message will appear: You already have a control named 'cmdTest'. Do you want to create a control array? Respond Yes. The pasted control will appear in the upper left-hand corner of the form. Move the pasted control toward the bottom, next the original. By answering yes to the prompt, VB automatically set the Index property of the original command button to 0 and set the Index of the pasted control to 1.

 

  • Paste two more times (VB will not prompt you any more now that it knows you want to create a control array), moving the pasted controls next to the others. Set the Captions of cmdTest(1), (2), and (3) to "Second", "Third", and "Fourth" respectively. At this point your form should look like this:

 

 

 

  • Place the following code in the cmdTest_Click event:

 

Private Sub cmdTest_Click(Index As Integer)

 

Print cmdTest(Index).Caption

 

End Sub

 

 

  • Run the project and click the various buttons in any order. A sample run is shown below:

 

If you enjoyed this post, subscribe for updates (it's free)

Can anyone help me!!

I'm trying to make a program to calculate the product of two matrices using array n looping....I'v taken the values of the two matrices from the user but the problem is in the "Coding of Multiplication"????

code for .bat script

i want to create a program in vb 6.0 that calling the program that i made in system 32.
how can i call the program that i created in vb 6?
can anybody help me with this?

Crap

Crap

I know! -.-

I know! -.-

how can i create multiple pages in browser in vb6.0?

i just created a new browser in vb and i dont know how to implement multiple window in that browser so please help me.

clinic management system

Hi,

How could I melfunction all my buttons in my startup form, since this button is allowed to be used by user once they have login into the program.

i just want to know how to

i just want to know how to get addition of two matrices in vb6

please help me!.. what is

please help me!.. what is the source code of exit or close code..in visual basic?...

Code for exit with message confimation

Dim R as String
R= Msgbox ("Are you sure you want to exit this program?", vbCritical + vbYesNo, "Exit")
If R = vbYes then
End
Else
Me.Show

Re: Code for exit with message confimation

Dim R as String
R= Msgbox ("Are you sure you want to exit this program?", vbCritical + vbYesNo, "Exit")
If R = vbYes then
End
Else
Me.Show
End if

Note: Paste this in any command button exit (Click or KeyPress)

private sub

private sub cmdclose_click()
unload me
end sub

storing com port value in excel sheet using vb

I m working with a project that receives the data from com port (serial port) and stores those values in an excel sheet continuously for a particular interval of time. i can receive the data but problem is with storing the value cell by cell... it can be stored using array or what else can be used.....

Sub fir multiple dynamically created controls

Using visual basic editor in Microsoft Excel I coded checkboxes to be created at runtime. There is no index property so referring to it by name only allows me to access the checkbox created last. How can I code for all the checkboxes to perform the action. I want each checkbox to change color depending on checked and unchecked. There are several checkboxes so I do not want them made at design time.

Multiple dynamically created controls

I created 10 command buttons within the code when the form loads using:
Dim WithEvents mycmd as CommandButton

And
Private Sub mycmd_click()
......
End Sub
But this only allows me to access the last button created so how do I access the other ones? Please Help.

Button

how can i change backcolor of button?

well it is for a random

well it is for a random example changing it to red in vb
cmd1.backcolor=vbred
you have to have vb in frount of the color and to get to the original color it vbbuttonface

CommandButton backcolor

Hi!

Below are the steps on how you can change the backcolor of a command button.

1.) Change its Style property to Graphical. (Properties window)
2.) Then, you are good to go.
Me.Command1.BackColor = vbBlue

I hope this can help.

Regards,
tobz

Backcolor property

Type:

Command1.Backcolor = vbRed (vbBlue or vbBlack etc)

or

Choose the backcolor you like from the pallete of colours through the properties toolbar of the command button.

VB6 TUTORIAL APPRECIATION

Good evening!

I love using this visual basic tutorial because I find it much helpful to me as I am just a beginner, but it helps me find coding in vb6.0 somehow clear. I realy want to continue using this site until I become perfect in programming with vb6.0.

It is a great hour and privilege to know you.

Just a thought

Hi, I don't know your background or the necessity to use VB6. But if you're new to programming, I highly suggest you to use .NET framework instead. VB6 is dead, or should be. But maybe you don't have the choice, which is my case atm. God help me.

help pls

make 4 button
left right down and up
if play this
once u click each button it will
move
sample
left
the arrow will move to left
gets?
only the codes i need

Keypress event on textbox control array

please help i'm stuck!

I have 10 textbox array. I wanted to allow numeric input only so i use this code:

Private Sub txtfield_KeyPress(Index As Integer, KeyAscii As Integer)
If Not Chr(KeyAscii) Like "[0-9]" And KeyAscii <> 8 Then KeyAscii = 0
End Sub

but it gives me compile error : Procedure declaration does not match description of event or procedure having the same name.

please help what to do, thanks.

Strange.

This is strange. Try:

Private Sub txtfield_KeyPress(KeyAscii As Integer)

If that works, you have not created a control array!!
Select the textbox and set Index Property to a number.
After that you have to change your code back to

Private Sub txtfield_KeyPress(Index As Integer, KeyAscii As Integer)

Command to print

I have created a project with 5 fields that I require to be printed, how do I link the command button to print these fields.

Command button to print

Please help: I have created a project with 5 fields with both text as well as dropdown list boxes, I need the lines to print when I press the print button (used the command button and labelled "PRINT").

msg

thank you........

To print the result of array in textbox

pgPlease send me how to print the array in the text box

simultaneously execute codes for multiple arrays

could anybody help me. i want to write a code that would be executed simultaneously by multiple arrays once a button is pressed. for example i have 3 arrays. i want those arrays to print out the number 5 simultaneously. not one after the other.

That doesn't work

You can't execute commands of what type ever simultaneously, at least not in VB. To do so, you need a computer with multiple processors (so that one can execute one thing while the other does the other one), and you need a programming language that supports threading. You might be able to do some HACKING to do that in VB6, but it's not save and no fun.

By the way, what do you mean when saying "i want those arrays to print out the number 5 simultaneously. not one after the other" ? And why should you want to do such a thing?

contol array

hWe're asked to create a program that will read 10 numbers using number generator, and print out the lowest and the highest number in another control plus its avearge...
P Please help me,,, anyone out there that is well practiced in vb 6.0..
please do email me at my account. ty

array put to textbox

how to put the print out of command array to text box because when i press the comand button then the print out show at form, please help me.

honestly....this site 's the

honestly....this site 's the most easy to understand....i'd love to keep watching this page regulerly to learn new stuffs about vb 6.0 and vb.net

agreed

agreed

array

i want to ask about array multidimesi. where one of value gets from value of combo box..
may be in java like this :
int z=holidays.lenght;
float[][] y2=new float[z][3];
thanks before...

Multiple dimensions

That's in VB:

Dim Z As Long
Dim Y2() As Single
 
' Assign a value to Z....
ReDim Y2(Z - 1, 2)

Note that you have to use dynamic arrays and that you have to subtract one from the count (since you specify the upper bound in VB).

By the way, this question is off topic! This page is about CONTROL arrays, not simple arrays (very different thing indeed!)

Control Arrays

Very basic info's on arrays. Can anyone help with code on parallel arrays. I am doing a homework project with two control arrays, one for states and the other for their capitals. They are in the form of option buttons. The user selects a capital from one array and a state from the other array. The user then hita a command button to check the answer which is displayed in a label control. I cant get the code right to ompare the two arrays and can't find info on it anywhere, can anyone help?

i cant do my home work HELP!!!!

I need help for my school project its the first project for school and i have no idea
how do i make a counclator in vb i have set buttons as arrayes and all that but don know how to make the arrayds index show up in my textbox

display on calculator screen

for each control button use the following code, it should display in your textbox upon clicking the buttons
Private Sub cmd1_click()
TxtDislay.text= "cmd1"
Chose = "1"
Txtdisplay = 1
End Sub

how do i give control array

how do i give control array to datapicker can u tell me?

Control array of Command Buttons

Hi friends, I want to develop a a sudoku game in VB. For that actually I need 81 buttons. Instead of creating 81 buttons, can I create an array of command buttons. And tell me how to access the individual buttons using loops? Pls clear my doubt..

i also am working on a

i also am working on a sudoku game in vb. i already have the codes for it on 81 control array of textboxes it will create a solved sudoku and then i created a code for random textboxes in the control array to disappear. my only problem now is how to detect if the player would input a wrong number then the textbox of selected index will change with a strikethru fontstyle. if you know how to please email me at myn3arian@yahoo.com in ym or facebook. and if you want the code of my program email me also.

control array-sudoku game

MsgBox Index
MsgBox Txt(Index).Text

paste this code in text box(named as Txt) got focus event. u can catch the textbox index and the data in the text box. then i think u can easily compare with the numbers in the remaing text boxes

to anyone who can read this

to anyone who can read this pls help me

what is the code for this

i want to craete a program in vb that goes like this
for example
spell "APPLE"
if the first letter is "A" then it will loop to the Second Letter, then if second letter is "P" then it will loop again but if the type letter does not match the correct arrangement a message box will appear

can you help me pls..
i will see it later.i will w8 fellas

in variable word will be the

in variable word will be the users answer
in variable answ will be solution

for i = 1 to len(word) 'loops "lenght of word" times
'pick i-th letter form word and confront it with i-th letter from answ
if not mid(word,i,1) = mid(answ,i,1) then 'if i-th letter from word is not same as i-th letter form answ then
'message box appear
end if
next i

what is the conditional statement use in array

somebody help me
what is the code for this
iwant to craete a program that determine the correct spelling of a word for example "spell APPLE"
if thefirst letter is "A" then it will continue,then if the second letter is "P" then it will continue but when for the third letter is "C" a message box will appear. how to do this
pla

Site is very very usefull! I

Site is very very usefull!
I like it!!!

can any show me how to limit

can any show me how to limit the size of label.caption.e.g if i entered ten character n i only want 4 to be shown

Truncate Lable Caption

One way would be:

If Len(label.caption) > 4 Then
    label.caption = Left$(label.caption,4)
End If

You don't need to use

You don't need to use code!
Just change MaxLength to 4!!!

How to create control arrays programmatically

How can I create a dynamic control array programmatically...........
Is it possible to do?

If you don't want to create

If you don't want to create object (you have objects created) then just
1. change the index of each object
2. change index of each object

I can't create an array

Hello there, I know this is the usual way on creating array. Today I am using Visual Studio, Visual Basic but having problem on creating one. When I paste the button (Button1) it did not prompted me about array. Instead it renamed the command button to Button2. I wonder why it did automatic renaming. Also if I try to change the properties (command name) to same name as Button1, it just says: Property value is not valid. The name button1 is already in use by another component. I really wonder why, though I am creating arrays before but I have used different installer before. Hope you can help. Thank you very much.

fist one to one

hi, at the initial stages fo your programme, create the objects and go into the code mode from here do the following in the order:

- Change the property name in Properties
- change the captionname either through the manusal insertion into the properties caption space or click on the text of the form or button and change the text to desired text
- CAUTION - DO NOT USE THE SAME NAMES FOR CAPTION, YOU ARE RIGHT THE NAME OS USED BY ONOTHER PROPERTY THEN RESORT TO SIMILIAR BUT DIFFERENT NAME FOR INSTANCE BUTTON 1(CDM1) AS THE PROPERTY NAME AND BUTTON 2 WILL BE CDM2.

just set the index property

just set the index property of the first control to zero and then copy/paste and it will work (but won't prompt you to make an array, it just does it, and increments the index property by one each time)

Its the perfect way.

Form name= form1
So here Arrays od command buttons are-
1st Command button >> name=command(0)
2nd Command Button >>name=command(1)
3rd Command button>> name=command(2)

Now create a Textbox on the Form called "Text1"
Now Copy and Paste Following commands in the code window of Form1-

Private Sub Command1_Click(x As Integer)
If x = 0 Then
Text1.Text = "one"
ElseIf x = 1 Then
Text1.Text = "two"
Else:
Text1.Text = "three"
End If
End Sub

I THINK ITS THE PERFECT ANSWER..

Reindexing Control Array

Hi,
I want to know how can i re-index control array in vb6.0 ??? e.g. i have a command button of 10 numbers. which meens a control array of command button which contains 10 (0-9 in array mode) command buttons. if some how i delete the 5th number of command button and add a new one then the place of 5th command button is empty and a new array number which is 11 is placed. this will increases the control array.

So how can i re-index rest of the command buttons which are after the 5th command button.

Just in window Properties

Just in window Properties change property Index

Adding lists to a combo box

I want to add the same list to multiple combo box, what is the best way to achieve this at initialization. Appreciate any code
thanks
PS good site thanks

If you want to do that in

If you want to do that in runtime then code is:
Combo1(0 - 9).AddItem "MyItem"
It will add item "MyItem" to Combos from 0 to 9

Thank you!

Thank you!

I like ur site

I like your website very much. I am not much more skilled in vB. I need extra sample programs in timer and shapes. Would you please posted me

what losers

that isn't very nice this helps people who are just learning vb i think you all need therapy. this is a great site.

I love your site.. it really

I love your site.. it really helps me with my VB programming.. thanks a lot for posting.. please post more.

Great site. Very helpful. #1

Great site. Very helpful. #1 is stupid.

yep

i agree that guy is stupid

hello i just wanna say that

hello i just wanna say that your website stinks because it did not help me

you must be stupid not to

you must be stupid not to learn from here.. and honestly, the guide is too easy. thanks for posting this site.

Sorry you feel that way. It

Sorry you feel that way. It would be helpful if you would be a little more specific as to why you didn't find it helpful.

This website is very good and this man (in message 1) is stupid

thats all.