简体   繁体   English

C# Checkedlistbox 如果选中

[英]C# Checkedlistbox if checked

Is it possible to apply.Checked== to checkedlistbox as in checkbox?是否可以像在复选框中那样将 .Checked== 应用于 checkedlistbox?

If to do it in a way as with checkbox it not works如果以与复选框一样的方式进行操作,则它不起作用

if(checkedListBox1.Items[2].Checked==true)
{
}

What you need is the method GetItemCheckState .您需要的是方法GetItemCheckState

Usage as follows:用法如下:

if(checkedListBox1.GetItemCheckState(2) == CheckState.Checked)
{

}

You can use it in this way你可以这样使用

if (checkedListBox1.CheckedItems.Contains("ItemWithIndex2"))
{
    MessageBox.Show("Test");
}

Try something like...尝试类似...

checkedListBox1.GetItemChecked(i)

foreach(int indexChecked in checkedListBox1.CheckedIndices) {
    // The indexChecked variable contains the index of the item.
    MessageBox.Show("Index#: " + indexChecked.ToString() + ", is checked. Checked state is:" +
                    checkedListBox1.GetItemCheckState(indexChecked).ToString() + ".");
}

GetItemCheckState() returns a boolean value. GetItemCheckState()返回一个布尔值。 So you can use as follows:所以你可以使用如下:

if(checkedListBox1.GetItemCheckState(index) == true)
{

}

where index is an integer value denoting the row index of CheckedListBox其中 index 是一个整数值,表示 CheckedListBox 的行索引

GetItemChecked() returns a boolean value. GetItemChecked()返回一个布尔值。 So you can use it as the following:因此,您可以将其用作以下内容:

if(checkedListBox1.GetItemChecked(index) == true) {

}

Where index is an integer value denoting the row index of checkedListBox1 .其中index是一个整数值,表示checkedListBox1的行索引。

you might be looking for something like this你可能正在寻找这样的东西

foreach(int i in checkedListBox1.SelectedIndices)
        {
            if(checkedListBox1.GetItemCheckState(i)!=CheckState.Checked)
            {
                ....
            }
        }

I ran into a similar issue: On a click of an item, the state should be converted from either checked/ non-checked to opposite.我遇到了一个类似的问题:单击一个项目时,state 应该从选中/未选中转换为相反。 Here i post the event and the check and change:我在这里发布事件以及检查和更改:

    CheckedListBox ChkLBox;
    private void CheckedListBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        int SelectedIndex = ChkLBox.SelectedIndex; //
        var Item = ChkLBox.Items[SelectedIndex];

        bool IsChecked = (ChkLBox.GetItemChecked(ChkLBox.SelectedIndex));
        ChkLBox.SetItemChecked(ChkLBox.Items.IndexOf(Item), !IsChecked);
    }

checkedListBox1.CheckedItems.Count>0

var itemChecked = checkedListBox1.GetItemChecked(checkedListBox1.SelectedIndex);

I'm not sure i understand your question, do you want to check if at least 1 item in the listbox is checked?我不确定我是否理解你的问题,你想检查列表框中是否至少有 1 个项目被选中? If so you could do that如果是这样,你可以这样做

if(checkedListBox1.Items.Any(item=>item.Checked))
{
}

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

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