Not sure if i'm doing this right

E

esebastian

Hello Everyone,
I would just like some advice to make sure i am doing what i want to
do the right way because i don't think i am.

I have a form that contains checkboxes. The checkboxes are created
dynamically from a database. When the user presses the submit button i
want to loop through all of the checkboxes and fill an IList with the
text from the checkboxes that are checked. Here is how i do it.. it
looks a bit convoluted , is there a better way of doing this?

Thanks in advance,


foreach (Object obj in this.Controls)
{
if (obj.GetType() == typeof(GroupBox))
{
GroupBox tempGroupBox = (GroupBox)obj;
foreach (Object tmpObj in
tempGroupBox.Controls)
{
if (tmpObj.GetType() == typeof(CheckBox))
{
CheckBox chk = (CheckBox)tmpObj;
if (chk.Checked)
{
imageList.Add(chk.Text);
}
}
}

}
}
 
B

Bruce Wood

Hello Everyone,
I would just like some advice to make sure i am doing what i want to
do the right way because i don't think i am.

I have a form that contains checkboxes. The checkboxes are created
dynamically from a database. When the user presses the submit button i
want to loop through all of the checkboxes and fill an IList with the
text from the checkboxes that are checked. Here is how i do it.. it
looks a bit convoluted , is there a better way of doing this?

Thanks in advance,

foreach (Object obj in this.Controls)
{
if (obj.GetType() == typeof(GroupBox))
{
GroupBox tempGroupBox = (GroupBox)obj;
foreach (Object tmpObj in
tempGroupBox.Controls)
{
if (tmpObj.GetType() == typeof(CheckBox))
{
CheckBox chk = (CheckBox)tmpObj;
if (chk.Checked)
{
imageList.Add(chk.Text);
}
}
}

}
}

How about:

ArrayList checkBoxText = new ArrayList();
GetCheckBoxText(this, checkBoxText);

where

private void GetCheckBoxText(Control ctl, ArrayList list)
{
CheckBox chk = ctl as CheckBox;
if (chk == null)
{
foreach (Control containedControl in ctl.Controls)
{
GetCheckBoxText(containedControl, list);
}
}
else
{
list.Add(chk.Text);
}
}
 
J

Jon Skeet [C# MVP]

Hello Everyone,
I would just like some advice to make sure i am doing what i want to
do the right way because i don't think i am.

I have a form that contains checkboxes. The checkboxes are created
dynamically from a database. When the user presses the submit button i
want to loop through all of the checkboxes and fill an IList with the
text from the checkboxes that are checked. Here is how i do it.. it
looks a bit convoluted , is there a better way of doing this?

Well, every time you're calling:

if (foo.GetType()==typeof(Bar))

it would be better to use

if (foo is Bar)

or

Bar bar = foo as Bar;
if (bar != null)

Reasons:
1) It's faster
2) It's more idiomatic
3) It works with polymorphism (eg for CheckBox, it'll still pick up the
case when you've got WhizzyDerivedCheckBox instead of just CheckBox).
 
E

esebastian

That worked beautifully, thank you so much for the code example! I
love asking for opinions as i feel it helps me grow as a developer.
I did add a small section in the else:
if(chk.checked)
{


}

around the list.add because i only want the text of the check boxes
whose checked values are checked.

Thanks so much!
Erin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top