C# ListView with CheckBoxes,多选行时自动选中复选框

标签 c# winforms listview checkbox multi-select

我正在使用带有多行和整行选择的 ListView 控件。当我一次选择多行时,我的一些行会神奇地变成选中状态。将鼠标拖到上方以及选择一个并单击另一个时会发生这种情况。

请在此处查看描述问题的图片:alt text

柚子里到底是怎么回事?有人吗?

最佳答案

不幸的是,ListView 类中存在错误,这是其中之一。以下代码是对我有用的修复。

编辑:抱歉,这不是很正确,尽管它确实可以防止您在问题中显示的错误。这可以防止选择多个项目,然后通过单击复选框来检查它们。

void SetupListView()
{
    listView.ItemCheck += new ItemCheckEventHandler(listView_ItemCheck);
    listView.MouseDown += new MouseEventHandler(listView_MouseDown);
    listView.MouseUp += new MouseEventHandler(listView_MouseUp);
    listView.MouseLeave += new EventHandler(listView_MouseLeave);
}

bool mouseDown = false;
void listView_MouseLeave(object sender, EventArgs e)
{
    mouseDown = false;
}

void listView_MouseUp(object sender, MouseEventArgs e)
{
    mouseDown = false;
}

void listView_MouseDown(object sender, MouseEventArgs e)
{
    mouseDown = true;
}

void listView_ItemCheck(object sender, ItemCheckEventArgs e)
{
    if(mouseDown)
    {
        e.NewValue = e.CurrentValue;
    }
}

关于C# ListView with CheckBoxes,多选行时自动选中复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2017170/

相关文章:

powershell - 如何引用列表框项目

android - 如何根据升序整数对sqlite表进行排序?

android - 具有 ViewHolder 模式的 ListView 中的远程图像

android - findviewbyid() 返回 null - 在自定义 CursorAdapter bindview() 中

ZeroMemory 的 C# 等效代码

c# - 创建现有程序集/模块的动态副本

c# - 无法使 Polly 超时策略覆盖 HttpClient 默认超时

c# - 在 Windows 窗体中启用滚动条

c# - 如何正确隐藏按钮并在后台工作人员执行某些操作后显示它?

c# - System.AccessViolationException 与 SQLite