Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / February 2006

Tip: Looking for answers? Try searching our database.

Simple Dynamic Control Access Question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
gwhite1@kc.rr.com - 16 Feb 2006 22:16 GMT
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!!
Hans Baumann - 16 Feb 2006 22:48 GMT
>I can't find this answer. I have a label1, label2 and label3. I want to
> be able to do something like this:
[quoted text clipped - 9 lines]
> 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!!
DWS - 17 Feb 2006 01:04 GMT
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

> I can't find this answer. I have a label1, label2 and label3. I want to
> be able to do something like this:
[quoted text clipped - 15 lines]
> Please help a girl out!!!
> Sheila!!

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.