Stephen Ó Connor
Stephen Ó Connor

Reputation: 376

C# winforms checkBox list or array

This is what I want to do in C# windows forms, however I am not sure how to do it. I want an array or list of checkBoxs 10 in total. When user clicks checkBox the button displays green and the letter B when user clicks again the button goes back to normal.
First question, am I doing this right, second question do I have to do this for each individual button? This is what I have so far.

checkBox1.Text = "1";
checkBox1.BackColor = BackColor;

if (checkBox1.Checked == true)
{
    checkBox1.Text = "B";
    checkBox1.BackColor = Color.Green;
}

Upvotes: 0

Views: 3020

Answers (5)

ryanyuyu
ryanyuyu

Reputation: 6486

Related to Arsen's answer, if you are comfortable with creating the Checkboxes programmatically, you can create them inside a loop. Then you can reuse a lot of code since the checkboxes are almost identical.

In the form load you could add:

for (int i = 1; i <= 10; i++)
{
    var checkbox = new CheckBox();
    checkbox.Name = String.Format("checkbox{0}", i);
    //Set the location.  Notice the y-offset grows 20px with each i
    checkbox.Location = new Point(50, 150 + (i*20)); 
    checkbox.Text = "1";
    checkbox.Checked = false;
    //Assign them all the same event handler
    checkbox.CheckedChanged += new EventHandler(checkbox_CheckedChanged); 
    //other checkbox considerations like checkbox.BringToFront()

    //very important, you must add the checkbox the the form's controls 
    this.Controls.Add(checkbox); 
}

And you would just have to define your event handler. Maybe something like this:

private void checkbox_CheckedChanged(object sender, EventArgs e)
{
    var checkBox = (CheckBox) sender;
    //no need to check bools against true or false, they evaluate themselves
    if (checkBox.Checked) 
    {
        checkBox.Text = "B";
        checkBox.BackColor = Color.Green;
    }
    else
    {
        checkBox.Text = "1";
        checkBox.BackColor = SystemColors.Control;
    }
}

Upvotes: 2

Zergling
Zergling

Reputation: 536

i think that is what you want

    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        var cb = (sender as CheckBox);

        if (cb.Checked == true)
        {
            cb.Text = "B";
            cb.BackColor = Color.Green;
        }
        else
        {
            cb.Text = "1";
            cb.BackColor = BackColor;
        }

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        foreach (var item in this.Controls)
        {
            if (item.GetType()== typeof(CheckBox))
            {
                (item as CheckBox).CheckedChanged += new EventHandler(checkBox1_CheckedChanged);
            }

        }
    }

Upvotes: 0

Arsen Mkrtchyan
Arsen Mkrtchyan

Reputation: 50712

Answer to 1st question: It depends. Personally I try not to use much code in UI part, but it depends from the application and how deep do you want to go with design. Overall this will work

Answer to the 2nd question: I would write a function, which take a checkbox and color and call this for all

public void InvalidateCheckboxAppearance(CheckBox cb)
{
   cb.Text = cb.Checked ? "B" : "1";
   cb.BackColor = cb.Checked ? Color.Green : BackColor;
}

Now you can point all Checked changed events to the same method and call this like

public void Cb_Changed(object sender, EventArgs args)
{
    InvalidateCheckboxAppearance(sender as CheckBox);
}

(Question is not clear, hope I catch the idea correctly)

Upvotes: 3

Rick
Rick

Reputation: 841

You can link all the checkboxes to the same event (via Properties -> Events) to avoid repeating code:

    private void checkBoxAll_CheckedChanged(object sender, EventArgs e) {
        var checkbox = (sender as CheckBox);
        if (checkbox.Checked == true) {
            checkbox.Text = "B";
            checkbox.BackColor = Color.Green;
        }
        else {
            checkbox.Text = "1";
            checkbox.BackColor = BackColor;
        }
    }

Upvotes: 0

Gino
Gino

Reputation: 176

You're doing it rigth.
You can improve your function by implementing the event and passing the checkbox.

//event Handler
function changeHandler(Object o, EventArgs e) handles MY event
{
changeHandler(o);
}
changeHandler(Object o)
{
//cast from object to checkbox
checkbox c= (checkbox)o;
if (checkBox1.Checked == true)
{
    checkBox1.Text = "B";
    checkBox1.BackColor = Color.Green;
}

}

Upvotes: 1

Related Questions