tags:

views:

132

answers:

4

For example in Windows Form, you have textboxes textbox0 to textbox29 and you need to assign them all to an array. What I currently can think of is to do this:

array[0] = textbox0;
array[1] = textbox1;
...
array[29] = textbox29;

Is it possile for me to do sth like this:

for(int i=0; i<30; i++)
{
    array[i] = textbox + i; 
 //and some magic is done such tt this is a variable, eg. textbox1
}
+4  A: 

From the top of my head:

int i = 0;
foreach (Control c in FormX.Controls)
{
    int i2;
    if (c.Name.StartsWith("textbox") && int.TryParse(c.Name.Substring(7),out i2))
    {
        array[i] = c;
        i++;
    }
}
array = array.OrderBy(a => Convert.ToInt32(a.Name.Substring(7))).ToArray();
rdkleine
This could result in the name not correlating with the array index if the controls are out-of-order on the page. For example, `array[0]` might contain `textbox7` etc. Not sure whether or not this would be a problem for the OP.
LukeH
Good one. Added sorting using System.Linq
rdkleine
Sorting will be aplha sorting, meaning You will get textbox1, textbox10, textbox11, textbox12, ..., textbox2, textbox20, ... etc.
ck
There you go added sorting on number after textbox
rdkleine
More nitpicks: *(1)* This will fail if there happen to be other textboxes on the page with non-numeric suffixes; for example, `textboxFoo`. *(2)* If the numeric suffixes aren't continuous then there could still be a mismatch between the name and the array index; for example, if there's no `textbox8` then `array[8]` will contain `textbox9` etc.
LukeH
Aaarghhh!!! ;) well ok added nitpick nr1. For nr2 I would go for another solution but would need to know where @Yeeen needs this for.
rdkleine
Why don't you initialize the array with a large number say array[100] and instead of saying array[i] = c; say array[i2] = c. This way a control textbox10 will be assigned to array[10].
sh_kamalh
Yes sure this could be done, I'm not sure if thats the request from Yeeen. To be honest I have no idea what he tries to accomplish using arrays with controls.
rdkleine
Thanks for your solution and all the discussion here. Would like to mark urs as the ans but the modified ans from sh_kamalh simplifies ur solution and is also what I thought off after looking at ur solution. Furthermore I am supposed to use VS2005 for this project, so cannot support .NET Framework 4. I shall mark ur ans as useful instead.
yeeen
+2  A: 

Well, you could use reflection... but personally I'd try to avoid creating all those separate variables to start with. For example, if you really need designer support, you could avoid creating separate variables but create the array by finding the controls by name.

Alternatively, if you can just autogenerate the controls programmatically in a loop, I'd do that.

Jon Skeet
+4  A: 
this.Controls.OfType<TextBox>().ToArray()

should work. It selects the controls which are TextBox and then converts them to an array.

Nick Jones
This could result in the name not correlating with the array index if the controls are out-of-order on the page. For example, `array[0]` might contain `textbox7` etc. Not sure whether or not this would be a problem for the OP.
LukeH
Depending on the naming convention you could chain some more LINQ methods together to refine the selection: this.Controls.OfType<TextBox>().Where(c => c.Text.StartsWith("textbox")).OrderBy(c => c.Text).ToArray();
Andy Rose
+1  A: 
sh_kamalh
Unless there are more then 100 controls. Use Array.Resize().
rdkleine
Or Control[] array = new Control[Formx.Controls.Count]. This way we will accomodate all the controls.
sh_kamalh
just realised tt default by Windows Form is start from 1 n also need to cast, so i hv to use array[index-1] = (TextBox)c;
yeeen