Reference a form object via reflection?

  • Thread starter Thread starter nam23
  • Start date Start date
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
 
Might be easier to loop thorough all controls contained in Form.Controls.

Example of what I've recently done:

foreach (Control c in this.Controls)
{
if (c is CheckBox)
{
CheckBox chk = c as CheckBox;
int tag = Convert.ToInt32(chk.Tag);

if ( (freq & tag) == tag)
{
chk.Checked = true;
}
else
{
chk.Checked = false;
}
}
}
 
Great! I think that will do the trick. Much appreciated...

Neil


Brendan said:
Might be easier to loop thorough all controls contained in Form.Controls.

Example of what I've recently done:

foreach (Control c in this.Controls)
{
if (c is CheckBox)
{
CheckBox chk = c as CheckBox;
int tag = Convert.ToInt32(chk.Tag);

if ( (freq & tag) == tag)
{
chk.Checked = true;
}
else
{
chk.Checked = false;
}
}
}


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
 
Hello (e-mail address removed),
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...

[PD] The solution you received from Brendan is much better than using reflection,
but just for your information:
- FieldType won't ever be of type CheckBox, because it's of type Type :)
You can write desired condition like this: FieldType.IsAssignableFrom(typeof(CheckBox)).
- To grab a reference you should call formFields.GetValue(this)
- By default fields created by the designer are private so GetFields() won't
return them. Use GetFields with BindingFlags.NonPublic
- You should be aware that not all controls that are on the form must be
referenced by form fields.

Piotr Dobrowolski
Piotr.Dobrowolski_usun_gmail.com
 
Back
Top