473,665 Members | 2,798 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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)String List[0].text = "mytext"

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

May 17 '07 #1
7 4157
On 17 May 2007 13:17:55 -0700, Mahmoud Al-Qudsi <mq****@gmail.c om>
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)Strin gList[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.GetPropert y("Text");

Then set its value:

textProperty.Se tvalue(StringLi st[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.c om>
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)Stri ngList[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.GetPropert y("Text");

Then set its value:

textProperty.S etvalue(StringL ist[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.c om>
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 GetControlByNam e()...

--
Ludwig
http://www.coders-lab.be
May 17 '07 #5
On May 17, 11:36 pm, Mahmoud Al-Qudsi <mqu...@gmail.c omwrote:
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.c om>
wrote:
>On May 17, 11:36 pm, Mahmoud Al-Qudsi <mqu...@gmail.c omwrote:
>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.c om
wrote:
How can I say

(Control)String List[0].text = "mytext"
Assuming your form is called "form1":

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

You can also use the ControlCollecti on.Find() method, but I have to admit
that I'm not completely clear on what additional functionality it offers,
if any, over using the ControlCollecti on.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
17335
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? Str = activeControl.Name does not seem to work.
3
3842
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 when for loads. How do I use a variable control name. This is what I'm trying to do below but of course it doesn't work, I'm rookie. Thanks Dim rbchecked as String
6
2999
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 confortable with my ability to use SESSIONS for authentication and access control. Would it better to learn and use LDAP or can you REALLY have just as secure authentication and access control using Sessions? Thanks for your thoughts and experience.
3
1855
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 ManageControl (string ButtonName) { switch (ButtonName)
7
1881
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
37144
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 can I know that the sender is "dataGridView_zip" ? Thanks. VS2005 2.0
9
20434
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". private void mtbPh0_Leave(object sender, EventArgs e) { string callingControl = (sender as MaskedTextBox).Name; this.Controls.Visible=false; I've manipulated callingControl to increment the last character, and I've...
4
1971
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() ChangeKitBase Me.Kit__ End Sub
2
1732
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 Form2(); 5 f.DoUpdate += new Form2.UpdateHandler(MyForm2_ButtonClicked); 6 } 7 private void MyForm2_ButtonClicked(object sender, ValuesUpdateEventArgs3 e)
2
3251
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 control, if is docked, gets undocked and is moved in increments depending on where you click on a PictureBox, changing the control's ".Top" and ".Left" properties. Here is my question: How do I convert the string ComboBox1.SelectedItem & ".Top" ...
0
8438
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8863
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8549
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6187
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5660
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4186
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4356
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2765
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1761
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.