Access a Control by its name as a string

M

Mahmoud Al-Qudsi

I have a list of strings that refer to various controls on a form.
They're not all the same type, they are all unique.

How can I say

(Control)StringList[0].text = "mytext"

Basically, set the `text` property for the winforms control named
'StringList[0]' to "mytext"
 
L

Ludwig

I have a list of strings that refer to various controls on a form.
They're not all the same type, they are all unique.

How can I say

(Control)StringList[0].text = "mytext"

Basically, set the `text` property for the winforms control named
'StringList[0]' to "mytext"

First, get a PropertyInfo object:

Type type = StringList[0].GetType();
PropertyInfo textProperty = type.GetProperty("Text");

Then set its value:

textProperty.Setvalue(StringList[0], "myText", null);
 
L

Ludwig

I have a list of strings that refer to various controls on a form.
They're not all the same type, they are all unique.

How can I say

(Control)StringList[0].text = "mytext"

Basically, set the `text` property for the winforms control named
'StringList[0]' to "mytext"

First, get a PropertyInfo object:

Type type = StringList[0].GetType();
PropertyInfo textProperty = type.GetProperty("Text");

Then set its value:

textProperty.Setvalue(StringList[0], "myText", null);

Sorry, it seems that you have a list of strings, not of controls.
Thereforen of course, you first have to get the control by their name.

You could therefore build a hashtable of all your controls at startup,
with the name as the key of the hashtable; and then you can access
them easily by name using the hashtable.
 
M

Mahmoud Al-Qudsi

Sorry, it seems that you have a list of strings, not of controls.
Thereforen of course, you first have to get the control by their name.

You could therefore build a hashtable of all your controls at startup,
with the name as the key of the hashtable; and then you can access
them easily by name using the hashtable.


Thanks.
I was pretty much wondering if such a table was already more or less
existing.

I'm currently iterating through all the controls and checking if the
string is a match to the name. Very inefficient, needless to say.
 
L

Ludwig

Thanks.
I was pretty much wondering if such a table was already more or less
existing.

I'm currently iterating through all the controls and checking if the
string is a match to the name. Very inefficient, needless to say.

Well you only have to do it once.... I don't think there exists a
method like GetControlByName()...
 
M

Mahmoud Al-Qudsi

Thanks.
I was pretty much wondering if such a table was already more or less
existing.

I'm currently iterating through all the controls and checking if the
string is a match to the name. Very inefficient, needless to say.

Would the getControls() method work? I'm not clear on exactly what it
does.
 
P

Peter Duniho

How can I say

(Control)StringList[0].text = "mytext"

Assuming your form is called "form1":

form1.Controls[StringList[0]].Text = "mytext";

You can also use the ControlCollection.Find() method, but I have to admit
that I'm not completely clear on what additional functionality it offers,
if any, over using the ControlCollection.Item property (which is what the
above syntax is doing). So I will avoid making any comments about it. :)

Pete
 

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

Top