I have a program written in C# with a clear button, which needs to clear the content of the entire form. For the button I used on foreach for the radiobuttons and one for the checkboxes.
foreach (RadioButton rad in quiztabs.TabPages)
{
rad.Checked = false;
}
foreach (CheckBox chk in quiztabs.TabPages)
{
chk.Checked = false;
}
However, when I click the clear button I get an Unable to cast object of type System.Windows.Forms.TabPage to type System.Windows.Forms.RadioButton.
You misunderstand the behaviour of foreach: It won't filter all elements in TabPages, it will try to cast all of them.
If you want to filter, you can do so explicitly using LINQ:
foreach (RadioButton rad in quiztabs.TabPages.OfType<RadioButton>())
{
rad.Checked = false;
}
foreach (CheckBox chk in quiztabs.TabPages.OfType<CheckBox>())
{
chk.Checked = false;
}
However, this still won't solve your problem, since the TabPages collection only contains TabPage elements. What you probably want is something like this:
foreach (TabPage page in quiztabs.TabPages)
{
foreach (RadioButton rad in page.Controls.OfType<RadioButton>())
{
rad.Checked = false;
}
foreach (CheckBox chk in page.Controls.OfType<CheckBox>())
{
chk.Checked = false;
}
}
Use the OfType<T>() method:
foreach (RadioButton item in yourChildern.OfType<RadioButton>())
{
//your code
}
Use
quiztabs.TabPages.OfType<RadioButton>() and quiztabs.TabPages.OfType<CheckBox>() instead of quiztabs.TabPages in your code.
Alternativly;
foreach(Control c in this.Controls)
{
if(c is RadioButton)
{
c.Checked = false;
}
}
foreach(Control i in this.Controls)
{
if(i is CheckBox)
{
i.Checked = false;
}
}
Your problem is that .TabPages is a list of Tab Pages... I believe you want the controls on each tab page
foreach(TabPage t in quizTabs.TabPages)
{
foreach (RadioButton rad in t.Controls)
{
rad.Checked = false;
}
foreach (CheckBox chk in t.Controls)
{
chk.Checked = false;
}
}
The foreach needs to have an iterator (rad here) of the same type of whatever you want to iterate through (TabPages here):
So it would look something like:
foreach (TabPage tab in quiztabs.TabPages) {
Although TabPages would be whatever type of object that quiztabs.TabPages is.
Hope this makes sense!
Take one List<> of type RadioButton and all radiobutton in that list .
List<RadioButton> list = new List<RadioButton>();
list.Add(radioButton1);
list.Add(radioButton2);
list.Add(radioButton3);
foreach (RadioButton item in list)
{
if(tabControl1.SelectedTab==tabControl1.TabPages[tabControl1.SelectedIndex])
item.Checked = false;
}
because, tabs in not a radioButton, you should do this instead,
foreach(TabPage tp in quizTabs.TabPages)
{
foreach (RadioButton rad in tp.Controls.OfType<RadioButton>())
{
rad.Checked = false;
}
foreach (CheckBox chk in tp.Controls.OfType<CheckBox>())
{
chk.Checked = false;
}
}
Related
I would like to compress the executable code segment (see below). How to do this with a foreach or for-loop?
private void UncheckCheckBox()
{
CheckBox[] Three = new []
{
checkBox1,
checkBox2,
checkBox3
};
checkBox1.Tag = "str1";
checkBox2.Tag = "str2";
checkBox3.Tag = "str3";
if (!checkBox1.Checked)
{
listBox4.Items.Remove(checkBox1.Tag);
}
if (!checkBox2.Checked)
{
listBox4.Items.Remove(checkBox2.Tag);
}
if (!checkBox3.Checked)
{
listBox4.Items.Remove(checkBox3.Tag);
}
}
You can do it without the array. The Controls collection should have your checkboxes:
foreach (CheckBox checkBox in Controls.AsQueryable().OfType<CheckBox>().Where(c => !c.Checked).ToArray())
listBox4.Items.Remove(checkBox.Tag);
Or using your existing array:
foreach (CheckBox checkBox in Three.Where(c => !c.Checked).ToArray())
listBox4.Items.Remove(checkBox.Tag);
It's as simple as:
foreach(CheckBox cbx in Three)
{
if(! cbx.Checked)
listBox4.Items.Remove(cbx.Tag);
}
I was wondering why below did not work as expected...
if if-statement changed to (!ctrl.checked) it returns all radio-button's names)
myForm f = new myForm();
foreach (RadioButton ctrl in f.Controls.OfType<RadioButton>())
{
if (ctrl.Checked)
MessageBox.Show(ctrl.Name);
}
I also tried
foreach (Control c in f.controls)
if (c is radiobutton)
{
if (c.Checked)
{
messagebox.show(c.name);
}
when I then put all radio-buttons in a group-box and used below code:
foreach (RadioButton c in groupBox1.Controls)
{
if (c.Checked)
{
MessageBox.Show(c.Name);
}
}
it worked fine.
what's the difference here.
any help appreciated
I'm guessing that your radio button is the child of a control other than the form. You need to recursively search for the radio buttons.
public void DisplayRadioButtons()
{
Form f = new Form();
RecursivelyFindRadioButtons(f);
}
private static void RecursivelyFindRadioButtons(Control control)
{
foreach (Control childControl in control.Controls)
{
RecursivelyFindRadioButtons(childControl);
if (childControl is RadioButton && ((RadioButton) childControl).Checked)
{
MessageBox.Show(childControl.Name);
}
}
}
I have a C# form with some types of controls, i throught this code for loop all Labels and re-parent:
private void MakeAllLabelTrans(Control frm, Control parent)
{
foreach (Control ctl in frm.Controls)
{
if (ctl is Label)
{
ctl.Parent = parent;
// ctl.BackColor = Color.Transparent;
}
else if (ctl.HasChildren)
{
MakeAllLabelTrans(ctl, parent);
}
}
}
and call as: MakeAllLabelTrans(this, picBackground); in Form_Load event, but some label was missed (i have puted a messagebox in the loop body - it really not in the loop), but i don't know why?
You're changing the collection while enumerating it. This leads to some items being skipped.
You should rewrite this to first build a list of controls that will need to be reparented, and then reparent them in a second pass.
You should not modify the collection on which you are iterating.
private static IEnumerable<Labels> FindAllLabels(Control container)
{
foreach (Control ctl in container.Controls)
{
var lbl = ctl as Label;
if (lbl != null)
{
yield return ctl;
}
else if (ctl.HasChildren)
{
foreach (var innerResult in FindAllLabels(ctl))
yield return innerResult;
}
}
}
// now call the method like this if you want to change a property on the label
foreach (var label in FindAllLabels(theForm))
label.BackgroundColor = Color.White;
// or like this (note the .ToList()) if you want to move the labels around:
foreach (var label in FindAllLabels(theForm).ToList())
label.Parent = someOtherControl;
I have a Repeater control that is creating a dynamic amount of CheckBoxList controls and each list has a different set of ListItems.
I have done this part just fine, but the issue I'm having is how to save the checked states of these dynamically created boxes. I cannot find out how to get the list of these CheckBoxList controls.
Here's some pseudo-code of what I'm trying to do:
foreach (Item i in MyRepeater)
{
if (i.ItemType is CheckBoxList)
{
foreach (ListItem x in i)
{
update table set tiChecked = x.Checked
where table.id = i.id and table.typeid = x.id
}
}
}
I have the ID of the CheckBoxList and the ListItem corresponding to the IDs in the DB.
Edit:
Of course after I ask, I find it out. This seems to be getting me what I want
foreach (RepeaterItem tmp in rptReportList.Items)
{
if (tmp.ItemType == ListItemType.Item)
{
foreach (Control c in tmp.Controls)
{
if (c is CheckBoxList)
{
DisplayMessage(this, c.ID.ToString());
}
}
}
}
You need to look deeper than the RepeaterItem:
// repeater item
foreach (Control cr in MyRepeater.Controls)
{
// controls within repeater item
foreach (Control c in cr.Controls)
{
CheckBoxList chklst = c as CheckBoxList;
if (chklst != null)
{
foreach (ListItem i in chklst.Items)
{
string valueToUpdate = i.Value;
string textToUpdate = i.Text;
bool checkedToUpdate = i.Selected;
// Do update
}
}
}
}
I'm creating some checkbox's from codebehind (adding through Panel.Controls.Add()).
My question is: How can i modify the values?
I've already tried creating the control, use the method FindControl and them change some properties but with no sucess.
CheckBox c = new CheckBox();
c.FindControl("CheckBoxP");
c.Checked = true;
Any ideas? Thanks
CheckBox _C = (CheckBox)this.Controls.Find("checkBox1", true).FirstOrDefault();
if (_C != null)
{
_C.Checked = true;
}
replace the 'checkBox1' with the name of the desired control
Try something like this (assuming you're using Windows Forms):
foreach (Control c in this.Controls)
{
if (c.Name == "MyName" && c is CheckBox)
{
((CheckBox)c).Checked = true;
}
}