Firstly, arrays are 0 based (you start at 1).
Secondly, you need to add a reference to the control -- in that code you're
just adding a string, and trying to cast it to a CheckBox.
Unfortunately AFAIK there's no way to do a name lookup of a control, so
you'll need this code first:
Hashtable controlLookup = new Hashtable();
foreach (Control ctl in Controls)
controlLookup.Add(ctl.Name, ctl);
Then replace this line:
checkArr
=(CheckBox)controlLookup["CheckBox" + (i+1)];
--
John Wood
EMail: first name, dot, last name, at priorganize.com
chxant said:
I tried the second solution, but it's not working.
This is what I tried:
CheckBox [] checkArr = new CheckBox[counter]; //no error on this
for (int i=1;i<=counter;i++)
{
checkArr
=??????? //I tried checkArr=(CheckBox)"CheckBox"+i
//but it's not working.
}
The CheckBoxes are named CheckBox1, CheckBox2, ...
I want to fill the array dynamicly (using counter).
Thanks again for your help!
John Wood said:
If you want to lookup by name, then you can put the control references into
a Hashtable (don't forget to add a 'using System.Collections' at the top).
Alternatively you can put them into an ArrayList and this will give you
numerically indexed access.
--
John Wood
EMail: first name, dot, last name, at priorganize.com
Thank you for your answer.
The checkboxes are already created.
Do I have to create an array like this:
CheckBox [] arr;
Or like this:
String [] arr; (and using the names)?
Do you mean that you want a control array?
There's a few ways you can achieve this. If you are dynamically creating
the
controls, then you can use the Controls.Add(x), and x.CreateControl()
functions (where x is a new checkbox instance), and then add the x
instance
to an ArrayList to provide indexed access. You'll also have to position
the
control yourself.
If the checkboxes are already created, you can add the names of the
checkboxes to an array at runtime and then use the array for indexed
access
to the checkboxes at runtime from then onwards.
--
John Wood
EMail: first name, dot, last name, at priorganize.com
Hi,
I want to use a parameter in a control name.
Something like this:
i=1;
(CheckBox + i).ID="test";
Is this possible?
Thanx.