|
Hi,
I have many TextBoxes on my Form. I want to populate them with values from a string array. (The the number of TextBoxes is the same as the array size.)
I'm guessing that the best would be to use a 'foreach' where I additionally test that each one is a TextBox and has the right TabIndex or Tab (ie. this should match the index of the array).
Two questions - is the above the right approach; and - if not - what would be a more robust way of approaching this?
Sample code would be greatly appreciated!
Many thanks
|
| GaryVe Thursday, May 17, 2007 9:30 PM |
If you give the text boxes the right name, like the default name that the designer gives them (textBox1, textBox2, etc), you could do something like this:
string[] textInit = { "a", "b", "c", "d" }; int ctl = 1; foreach (string txt in textInit) { this.Controls["textBox" + ctl].Text = txt; ctl++; }
|
| nobugz Friday, May 18, 2007 1:10 AM |
Instead work with indexes and arrays ... use a Dicctionary<,> ... see this:
Code Snippet
Dicctionary myValues = new Dicctionary();
TextBox t;
foreach(Control c in this.Controls)
{
t = c as TextBox;
if(t!=null)
{
myValues.Add(t.Name,t.Text);
}
}
After, you cant query a value by the textbox name :
Code Snippet string test = myValues["textBox1"];
I hope that this be helpful.
Regards. |
| vtortola Thursday, May 17, 2007 9:38 PM |
I don't think that will work.
1. You'll need to know the name of the text boxes in order to assign a value to it.
2. Unless your textboxes have a number value set as a name property, a loop is not necessary.
Adamus |
| Adamus Turner Thursday, May 17, 2007 9:39 PM |
Hello,
Here is a simple example based on something I picked up from this very same forum some time ago.
Patrice
string [] arr = new string[2];
arr[0] = "Hello";
arr[1] = "World";
foreach (Control c in this.Controls)
{
if(c is TextBox)
{
switch(c.Name)
{
case "textBox1":
textBox1.Text = arr[0];
break;
case "textBox2":
textBox2.Text = arr[1];
break;
default:
break;
}
}
}
|
| Patrice1974 Thursday, May 17, 2007 10:11 PM |
| Patrice1974 wrote: |
|
Hello,
Here is a simple example based on something I picked up from this very same forum some time ago.
Patrice
string [] arr = new string[2];
arr[0] = "Hello";
arr[1] = "World";
foreach (Control c in this.Controls)
{
if(c is TextBox)
{
switch(c.Name)
{
case "textBox1":
textBox1.Text = arr[0];
break;
case "textBox2":
textBox2.Text = arr[1];
break;
default:
break;
}
}
}
| |
The following is a the same as above. No loop is required:
textBox1.Text = arr[0];
textBox2.Text = arr[1];
2 lines...why are you looping through controls?
Adamus |
| Adamus Turner Thursday, May 17, 2007 10:25 PM |
How are the textboxes and the string array related?.
Is like:
TextBox0 should contain array[0];
TextBox1 should contain array[1];
etc?
|
| Marcelo Guerra - MSFT Thursday, May 17, 2007 10:38 PM |
If you give the text boxes the right name, like the default name that the designer gives them (textBox1, textBox2, etc), you could do something like this:
string[] textInit = { "a", "b", "c", "d" }; int ctl = 1; foreach (string txt in textInit) { this.Controls["textBox" + ctl].Text = txt; ctl++; }
|
| nobugz Friday, May 18, 2007 1:10 AM |
Thanks - this is exactly what I am after. Essentially it is accessing the windows controls by using a variable name in a loop, where the name is of the format txtR1C1, txt R1C2, etc.
The foreach loop is also useful, although in my case now no longer needed.
Much appreciated!
Gary
|
| GaryVe Friday, May 18, 2007 3:15 PM |