Accessing a control if its name is in a string variable

  • Thread starter Thread starter JohnR
  • Start date Start date
J

JohnR

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...
 
JohnR said:
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...

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
 
"JohnR" <[email protected]>'s wild thoughts were released
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...


Me.Controls("TextEdit1").Text = "Hello"
 
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
 
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





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
 
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...
 
Seth

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

Thanks

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

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
 
Back
Top