how to use variable to refer to a control?

B

Bill Nguyen

I have several textbox controls in a form,.
I name them txt1, txt2, ... txt10.
How dow I refer to them in a loop to get the value in text proerty of each
control?

For x = 1 to 10

value = txt(x).text

next x

Thanks

Bill
 
K

kgerritsen

Since I try to give my controls business-meaning names, I don't iterate
based on an integer index. I have had some success organizing
similar-ruled controls within containers such as panels or tabcontrol
tabpages and using the container controls collection.

Dim textitem as textbox
For each textitem in form1.controls
'do stuff
next textitem

For each textitem in panel1.controls
'do stuff
next textitem

For each textitem in tabcontrol1.tabpages(1).controls
'do stuff
next textitem

If I have to apply a differential rule based on the exact name, an if
or select case on textitem.name will work.

HTH,
Keith
 
B

Bill Nguyen

Keith;

This is great.
However, I still need to be able to refer to an actual control name.
Is there a way to do that?

Thanks again

Billl
 
P

Phill W.

Bill said:
I have several textbox controls in a form,.
Good.

I name them txt1, txt2, ... txt10.

Not so good.
How dow I refer to them in a loop to get the value in text proerty of each
control?

(I love this answer; it /so/ annoys the .Net purists)

You use a Control Array. :)

No, seriously.

You create an array of Controls, in this case TextBoxes, and loop
through that.

Dim textboxes As TextBox() = { txt1, txt2, ... txt10 }

For Each tb As TextBox in textboxes
value = tb.Text
Next

Regards,
Phill W.
 
R

RobinS

I'm a .Net purist, and I think your answer is brilliant.
It's simple and concise, and gets the job done.

Does that mean I'm really *not* a .Net purist? :-(

Robin S.
 
P

Phill W.

RobinS said:
I'm a .Net purist, and I think your answer is brilliant.
It's simple and concise, and gets the job done.

Does that mean I'm really *not* a .Net purist? :-(

? Microsoft.Interaction.Responses.IndefiniteResponses.Maybe

It used to be the case that mentioning "Control Array" (a very specific
VB "Proper" concept) in a .Net group brought down all manner of scorn of
the head of the unwary poster.

I suspect the previously vehement VB bashers are all off trying to work
out what their programs are doing now that they've dived into these
Generics things ... ;-)

Regards,
Phill W.
 
R

RobinS

Well, there's no doubt that Generics are very cool.
But that doesn't mean there's no place in the world
for an array or arraylist. After all, even though
there are plasma tv's out there, some people still
rely on their Sony Wegas to do the job.

Robin S.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top