Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable control names

Status
Not open for further replies.

shiggyshag

Programmer
Dec 14, 2001
227
GB
HI

I have 10 Lables all named cBox1 to cBox10

I have a loop which i use I as an interger

so I =1
Then I loop through but how do I change the contol name

I.e cBox I = cBox1 etc

I = I +1

Hope I making sense

Cheers
 
Hmm I am not sure I understand. Do you want to change the name of the control at run time, or do you just want to do something to all of these labels, or do you want them all to share the same click event or something?

Anyways, here is some code which would change the caption of all the labels on a form called cBox(Number) to 'Hello'

Code:
 Dim ctl As Control, lbl As Label
        For Each ctl In Me.Controls
            If TypeOf ctl Is Label Then
                lbl = CType(ctl, Label)
                If lbl.Name Like "CBox#" Then
                    lbl.Text = "Hello"
                End If
            End If
        Next

But there are more elegant ways to do things, depending on what exactly you want to do.


Mark [openup]
 
Hi thank you for your help

But its not quite what I want.

In Access I use.
I = 1
Do while I < 5
Me(&quot;cBox&quot; & I) = I
I = I +1
loop

But in VB.net it isnt happy

Cheers


 
yes in access thwt works because texbox has default property text abd labels have caption. so if you say textbox1=&quot;3&quot; it will show 3
in ve.net this does't work
there are no default properties
you shoud do something like this:
- you shoud have a function getControlByName (&quot;name&quot;) witch returns the control with specified name
then - me.controls( me.controls(getControlByName(&quot;cBox&quot;& i)).text=i.tostring

 
Or put the comboboxes in a container control then iterate round the child controls of the container.

Craig
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top