Thursday, March 22, 2012

Simple Dynamic Control Access Question

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!!<gwhite1@.kc.rr.com> escribi en el mensaje
news:1140128170.531340.150220@.g43g2000cwa.googlegroups.com...
>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

> I tried:
> form.control("label1").text = "test" but it did not work.
> Please help a girl out!!!
> Sheila!!
>
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
"gwhite1@.kc.rr.com" wrote:

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

0 comments:

Post a Comment