浏览一个选中的列表框并检查所有项目C#

use*_*653 9 c# loops checkedlistbox

我需要循环检查列表框,对于其中的每个项目,我需要检查它们(基本上像"全选"功能).

有没有一个基本的例子可以帮我解决一下?

SwD*_*n81 22

使用SetSelected和互动通过所有Items

// Loop through and set all to selected.
for (int x = 0; x < listBox1.Items.Count; x++)
{
   listBox1.SetSelected(x, true);
}
Run Code Online (Sandbox Code Playgroud)

要检查项目,请使用 SetItemChecked

// Loop through and set all to checked.
for (int x = 0; x < listBox1.Items.Count; x++)
{
   listBox1.SetItemChecked(x, true);
}
Run Code Online (Sandbox Code Playgroud)


Kha*_*han 5

您可以将所有项目查看为ListItems:

foreach (ListItem li in CheckBoxList1.Items)
{
    li.Selected = true;
}
Run Code Online (Sandbox Code Playgroud)

  • 匿名添加:( ListItem将需要对System.Web.UI.WebControls的引用) (4认同)