0

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;
}
1
  • Any item container like grid use and then item template modif it as checkbox Commented May 13, 2015 at 12:29

5 Answers 5

3

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)

Sign up to request clarification or add additional context in comments.

5 Comments

The only downside to doing it this way, is that there will be 10 events declared in the code. Seems a little un-DRY.
not necessarily, what about subscribing same method to all checkboxes? just 10 subscribers... it is also possible to loop and subscribe .. but that is another topic
I had read your code as not being able to handle >any< checkbox - my mistake. I would go with your suggestion then.
@ArsenMkrtchyan thanks for answering to my questions. I have just started with Visual studio and forms to create a win app. I find VS winforms UI a little bloated and inflexible but hey I am new to it so that could just be me.
check if your forms constructor does not throw exception, actually there are lots of reasons when designer fails to display... so hard to say here
2

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;
    }
}

Comments

1

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;
}

}

2 Comments

checkbox c= (checkbox)o; you mean CheckBox c= (CheckBox)o;?
Yes.... Don't take this as an answer please instead take it as a good advice to improve your code. (Consider it as pseudo code). Hope it helped
0

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;
        }
    }

Comments

0

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);
            }

        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.