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

发布于 2024-08-16 16:56:37 字数 199 浏览 2 评论 0 原文

我正在使用带有多行和全行选择的 ListView 控件。当我一次选择多行时,我的一些行会神奇地被选中。当将鼠标拖过以及选择一个并按住 Shift 键单击另一个时,会发生这种情况。

请参阅此处描述问题的图片: alt text

葡萄柚发生了什么?有人吗?

I'm using a ListView control with multirow and fullrow select on. When I'm selecting multiple rows at once, some of my rows magically become checked. This happens when dragging the mouse over and also when selecting one, and shift clicking another.

See image describing issue here: alt text

What in the grapefruit is going on? Anyone?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小梨窩很甜 2024-08-23 16:56:37

不幸的是,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;
    }
}

Unfortunately there are bugs in the ListView class, this is one of them. The following code is a fix that worked for me.

Edit: Sorry, this doesn't work quite right, although it does prevent the error that you show in your question. This prevents selecting multiple items and then checking them by clicking the check box.

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;
    }
}
白云悠悠 2024-08-23 16:56:37

这是一个简单的问题
试试这个

private void listView1_ItemCheck(object sender, ItemCheckEventArgs e)
{
    if (ModifierKeys == Keys.Control || ModifierKeys == Keys.Shift)
    {
        e.NewValue = e.CurrentValue;
    }
}

it`s simple question
just try this

private void listView1_ItemCheck(object sender, ItemCheckEventArgs e)
{
    if (ModifierKeys == Keys.Control || ModifierKeys == Keys.Shift)
    {
        e.NewValue = e.CurrentValue;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文