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:

 

Comments

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.

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

fav vb6 tutorials

What a great contribution! I'v bookmarked Your site.

I loved it - helpful and Your own comments appreciated! :)
Coming back ---

These are my fav vb6 tutorials (other then yours!)

www.profsr.com
www.vbtutor.net
www.programmingtutorials.com

Data mining and Text mining with VB.NET. Visual Studio .NET .ADO.NET. Programming Services
http://www.SQLVB.com

Hiring / VB.NET Jobs
http://www.DataVB.com

Domain Investor Needed
http://www.Beginz.com

Data mining and Text mining with VB.NET. Visual Studio

.NET .ADO.NET. Programming Services

Hiring / VB.NET Jobs

Domain Investor Needed

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...

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

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.

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.

I agree. Site is very good

I agree. Site is very good and usefull!
---
Owner of kalkulator kredytowy

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You may post block code using <blockcode [type="language"]>...</blockcode> tags. You may also post inline code using <code [type="language"]>...</code> tags.

More information about formatting options