N
nam23
Hi,
I'd like to create a List containing all the checkboxes on a form. The
thing is, I don't know how many checkboxes there might be. I thought
perhaps I could use reflection to look at all of the fields on the
form; if a given field is of type System.Windows.Forms.Checkbox, then
I'd add it to the List. But the following code isn't working.
....
public formConstructor()
{
List<CheckBox> checkBoxesOnForm = new List<CheckBox>();
InitializeComponents();
FieldInfo[] formFields = this.GetType().GetFields();
int numberOfFields = formFields.getLength(0);
For (int i = 0; i < numberOfFields; i++)
{
if (formFields.FieldType is CheckBox)
{
//add the checkbox to the List somehow...???
//The following line doesn't work but was a wild guess
checkBoxesOnForm.Add(((Checkbox)formFields.GetValue()))
}
}
}
....
The above code has two main problems:
- The compiler warns that FieldType won't ever be of type CheckBox
- I don't know how to grab a reference to the checkbox from the
FieldInfo array
Anyone have any ideas? Thanks very much...
Neil McQuarrie
I'd like to create a List containing all the checkboxes on a form. The
thing is, I don't know how many checkboxes there might be. I thought
perhaps I could use reflection to look at all of the fields on the
form; if a given field is of type System.Windows.Forms.Checkbox, then
I'd add it to the List. But the following code isn't working.
....
public formConstructor()
{
List<CheckBox> checkBoxesOnForm = new List<CheckBox>();
InitializeComponents();
FieldInfo[] formFields = this.GetType().GetFields();
int numberOfFields = formFields.getLength(0);
For (int i = 0; i < numberOfFields; i++)
{
if (formFields.FieldType is CheckBox)
{
//add the checkbox to the List somehow...???
//The following line doesn't work but was a wild guess
checkBoxesOnForm.Add(((Checkbox)formFields.GetValue()))
}
}
}
....
The above code has two main problems:
- The compiler warns that FieldType won't ever be of type CheckBox
- I don't know how to grab a reference to the checkbox from the
FieldInfo array
Anyone have any ideas? Thanks very much...
Neil McQuarrie