繁体   English   中英

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

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

我试图在选中“所有复选框”时在选中的列表框中选择所有项目。“如何获取此代码,这是我的代码

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;                
            }
        }
    }

或者这更好

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

    }

处理“全选”复选框的CheckedChanged事件。 在此过程中,遍历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);
}

如果要取消选中“全选”复选框,则取消选中所有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);
}

如果未选中任何checkedListBox项目,则可能还需要取消选中“全选”复选框。 为此,如果未选中任何项,则处理checkedListBox的ItemCheck事件,并取消选中“全选”复选框。

暂无
暂无

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

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