单击项目下方的控件时,OwnerDrawVariable ListBox 选择最后一个项目 [英] OwnerDrawVariable ListBox selects last item when clicking on control below items

查看:22
本文介绍了单击项目下方的控件时,OwnerDrawVariable ListBox 选择最后一个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序的 ListBox 中注意到了这个问题,然后确定 MSDN 上给出的 ListBox.MeasureItem 示例也遇到了同样的问题.

I noticed this problem in a ListBox in my application, and then determined that the example given on MSDN for ListBox.MeasureItem suffers from the same problem.

当您将 ListBox 的 DrawMode 设置为 OwnerDrawVariable 以处理 MeasureItem 事件(比如绘制具有增加高度的列表项)时,如果您单击最后一项下方的空白区域,控件将选择最后一项.

When you set a ListBox's DrawMode to OwnerDrawVariable in order to handle the MeasureItem event ( say to draw the list items with an increased height ), the control will select the last item if you click on the empty region below the last item.

我希望它在 DrawMode 设置为 OwnerDrawFixed 或 Normal 时的行为方式,并且如果用户单击项目列表下方的控件,则不会更改项目选择.

I would like it to behave the way it does when DrawMode is set to OwnerDrawFixed or Normal, and not change the item selection if the user clicks on the control below the list of items.

我尝试通过处理 MouseDown 事件来实现此行为,发现控件在触发 MouseDown 事件之前选择了最底部的项目.

I tried to achieve this behavior by handling the MouseDown event and found that the control selects the bottommost item before it fires the MouseDown event.

我想知道我是否需要对 ListBox 进行子类化,或者是否有更好的方法来做到这一点.

I wonder if I need to subclass ListBox, or if there is a better way to do this.

为了查看行为,来自 MSDN 的代码示例就足够了:

In order to see the behavior, the code sample from MSDN suffices:

https://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.measureitem(v=vs.110).aspx

推荐答案

这是 Win32 控件中的内置行为.本质上,当您单击 ListBox 时,会调用一个名为 IndexFromPoint(您可以通过控件实例自己调用)的方法来确定所选项目的索引.

This is built-in behavior down in the Win32 control. Essentially, when you click the ListBox, a method called IndexFromPoint (you can call this yourself via the control instance) is called to determine the index of the selected item.

我尝试完全控制该过程,但我找不到任何证据表明即使这样,当控件使用 DrawMode.OwnerDrawVariable.

I tried taking full control over the process, but I couldn't find any evidence that even then you can tell the difference between clicking on an actual item or not when the control is using DrawMode.OwnerDrawVariable.

因此,我相信您无法控制它.但你可能会躲避它:

As such, it is my belief that you cannot control it. But you might dodge it:

private void ListBox1_MouseDown(object sender, MouseEventArgs e)
{
    for (Int32 i = 0; i < ListBox1.Items.Count; i++)
    {
        var rect = ListBox1.GetItemRectangle(i);
        if (rect.Contains(e.Location))
            return;
    }
    ListBox1.SelectedIndex = -1;
}

如您所见,在此事件触发时,该项目已被选中.这只是试图以足够快的速度取消选择它,以免引起注意.如果 ListBox 中有很多项目,您可能会注意到它在闪烁.

As you noticed, the item will already be selected by the time this event fires. This simply tries to un-select it fast enough to not be noticeable. If the ListBox has a lot of items in it, you might notice it as a flicker.

这可能是你能做的最好的了.

This is probably the best you could do.

这篇关于单击项目下方的控件时,OwnerDrawVariable ListBox 选择最后一个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆