Simple Dynamic Control Access Question

  • Thread starter Thread starter gwhite1
  • Start date Start date
G

gwhite1

I can't find this answer. I have a label1, label2 and label3. I want to
be able to do something like this:

worklabel = "label1"

worklabel.text = "this is label 1"

worklabel = "label2"

worklabel.text = "this is label 2"

Is there a way to access controls with string variables? I don;t want
to have to hardcode the variable name.

I tried:
form.control("label1").text = "test" but it did not work.

Please help a girl out!!!
Sheila!!
 
I can't find this answer. I have a label1, label2 and label3. I want to
be able to do something like this:

worklabel = "label1"

worklabel.text = "this is label 1"

worklabel = "label2"

worklabel.text = "this is label 2"

Is there a way to access controls with string variables? I don;t want
to have to hardcode the variable name.

I guess you can try with FindControl("") like this

public sub Form_Load() handles .....bla bla

myresultlabel.text = DirectCast(FindControl("mycontrolname"), Label).Text

end sub
 
kcrr you can access controls by name and you get intellisence and compiler
error checking.

label1.text = "this is label1"
label2.text = "this is label 2"
label3.text = "this is label 3"

Hans was right. You could do the find control like hans said but its tricky
and it could cause problems cause no error checking or compiler errors. I
would put the findcontrol in a function if I went with this.

private function worklabel(byval label as string) as label
return ctype(me.findcontrol(label),label)
end function

then you can code after function added.
worklabel("label1").text = "this is label1"
worklabel("label2").text = "this is label2"

Good Luck
DWS
 

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

Back
Top