簡體   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