有限检查在c# [英] limited check the CheckBox in c#

查看:158
本文介绍了有限检查在c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个胜利的表格,并给了一些复选框,显示学生可用的课程,并通过checing的复选框,他将能够告诉他要做什么课程,但我想restirct他,当他检查三个课程和如果他试图检查第四个复选框,那么消息框应该显示告诉他,他不能选择超过三个,第四个复选框不应该检查。

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?

推荐答案

您必须实现一个OnCheckedChange处理程序,并将其设置为每个复选框。
根据复选框的状态,增加/减少一个计数器。

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.

并且不要忘记将这个checkhandler应用到所有CheckedChange事件的复选框。

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

这篇关于有限检查在c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆