473,396 Members | 1,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Access a Control by its name as a string

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"

May 17 '07 #1
7 4141
On 17 May 2007 13:17:55 -0700, Mahmoud Al-Qudsi <mq****@gmail.com>
wrote:
>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);

--
Ludwig
http://www.coders-lab.be
May 17 '07 #2
On Thu, 17 May 2007 20:25:54 GMT, Ludwig
<ludwig.stuyck(remove)@telenet.bewrote:
>On 17 May 2007 13:17:55 -0700, Mahmoud Al-Qudsi <mq****@gmail.com>
wrote:
>>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.

--
Ludwig
http://www.coders-lab.be
May 17 '07 #3
On May 17, 11:30 pm, Ludwig <ludwig.stuyck(remove)@telenet.bewrote:
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.

May 17 '07 #4
On 17 May 2007 13:36:11 -0700, Mahmoud Al-Qudsi <mq****@gmail.com>
wrote:
>On May 17, 11:30 pm, Ludwig <ludwig.stuyck(remove)@telenet.bewrote:
>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.
Well you only have to do it once.... I don't think there exists a
method like GetControlByName()...

--
Ludwig
http://www.coders-lab.be
May 17 '07 #5
On May 17, 11:36 pm, Mahmoud Al-Qudsi <mqu...@gmail.comwrote:
On May 17, 11:30 pm, Ludwig <ludwig.stuyck(remove)@telenet.bewrote:
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.
Would the getControls() method work? I'm not clear on exactly what it
does.

May 17 '07 #6
On 17 May 2007 13:43:21 -0700, Mahmoud Al-Qudsi <mq****@gmail.com>
wrote:
>On May 17, 11:36 pm, Mahmoud Al-Qudsi <mqu...@gmail.comwrote:
>On May 17, 11:30 pm, Ludwig <ludwig.stuyck(remove)@telenet.bewrote:
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.

Would the getControls() method work? I'm not clear on exactly what it
does.
GetControls method? Which method is that?

--
Ludwig
http://www.coders-lab.be
May 17 '07 #7
On Thu, 17 May 2007 13:17:55 -0700, Mahmoud Al-Qudsi <mq****@gmail.com
wrote:
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
May 18 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Woody Splawn | last post by:
I know that I can do the following to identify the parent name of an active control Dim sParentName As String = ActiveControl.Parent.Name But how do I identify the name of the active control?...
3
by: B-Dog | last post by:
I'm capturing the checked radio button to XML file using the name of the radio button. I want to read my xml file to find which button was checked on close and the check the appropriate button...
6
by: Notgiven | last post by:
I am considering a large project and they currently use LDAP on MS platform. It would be moved to a LAMP platform. OpenLDAP is an option though I have not used it before. I do feel fairly...
3
by: victor | last post by:
Hello guys, I'm stuck at the following situation, please advise. In my app there exists several control buttons (radio and push). One of the methods is the following: private void...
7
by: =?Utf-8?B?Um9i?= | last post by:
Hi all, I would like to access just one of several controls by its number: I do not want call: Button1.Text instead I want to call: Button+"1".Text
2
by: VMI | last post by:
In my Windows Form, is it possible to get the control name through object sender in an event handler? For example, in private void dataGridView_zip_KeyPress(object sender, KeyPressEventArgs e), how...
9
by: redivider | last post by:
I followed discussion "access form control propertys via control name as string " but am getting NullReferenceException when trying to access any controls via their name using "this.Controls". ...
4
by: davjoh123 | last post by:
Control name on form is "Kit #" The afterupdate event that fires for this control calls the control Kit__ as per following. What is happening here? Private Sub Kit___AfterUpdate()...
2
by: =?Utf-8?B?Unlhbg==?= | last post by:
I have the following code in vs2005; line number is added for convenience: 1 private void Form1_Load(object sender, EventArgs e) 2 { 3 txtMyField.Enabled = true; 4 Form2 f = new...
2
by: ricardosms | last post by:
Hello: I have a custom control with a Combobox that at form1_Load gets filled with the names of the controls with visual interface. From this ComboBox the user selects a control name and that...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.