简体   繁体   English

复选框中的C#

[英]Check boxes in C#

How to check the box dynamically without firing check changed function? 如何动态检查框而不激活检查更改功能? I have 20 checkboxes that are dynamically created and I have a drop-down that determines how many checkboxes are to be checked. 我有20个动态创建的复选框,我有一个下拉列表,用于确定要检查的复选框数。

If I selected 3 and click on 6th check box, that should check checkboxes 9, 7 and 8. In this process I don't want to fire checkchanged function. 如果我选中3并单击第6个复选框,则应选中复选框9,7和8.在此过程中,我不想触发checkchanged功能。

CheckBox cb1 = (CheckBox)sender;
selectedbox = int.Parse(cb1.Name);

for (int i = 1; i < selectedquantity; i++)
{
    premiumticket[selectedbox].Checked = true; 
    //here check changed firing  i dont want that
    selectedbox++;
}

You can't stop CheckBox from firing event when it's state changed. 当状态发生变化时,您无法阻止CheckBox触发事件。 Consider to either unsubscribe from event when you don't need it: 考虑在您不需要时取消订阅活动:

for (int i = 0; i < selectedquantity; i++)
{
     premiumticket[selectedbox + i].CheckedChanged -= checkBox_CheckedChanged;
     premiumticket[selectedbox + i].Checked = true;
     premiumticket[selectedbox + i].CheckedChanged += checkBox_CheckedChanged;
}

or use some flag to omit handling of event if you don't need it: 如果您不需要,可以使用一些标志来省略事件处理:

flag = false;

for (int i = 0; i < selectedquantity; i++)    
     premiumticket[selectedbox + i].Checked = true;         

flag = true;

And in handler: 在处理程序中:

private void checkBox_CheckedChanged(object sender, EventArgs e)
{
    if (!flag)
       return;

    //...
}

I can't see really solution how not to fire the event. 我无法看到真正的解决方案如何不发射事件。 You could disable the event with -= and then add it again with +=. 您可以使用 - =禁用该事件,然后使用+ =再次添加它。

 checkBox.CheckedChanged -= checkBox_CheckedChanged;
 checkBox.Checked = true;
 checkBox.CheckedChanged += checkBox_CheckedChanged;

The big disadvantag of this is that you would have to fire the event manually if you would like to have this event once, when you're updating the values. 这样做的一大不足之处在于,如果您希望在更新值时有一次此事件,则必须手动触发事件。

I'd rather set a flag in the class and check this flag in the update code. 我宁愿在类中设置一个标志,并在更新代码中检查此标志。

Remove the event attached then attach it after the you change the check : 删除附加的事件,然后在更改检查后将其附加:

{
    cb1.CheckedChanged -= new EventHandler(cb1_CheckedChanged);
    premiumticket[selectedbox].Checked = true;
    cb1.CheckedChanged += new EventHandler(cb1_CheckedChanged);
}



private void cb1_CheckedChanged(object sender, EventArgs e)
{
                // Some COde
}

Re-subscribing (unsubscribing/subscribing) technique is not very nice, because you have to deal with every event handler, making it common, adding flags, etc. 重新订阅(取消订阅/订阅)技术不是很好,因为你必须处理每个事件处理程序,使其通用,添加标志等。

What is really simple to do is to use another event - Click - to simply check changes to the control which are made by user : 真正简单的做法是使用另一个事件 - Click - 只需检查用户对控件的更改:

    private void checkBox1_Click(object sender, EventArgs e)
    {
        checkBox2.Checked = checkBox1.Checked;
        checkBox3.Checked = false;
    }
    private void checkBox2_Click(object sender, EventArgs e)
    {
        // this is not called when you set "Checked" programmatically
    }

It is obvious, what you have to implement different pattern, to actually do something with data (save them or react on changes), if they are changed programmatically. 很明显,如果以编程方式更改,您需要实现不同的模式,实际对数据执行某些操作(保存或对更改做出反应)。 In above example, clicking checkBox2 will run handler, where you, to example, save something into configuration, while setting checkBox2.Checked = true , when clicking checkBox1 , will not. 在上面的示例中,单击checkBox2将运行处理程序,例如,您将某些内容保存到配置中,而设置checkBox2.Checked = true ,当单击checkBox1 ,则不会。

It may happens, what for some CheckBox 'es you will use only Click , for others - only CheckedChanged . 它可能会发生,对于某些CheckBox ,你只会使用Click ,对于其他人来说 - 只有CheckedChanged

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM