简体   繁体   English

试图在单击单个复选框时在选中的列表框中选择所有项目

[英]Trying to select all items in checked list box on clicking single checkbox

I'm trying to select all items in checked list box when check box All is checked" How to get that, here is my code 我试图在选中“所有复选框”时在选中的列表框中选择所有项目。“如何获取此代码,这是我的代码

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
    if (cbAll.Checked)
    {
        if(clbViruslist.Items.Count > 0)
        {
           // here clbViruslist is the checked list o
           // for(int i=0;i<clbViruslist.Items.Count;i++)
           // clbViruslist.SetSelected(i,true);
           // clbViruslist.SetSelected(0,true ) ;
        }
     }
 }
private void cbAll_CheckedChanged(object sender, EventArgs e)
    {
        if (cbAll.Checked)
        {
            foreach (ListItem item in clbViruslist.Items)
            {
                item.Selected = true;                
            }
        }
    }

or this is much better 或者这更好

private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
       foreach (ListItem item in clbViruslist.Items)
       {
           item.Selected = checkBox1.Checked;                
       }

    }

Handle the CheckedChanged event of 'Select All' checkBox. 处理“全选”复选框的CheckedChanged事件。 In this, loop through all the items of checkedListBox and check them. 在此过程中,遍历checkedListBox的所有项目并进行检查。

private void checkBoxAll_CheckedChanged(object sender, EventArgs e)
{
    if (checkBoxAll.Checked)
        for (int i=0; i <= clbViruslist.Items.Count; i++)
            clbViruslist.SetItemChecked(i, true);
}

If you want to uncheck all checkedListBox items on unchecking the 'Select All' checkbox, use this: 如果要取消选中“全选”复选框,则取消选中所有selectedListBox项,请使用以下命令:

private void checkBoxAll_CheckedChanged(object sender, EventArgs e)
{
    if (checkBoxAll.Checked)
        for (int i=0; i <= clbViruslist.Items.Count; i++)
            clbViruslist.SetItemChecked(i, true);
    else
        for (int i=0; i <= clbViruslist.Items.Count; i++)
            clbViruslist.SetItemChecked(i, false);
}

You may also want to uncheck the 'Select All' checkBox, if any of the checkedListBox items is unchecked. 如果未选中任何checkedListBox项目,则可能还需要取消选中“全选”复选框。 For this, handle the ItemCheck event of checkedListBox and uncheck 'Select All' checkBox if any of the items is unchecked. 为此,如果未选中任何项,则处理checkedListBox的ItemCheck事件,并取消选中“全选”复选框。

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

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