2

I'm terribly inept at programming, and everything I know I learned from googling.

I cannot figure out the right terms to search to find my answer, I know I have seen it before if someone could just tell me the name of what I'm trying to accomplish and a very basic example i would greatly appreciate it.

Here is my code,

if (metroButton2.Text == "Check All")
{
    metroCheckBox1.CheckState = CheckState.Checked;
    metroCheckBox2.CheckState = CheckState.Checked;
    //Insert 58 Lines
    metroCheckBox61.CheckState = CheckState.Checked;
    metroCheckBox62.CheckState = CheckState.Checked;
    metroButton2.Text = "Uncheck All";
    metroTabPage1.Focus();
}
else if (metroButton2.Text == "Uncheck All")
{
    metroCheckBox1.CheckState = CheckState.Unchecked;
    metroCheckBox2.CheckState = CheckState.Unchecked;
    //Insert 58 Lines
    metroCheckBox61.CheckState = CheckState.Unchecked;
    metroCheckBox62.CheckState = CheckState.Unchecked;
    metroButton2.Text = "Check All";
    metroTabPage1.Focus();
}

Please, and thank you for your time.

I dont see how finding the control name is what i am trying to do here, i know all the control names.

What im trying to do is condense all of this

            if (metroButton2.Text == "Uncheck All")
            {
                metroCheckBox1.CheckState = CheckState.Unchecked;
                metroCheckBox2.CheckState = CheckState.Unchecked;
                metroCheckBox3.CheckState = CheckState.Unchecked;
                metroCheckBox4.CheckState = CheckState.Unchecked;
                metroCheckBox5.CheckState = CheckState.Unchecked;
                metroCheckBox6.CheckState = CheckState.Unchecked;
                metroCheckBox7.CheckState = CheckState.Unchecked;
                metroCheckBox8.CheckState = CheckState.Unchecked;
                metroCheckBox9.CheckState = CheckState.Unchecked;
                metroCheckBox10.CheckState = CheckState.Unchecked;
                metroCheckBox11.CheckState = CheckState.Unchecked;
                metroCheckBox12.CheckState = CheckState.Unchecked;
                metroCheckBox13.CheckState = CheckState.Unchecked;
                metroCheckBox14.CheckState = CheckState.Unchecked;
                metroCheckBox15.CheckState = CheckState.Unchecked;
                metroCheckBox16.CheckState = CheckState.Unchecked;
                metroCheckBox17.CheckState = CheckState.Unchecked;
                metroCheckBox18.CheckState = CheckState.Unchecked;
                metroCheckBox19.CheckState = CheckState.Unchecked;
                metroCheckBox20.CheckState = CheckState.Unchecked;
                metroCheckBox21.CheckState = CheckState.Unchecked;
                metroCheckBox22.CheckState = CheckState.Unchecked;
                metroCheckBox23.CheckState = CheckState.Unchecked;
                metroCheckBox24.CheckState = CheckState.Unchecked;
                metroCheckBox25.CheckState = CheckState.Unchecked;
                metroCheckBox26.CheckState = CheckState.Unchecked;
                metroCheckBox27.CheckState = CheckState.Unchecked;
                metroCheckBox28.CheckState = CheckState.Unchecked;
                metroCheckBox29.CheckState = CheckState.Unchecked;
                metroCheckBox30.CheckState = CheckState.Unchecked;
                metroCheckBox31.CheckState = CheckState.Unchecked;
                metroCheckBox32.CheckState = CheckState.Unchecked;
                metroCheckBox33.CheckState = CheckState.Unchecked;
                metroCheckBox34.CheckState = CheckState.Unchecked;
                metroCheckBox35.CheckState = CheckState.Unchecked;
                metroCheckBox36.CheckState = CheckState.Unchecked;
                metroCheckBox37.CheckState = CheckState.Unchecked;
                metroCheckBox38.CheckState = CheckState.Unchecked;
                metroCheckBox39.CheckState = CheckState.Unchecked;
                metroCheckBox40.CheckState = CheckState.Unchecked;
                metroCheckBox41.CheckState = CheckState.Unchecked;
                metroCheckBox42.CheckState = CheckState.Unchecked;
                metroCheckBox43.CheckState = CheckState.Unchecked;
                metroCheckBox44.CheckState = CheckState.Unchecked;
                metroCheckBox45.CheckState = CheckState.Unchecked;
                metroCheckBox46.CheckState = CheckState.Unchecked;
                metroCheckBox47.CheckState = CheckState.Unchecked;
                metroCheckBox48.CheckState = CheckState.Unchecked;
                metroCheckBox49.CheckState = CheckState.Unchecked;
                metroCheckBox50.CheckState = CheckState.Unchecked;
                metroCheckBox51.CheckState = CheckState.Unchecked;
                metroCheckBox52.CheckState = CheckState.Unchecked;
                metroCheckBox53.CheckState = CheckState.Unchecked;
                metroCheckBox54.CheckState = CheckState.Unchecked;
                metroCheckBox55.CheckState = CheckState.Unchecked;
                metroCheckBox56.CheckState = CheckState.Unchecked;
                metroCheckBox57.CheckState = CheckState.Unchecked;
                metroCheckBox58.CheckState = CheckState.Unchecked;
                metroCheckBox59.CheckState = CheckState.Unchecked;
                metroCheckBox60.CheckState = CheckState.Unchecked;
                metroCheckBox61.CheckState = CheckState.Unchecked;
                metroCheckBox62.CheckState = CheckState.Unchecked;
                metroButton2.Text = "Check All";
                metroTabPage1.Focus();
            }

Everything i have works perfectly as is, i would just like to know how to condense all of this, in the way i requested, i have seen it done in other languages, do i have to make a string first and execute that? i want to execute one small block of code, that will uncheck each of those boxes. If i could just get a simple example like

if (1<2)
{
    for (int i = 1; i < 62; i++)
    {
        var myVariable = i;
        metroCheckBox["myVariable"].CheckState = CheckState.Unchecked;
    }
}

I would really like to figure out how to accomplish something like this

Please and thank you.

Also, i am using windows form application for this i dunno if that helps, this does seem like a basic thing i am asking, im just too stupid to find the answer myself.

tabby
  • 1,878
  • 1
  • 21
  • 39
  • good effort on your first question - keep it up! just try to line up your code formatting a bit better of the OCD inflicted. – jazb Oct 24 '18 at 08:14
  • 5
    Do you have *Win Forms*, *WPF*, something else UI? – Dmitry Bychenko Oct 24 '18 at 08:16
  • Just one thing: Do not repeat code in both sections of the "if" statement. Write instead a single function called "SetCheckButtonStates( CheckState checkState )" and pass in the needed state. – Trap Oct 24 '18 at 08:32
  • I really do not see how this is a duplicate of the linked answer can someone please explain this to me? – Onelostpuppy Oct 24 '18 at 09:00
  • Yeah, it was a duplicate if it was "I want to get a control by name" (a not *unreasonable* "surface" reading of the question, especially if just looking at the last code sample), but not if it was "I've got a tonne of controls which I want to interact with as a group", which is how I've read your question, so removed the dupe flag. – Damien_The_Unbeliever Oct 24 '18 at 10:12

3 Answers3

0

You can check and uncheck all Checkbox controls like below

For Check

foreach(Control control in this.Controls)
{
     if(control.GetType() == typeof(CheckBox) && control.Name.Contains("metroCheckBox"))
     {
          ((CheckBox)control).CheckState = CheckState.Checked;
     }
}

For UnCheck

foreach(Control control in this.Controls)
{
     if(control.GetType() == typeof(CheckBox) && control.Name.Contains("metroCheckBox"))
     {
          ((CheckBox)control).CheckState = CheckState.Unchecked;
     }
}
Onur Tekir
  • 220
  • 1
  • 3
  • this will only check/uncheck `CheckBoxex` that are parented by `this` If you have a `CheckBox` on a `Panel` it will not be found by this code – GuidoG Oct 24 '18 at 12:52
  • @onurTekir: To add something for lazy people: if (control.GetType() == typeof(Checkbox) ... could be shortend to if (control is CheckBox .... – nabuchodonossor Oct 24 '18 at 15:07
  • @GuidoG: Next week he will start with recursion: Iterate through all controls, check if there is a Controls ... or see my answer below – nabuchodonossor Oct 24 '18 at 15:12
0

Create a field in your class to hold reference to all of the controls that you wish to toggle together (note that you can do this multiple times if you want to have "All in this group" style functionality as well as "All"):

private List<CheckBox> _allCheckboxes;

Then populate it once. Usually straight after InitialiseComponents has been called is a good spot:

_allCheckBoxes = new List<CheckBox>{
  metroCheckBox1,
  metroCheckBox2,
  /* ... */
};

In the alternative, if it really is all CheckBox controls on the form, something like this should do:

_allCheckBoxes = this.Controls.OfType<CheckBox>().ToList();

(using LINQ) should do the trick).

Now, when you want to toggle all of the controls, you can use a foreach statement to iterate through each control and set it's CheckState property (and note that we no longer have to know that there are e.g. 62 items if we use a foreach)

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
0

To show, what Guido stated, for accessing them when in sub panels, a very simple approach:

            if (c is CheckBox && c.Name.StartsWith("metroCheck"))
            {
                ((CheckBox)c).CheckState = CheckState.Checked;
            }
            else if (c.HasChildren)
            {
                foreach (Control cc in c.Controls)
                {
                    if (cc is CheckBox && cc.Name.StartsWith("metroCheck"))
                    {
                        ((CheckBox)cc).CheckState = CheckState.Checked;
                    }
                }
            }

Thanks, Guido

nabuchodonossor
  • 2,095
  • 20
  • 18