gpt4 book ai didi

c# - C# 中的复选框

转载 作者:行者123 更新时间:2023-11-30 13:46:27 25 4
gpt4 key购买 nike

如何在不触发检查更改功能的情况下动态检查复选框?我有 20 个动态创建的复选框,我有一个下拉列表来确定要选中多少个复选框。

如果我选择 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++;
}

最佳答案

当 CheckBox 的状态发生变化时,您无法阻止它触发事件。考虑在不需要时取消订阅事件:

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

或者在不需要时使用一些标志来省略事件处理:

flag = false;

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

flag = true;

在处理程序中:

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

//...
}

关于c# - C# 中的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21087831/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com