Hello,
I am trying to test a checkbox state by selecting it by its Tag.
Problem is that although the program specifically selects only Check boxes,
it will not recognize the control as a checkbox, and thus will not accept the 'Checked' property that is normal for CheckBoxes.
IMHO This seems liek a VS ignored feature.
Can anyone advice on a workaround or another way to get the same result?
foreach (Control chkbx in Controls)
{
if (chkbx.GetType() == typeof(CheckBox))
{
if (chkbx.Tag == NumUpDn3.Tag && chkbx.Checked) << This 'Checked' not accepted!!
{ do something }
}
}
VulpesPosted Feb 28, 2012, 9:06 AM
If so, then you either need to iterate through the controls collection for that container or do a recursive search for the checkboxes.
SamPosted Feb 28, 2012, 1:13 PM
Yes, you are right. I figured out that the problem was that my check boxes were all part of group boxes.
I moved them out, and it works as expected.
The only problem was that I wanted them inside the frame of the group.
I solved it with by nested foreach: find first the group boxes and then find the check boxes within each group box.
foreach (Control gb in Controls)
if (gb is GroupBox)
{
GroupBox grpbx = (GroupBox)gb;
foreach (Control c in grpbx.Controls)
{
if (c is CheckBox)
{
CheckBox chkbx = (CheckBox)c;
if (chkbx.Tag == numUpDn.Tag && chkbx.Checked)
{
textboxOut.Text = chkbx.Name;
calibration_mode = 3;
}
}
}
SamPosted Feb 28, 2012, 7:00 AM
This solved the 'Checked' issue, and I beleive I understand what you did (Separating the foreach and then Casting c to CheckBox?).
I still have another issue that the foreach iterates and stops after the first foundchaeckbox item.
I'll keep working, but you solved the issue I asked about.
Thanks.
VulpesPosted Feb 28, 2012, 6:08 AM
foreach (Control c in Controls)
{
if (chkbx is CheckBox)
{
CheckBox chkbx = (CheckBox)c;
if (chkbx.Tag == NumUpDn3.Tag && chkbx.Checked)
{
// do something
}
}
}