C# 如何通过按一选中所有复选框?

C# 如何通过按一选中所有复选框?,c#,winforms,checkbox,C#,Winforms,Checkbox,我想通过从ListView中选择一个复选框来生成一个代码,以自动从ListView中选择所有复选框 我使用的是VisualStudio2005,所以我没有ItemChecked表单。 这就是为什么我想使用ListView itemcheck事件来实现这一点。这是我的密码 你能帮我填一下吗?或者,如果您有更好的想法,请分享:使用该属性: foreach(ListViewItem item in lv.Items) item.Selected = true; foreach(ListVi

我想通过从ListView中选择一个复选框来生成一个代码,以自动从ListView中选择所有复选框

我使用的是VisualStudio2005,所以我没有ItemChecked表单。 这就是为什么我想使用ListView itemcheck事件来实现这一点。这是我的密码

你能帮我填一下吗?或者,如果您有更好的想法,请分享:

使用该属性:

foreach(ListViewItem item in lv.Items)
    item.Selected = true;


foreach(ListViewItem item in lv.Items)
    item.Selected = !item.Selected;
使用属性:

foreach(ListViewItem item in lv.Items)
    item.Selected = true;


foreach(ListViewItem item in lv.Items)
    item.Selected = !item.Selected;

注意:很可能有更好的方法可以做到这一点,但这是我很久以前使用的一种模式,当时它就起作用了

在上面显示的事件中,将从listView调用它,ItemCheckEventArgs e将告诉您是否选中该框。它实际上会在检查之前告诉您复选框的状态。因此,如果复选框未选中,而用户刚刚选中了它,则e.CurrentValue将为CheckState.unchecked

现在,如果我们试图更新ItemCheck事件中所有复选框的状态,我们可能会遇到的问题是,我们将在每次选中复选框时递归调用该事件。解决此问题的一种方法是通过选中一个框来跟踪用户是否正在调用事件,或者通过调用item.Checked=true;来触发事件

像这样的事情可能会奏效:

// Set this to true when our code is modifying the checked state of a listbox item
private bool changingCheckboxState = false;

private void lvBase_ItemCheck(object sender, ItemCheckEventArgs e)
{
    // If our code triggered this event, just return right away
    if (changingCheckboxState) return;

    // Set our flag so that calls to this method inside the 
    // loop below don't trigger more calls to this method
    changingCheckboxState = true;

    // Set all the checkboxes to match the state of this one
    foreach(ListViewItem item in lvBase.Items)
    {
        item.Checked = e.CurrentValue != CheckState.Checked;
    }

    // Now that we're done, set our flag to false again
    changingCheckboxState = false;
}

注意:很可能有更好的方法可以做到这一点,但这是我很久以前使用的一种模式,当时它就起作用了

在上面显示的事件中,将从listView调用它,ItemCheckEventArgs e将告诉您是否选中该框。它实际上会在检查之前告诉您复选框的状态。因此,如果复选框未选中,而用户刚刚选中了它,则e.CurrentValue将为CheckState.unchecked

现在,如果我们试图更新ItemCheck事件中所有复选框的状态,我们可能会遇到的问题是,我们将在每次选中复选框时递归调用该事件。解决此问题的一种方法是通过选中一个框来跟踪用户是否正在调用事件,或者通过调用item.Checked=true;来触发事件

像这样的事情可能会奏效:

// Set this to true when our code is modifying the checked state of a listbox item
private bool changingCheckboxState = false;

private void lvBase_ItemCheck(object sender, ItemCheckEventArgs e)
{
    // If our code triggered this event, just return right away
    if (changingCheckboxState) return;

    // Set our flag so that calls to this method inside the 
    // loop below don't trigger more calls to this method
    changingCheckboxState = true;

    // Set all the checkboxes to match the state of this one
    foreach(ListViewItem item in lvBase.Items)
    {
        item.Checked = e.CurrentValue != CheckState.Checked;
    }

    // Now that we're done, set our flag to false again
    changingCheckboxState = false;
}


您应该在Items集合中循环,并为每个集合设置Checked属性。感谢您的回复!我不擅长C,但你是说lvBase.Items[I].Checked==true吗?我不知道在if中填写什么。。你能帮我吗?那是另一个问题。查看listview属性中的checkAll或其他内容-抱歉,我自己不确定您是否应该在Items集合中循环并为每个集合设置Checked属性。感谢您的回复!我不擅长C,但你是说lvBase.Items[I].Checked==true吗?我不知道在if中填写什么。。你能帮我吗?那是另一个问题。查看一下listview属性中的checkAll或其他内容-抱歉,我自己不确定是否要删除If-else并粘贴代码?它不起作用:看看这个问题:我正在显示Checked属性的设置。我不清楚用户是想从代码中全部选择还是全部取消选择。您能告诉我如何让ListView复选框“选择代码”来为您填写IFUpdate my Response-如果高兴,请单击勾号标记为已回答-谢谢您是否要删除if else并粘贴您的代码?它不起作用:看看这个问题:我正在显示Checked属性的设置。我不清楚用户是想从代码中全部选择还是全部取消选择。您能告诉我如何让ListView复选框选择代码来为您填写IFUpdate my Response-如果高兴,请单击勾号标记为已回答-谢谢!!谢谢你分享你的知识Rufus L:这很有效!!感谢您分享您的知识Rufus L: