首页 > 解决方案 > 如何以编程方式取消选中checkedListBoxin ac#表单中的复选框?

问题描述

我想编写一个方法,根据条件,仅取消选中 c# 中复选框列表中的某个复选框。我已经为变量“money”分配了一个值,确定了列表中复选框的索引,并为它的值分配了一个名为“index”的全局变量。此外,为checkedList Name 输入的字符串与checkedListBox 的名称相同。

这是我的代码:

public void updateResources(String checkedListName, int cost)
        {
            if (money < cost)
            {
                if (checkedListName == "checkedListBoxBasics")
                {
                    //uncheck box at index
                }
                else if (checkedListName == "checkedListBoxConstruction")
                {
                    //uncheck box at index
                }

                //... more else if statements
            }
            else
            {
                //implement input variables with other external variables
            }
        }

标签: c#formscheckedlistbox

解决方案


如果您已经知道需要更改状态的 Checkbox 的索引,则可以使用CheckListBox.SetItemCheckState方法。例如,

 checkedListBoxBasics.SetItemCheckState(index, CheckState.Checked);

或取消选中

checkedListBoxBasics.SetItemCheckState(index, CheckState.Unchecked);

推荐阅读