【发布时间】:2023-03-19 22:36:01
【问题描述】:
我有一个带有 5 个 CheckBox 的 checkedListBox,我希望第一个是“All”。 我写下了这段代码,但我得到了一个无限循环:
private void chkLstBx_ItemCheck(object sender, ItemCheckEventArgs e)
{
// ----- Get the name of the CheckBox that's changed: -----
string selected = chkLstBx.SelectedItem + "";
// ----- If "All" changed: -----
if (selected.Equals("All"))
// ----- to TRUE(from unchecked): -----
if (("" + (chkLstBx.GetItemCheckState(0))).Equals("Unchecked"))
for (int i = 1; i < chkLstBx.Items.Count; i++)
**chkLstBx.SetItemChecked(i, true);**
else // ----- to FALSE(from checked): -----
for (int i = 1; i < chkLstBx.Items.Count; i++)
chkLstBx.SetItemChecked(i, false);
// -----------------------------------------------
// -------------- REST OF CODE HERE --------------
// -----------------------------------------------
}
粗线 (**) 不幸地再次调用“chkLstBx_ItemCheck”......递归......导致无限循环,其中选定的始终是“全部”,仍然是“未选中”,并且 i 再次从 1 开始。 我该如何解决这个问题?
【问题讨论】:
标签: c# winforms checkedlistbox