Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have around 40 CheckBoxes on my WinForm. Each CheckBox has its unique CheckBox.Text. A ListBox has 15 text items. which are same as for 15 CheckBoxes. I want to check only those CheckBoxes whose Name are in ListBox. And I want to use foreach statement for this CheckBox coding. Can anyone help me with this code ASAP.
Posted
Comments
Samira Radwan 8-Sep-15 13:38pm    
you should use CheckBoxList instead of checkboxes to be able to use foreach

Iterate on check boxes (see, for instance How can I iterate through all checkboxes on a form?[^]) and check only the ones whose text is found in the list box (see "ListBox.FindStringExact Method (String)"[^]).
 
Share this answer
 
able to solve my problem thnx for the suggestion's.

C#
str = ds.Tables[0].Rows[0]["genere"].ToString();
    str = str.Replace(",", "");
    temptextbox.Text = str;
    string[] stringArray = str.Split(' ');
    templistbox.Items.AddRange(stringArray);
    for (int i = 0; i < templistbox.Items.Count; i++)
    {
        templistbox.Text = templistbox.Items[i].ToString();
        str = templistbox.Text;
        foreach (Control c in panel6.Controls)
        {
            if (c is CheckBox)
            {
                if (c.Text == str)
                {
                    CheckBox temp = (CheckBox)c;
                    temp.Checked = true;

                }
            }
        }
    }
 
Share this answer
 
Answer 3 Will Be Helpful to you
 
Share this answer
 
First of all, I would seriously rethink the UI design which has 40 check boxes. It could be some tree view with check boxed on nodes, DataGridView, CheckedListBox, or something else, and even then 40 check boxes would look suspicious.

Anyway, you can change the state of check box using this property: System.Windows.Forms.CheckBox.Checked:
https://msdn.microsoft.com/en-us/library/system.windows.forms.checkbox.checked%28v=vs.110%29.aspx[^].

You can do it in cycle in different ways. If you don't have an array or collection of check boxed, just make it. It would be better to add all of them in code, not in the designer, which would mean a lot of dumb manual clicks, instead of just one loop. Alternatively, add all of them in some container control, such as Panel and traverse its collection of chold controls referenced by its Controls property: https://msdn.microsoft.com/en-us/library/system.windows.forms.control.controls%28v=vs.110%29.aspx[^].

In a loop, you can use an "if" block and check up the values of the Name property. However, the whole idea is also bad: the property Name is just to support the designer, is meaningless during runtime and should better be kept meaningless.

Again, the impression is that you need to create much nicer UI design, according to your ultimate goals.

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900