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 / Languages / VB.NET / July 2007

Tip: Looking for answers? Try searching our database.

Accessing a control if its name is in a string variable

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
JohnR - 05 Jul 2007 06:02 GMT
I have the name of a control in a string variable and I want to change one
of the controls properties.  Right now I recursively scan all the controls
on the form until I get one whose name matches the name in my string
variable, then I know I have the correct control and can proceed to change
the property.
However all that searching seems like overkill.  Does anyone know of a way
to directly access the control if it's name is in a string variable?
Something like an indirect access...
Thanks for any advice...
Petar Atanasov - 05 Jul 2007 09:13 GMT
> I have the name of a control in a string variable and I want to change one
> of the controls properties.  Right now I recursively scan all the controls
[quoted text clipped - 5 lines]
> Something like an indirect access...
> Thanks for any advice...

try Controls.Find [http://msdn2.microsoft.com/en-us/library/ms404295.aspx]
Althought the MSDN sample is about keyed collectios, in the begining of
the page is shown how to focus on control specified by it's name.

Regards,
Petar Atansov
http://a-wake.net
Jan Hyde (VB MVP) - 05 Jul 2007 09:13 GMT
"JohnR" <JohnR104@hotmail.com>'s wild thoughts were released
on Thu, 05 Jul 2007 05:02:56 GMT bearing the following
fruit:

>I have the name of a control in a string variable and I want to change one
>of the controls properties.  Right now I recursively scan all the controls
[quoted text clipped - 5 lines]
>Something like an indirect access...
>Thanks for any advice...

Me.Controls("TextEdit1").Text = "Hello"
--
Jan Hyde

https://mvp.support.microsoft.com/profile/Jan.Hyde
Cor Ligthert [MVP] - 05 Jul 2007 11:52 GMT
Jan,

Cylix is in my opinion doing it correct recursively, I assume he wants to
find the controls on a panel to.

Your me.controls shows in my idea only the controls on the top of the form.

Although I did not check it,  the Find is also to find in a collection, not
to find a child in a collection.

However I am not 100% sure about the last.

Cor

> "JohnR" <JohnR104@hotmail.com>'s wild thoughts were released
> on Thu, 05 Jul 2007 05:02:56 GMT bearing the following
[quoted text clipped - 15 lines]
>
> https://mvp.support.microsoft.com/profile/Jan.Hyde
rowe_newsgroups - 05 Jul 2007 12:27 GMT
On Jul 5, 6:52 am, "Cor Ligthert [MVP]" <notmyfirstn...@planet.nl>
wrote:
> Jan,
>
[quoted text clipped - 29 lines]
>
> >https://mvp.support.microsoft.com/profile/Jan.Hyde

> Although I did not check it,  the Find is also to find in a collection, not
> to find a child in a collection.

Controls.Find has a searchAllChildren bool parameter that you can use
to specify a recursive search.

Thanks,

Seth Rowe
JohnR - 05 Jul 2007 17:37 GMT
Hi all,

Thanks for the great replies.  Looks like Control.find will do what I want,
but that method was introduced in .NET 2.0 and we have not upgraded yet.
So, I'll continue doing it manually (at least it works), until we upgrade
later this year.
Thanks again...

> On Jul 5, 6:52 am, "Cor Ligthert [MVP]" <notmyfirstn...@planet.nl>
> wrote:
[quoted text clipped - 48 lines]
>
> Seth Rowe
Kerry Moorman - 05 Jul 2007 20:52 GMT
JohnR,

If you need to do this for several controls, then you could scan all the
controls on the form once, adding the control to a hashtable and using the
control's name as the key.

Once all the controls are in the hashtable, you could access them by the
hashtable's key, the name of the control.

But this won't gain you any performance benefit if you just need to access
one control by name one time in your app.

Kerry Moorman

> Hi all,
>
[quoted text clipped - 56 lines]
> >
> > Seth Rowe
JohnR - 06 Jul 2007 00:29 GMT
Hi Kerry,

 Ya know, that's a great idea.  That's exactly what I'll do.

Thanks, John

> JohnR,
>
[quoted text clipped - 76 lines]
>> >
>> > Seth Rowe
rowe_newsgroups - 06 Jul 2007 11:55 GMT
On Jul 5, 3:52 pm, Kerry Moorman
<KerryMoor...@discussions.microsoft.com> wrote:
> JohnR,
>
[quoted text clipped - 70 lines]
>
> > > Seth Rowe

You might also want to subscribe to the form's controladded event and
add any dynamically created controls that were created after your scan
to your hashtable.

Thanks,

Seth Rowe
Cor Ligthert [MVP] - 05 Jul 2007 18:21 GMT
Seth

I had the plan to check it when I was home, but now you did it for me.

Thanks

Cor

> On Jul 5, 6:52 am, "Cor Ligthert [MVP]" <notmyfirstn...@planet.nl>
> wrote:
[quoted text clipped - 48 lines]
>
> Seth Rowe

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



©2008 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.