avirk
avirk

Reputation: 3086

limited check the CheckBox in c#

I made a win form and give some checkboxes that show the courses avilable for the student and by checing the checkbox he will able to tell that which courses he want to do but i want to restirct him when he check the three courses and if he try to check the fourth checkbox then the message box should appear to tell him that he can't select more than three and the fourth checkbox should not be check. So how can I do it?

Upvotes: 3

Views: 5072

Answers (5)

Nathan Baulch
Nathan Baulch

Reputation: 20703

I would recommend you use a CheckedListBox when dealing with an unknown number of options. Here is a complete example that shows how to use the ItemCheck event to restrict the total number of selections.

var box = new CheckedListBox
    {
        Dock = DockStyle.Fill,
        CheckOnClick = true
    };
box.ItemCheck += (sender, e) =>
    {
        // is the item being checked when 3 are already checked?
        if (e.NewValue == CheckState.Checked && box.CheckedItems.Count == 3)
        {
            // block the change
            e.NewValue = CheckState.Unchecked;
        }
    };
for (var i = 0; i < 10; i++)
{
    box.Items.Add("item " + i);
}
new Form {Controls = {box}}.ShowDialog();

Edit: You can create a grouped appearance by removing the list's border, nesting it inside a GroupBox and changing the background color.

box.BorderStyle = BorderStyle.None;
box.BackColor = Control.DefaultBackColor;
box.MultiColumn = true;
box.IntegralHeight = false;
var group = new GroupBox
    {
        Text = "Options",
        Dock = DockStyle.Fill,
        Controls = {box}
    };

Group Check Box

Upvotes: 4

CharithJ
CharithJ

Reputation: 47570

You will have to fire the same event handler for all the CheckedBoxes CheckChanged events.

CheckBox1.CheckedChanged += new System.EventHandler(MyCheckedChanged)
CheckBox2.CheckedChanged += new System.EventHandler(MyCheckedChanged)

private void MyCheckedChanged(object sender, EventArgs e)
{
      CheckBox checkbox = sender as CheckBox;

      // Check all the checked items. If already 3 selectec
      checkbox.Checked = false;
 }

Upvotes: 2

BitKFu
BitKFu

Reputation: 3697

You have to implement an OnCheckedChange Handler and set this to every checkbox. This increases / decreases a counter, depending on the state of the checkbox.

    private int checkCounter;

    private void OnCheckedChanged(object sender, EventArgs e)
    {
        // Increase or decrease the check counter
        CheckBox box = (CheckBox) sender;
        if (box.Checked)
            checkCounter++;
        else
            checkCounter--;

        // prevent checking
        if (checkCounter == 4)
        {
            MessageBox.Show("YOU ARE EVIL", "Bad");
            box.Checked = false;
        }

    }

If your count is reached, you can output a message box and reset the checkbox. That's all.

And don't forget to apply this checkhandler to all CheckedChange events of the checkboxes.

Upvotes: 4

AK.
AK.

Reputation: 763

Assuming you are using WinForms, the CheckBox control has a OnCheckStateChanged event. Wire the events of all your check boxes to call another function. In this function, you can track the total number of boxes checked and display a pop-up to let the user know that the max has been reached.

Upvotes: 1

JohnD
JohnD

Reputation: 4002

Use the checkbox's CheckChanged event and count the current number of checked checkboxes on the form, then show a messagebox and uncheck the checkbox that triggered the event if it currently over 3.

Upvotes: 1

Related Questions