Using a variable to specify the name of a control

L

lgbjr

Hi All,

I need to specify a control's name using a variable and I can't seem to get
it right.

I have several labels on a form and depending on a user's actions, a label's
text is changed. Rather than writing code for each label and each event that
can change the labels, I'm trying to do all of the changes in one event, but
I need to specify the label's name using a variable.

for instance, I have an integer (i) that is incremented in a loop. The label
that I want to change in each pass of the loop might be Label+i.ToString.
What I can't figure out is how to make a change to the label. This is what
I'm trying, but I get a Cast not Valid error:

DirectCast("Label"+i.ToString,System.Windows.Forms.Label).text="MyText"

TIA
Lee
 
P

Peter Huang [MSFT]

Hi

the "Label"+i.ToString is an string, it is not the reference to the labeli
object, directcast is used to cast certain reference to any other
compatible type.

e.g.
Class A
Class B herits A

So the B instance can be casted to A or vice visa.

For your scenario, I think we may enumerate the control in the form's
controls collection as below.
For Each o As Control In Me.Controls
If TypeOf o Is Label Then
Debug.WriteLine(o.Name)
End If
Next

or We can use the reflection to get the instance directly.
e.g. the code below will get the reference of the Label2.
Dim lb As Label = Me.GetType().InvokeMember("_Label2",
BindingFlags.Instance Or BindingFlags.GetField Or BindingFlags.NonPublic,
Nothing, Me, New Object() {})
lb.Text = "Changed"

NOTE: If we declare the the Label1 as below, we can get the reference by
Label1,
Friend Label1 As System.Windows.Forms.Label
while if the Label1 is declared as below.
Friend WithEvents Label1 As System.Windows.Forms.Label
We need to get the reference by _Label1.


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
C

Cor Ligthert

Lbjr,

Thanks to Peter I see that I misunderstood your question. To give you an
extra alternative (typed here watch typos)

\\\
For i as integer = 0 to Controls.count
If Controls(i) = ("Label" & i.ToString) then
Controls(i).text="MyText"
End if
Next
///
This sample is by the way only with labels direct on a form.

I hope this helps better.

Cor
 
H

Herfried K. Wagner [MVP]

lgbjr said:
for instance, I have an integer (i) that is incremented in a loop. The
label that I want to change in each pass of the loop might be
Label+i.ToString. What I can't figure out is how to make a change to the
label. This is what I'm trying, but I get a Cast not Valid error:

DirectCast("Label"+i.ToString,System.Windows.Forms.Label).text="MyText"

Accessing controls by their names or indices
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=controlbynameindex&lang=en>
 
G

Guest

I sometimes use Cor's code with a minor change:

For i as integer = 0 to Controls.count
If Controls(i).Name = ("Label" & i.ToString) then
Controls(i).text="MyText"
End if
Next
 
P

Peter Huang [MSFT]

Hi Cor,

Thanks for your knowledge sharing in the community. :)

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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