.net - c# 检查列表框 MouseMove 与 MouseHover 事件处理程序

标签 .net winforms checkedlistbox mousemove mousehover

我使用以下MouseMove事件处理程序将文本文件内容显示为CheckedListBox上的工具提示,并且每个checkedListBoxItem都有一个标记的文本文件对象。

private void checkedListBox1_MouseMove(object sender, MouseEventArgs e)
        {
            int itemIndex = checkedListBox1.IndexFromPoint(new Point(e.X, e.Y));

            if (itemIndex >= 0)
            {
                if (checkedListBox1.Items[itemIndex] != null)
                {
                    TextFile tf = (TextFile)checkedListBox1.Items[itemIndex];

                    string subString = tf.JavaCode.Substring(0, 350);

                    toolTip1.ToolTipTitle = tf.FileInfo.FullName;
                    toolTip1.SetToolTip(checkedListBox1, subString + "\n... ... ...");
                }
            }
        }

问题是,由于鼠标在 checkListBox 上频繁移动,我的应用程序速度变慢。

作为替代方案,我认为我应该使用 MouseHover 事件及其处理程序。但我无法找到我的 musePointer 当前位于哪个 checkListBoxItem 上。像这样:

private void checkedListBox1_MouseHover(object sender, EventArgs e)
        {
            if (sender != null)
            {
                CheckedListBox chk = (CheckedListBox)sender;

                int index = chk.SelectedIndex;

                if (chk != null)
                {
                    TextFile tf = (TextFile)chk.SelectedItem;

                    string subString = tf.FileText.Substring(0, 350);

                    toolTip1.ToolTipTitle = tf.FileInfo.FullName;
                    toolTip1.SetToolTip(checkedListBox1, subString + "\n... ... ...");
                }
            }
        }

此处 int index 返回 -1,chk.SelectedItem 返回 null

解决此类问题的方法是什么?

最佳答案

在 MouseHover 事件中,您可以使用 Cursor.Position property并将其转换为客户端位置并传递给 IndexFromPoint() 以确定它是否包含在哪个列表项中。

例如。

 Point ptCursor = Cursor.Position; 
 ptCursor = PointToClient(ptCursor); 
 int itemIndex=checkedTextBox1.IndexFromPoint(ptCursor);
 ...
 ...

这对于其他事件也很有用,在这些事件中,您不会在事件参数中获得鼠标位置。

关于.net - c# 检查列表框 MouseMove 与 MouseHover 事件处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1550037/

相关文章:

Powershell CheckedListBox 检查是否在字符串/数组中

c# - 带有 TextBox 的 CheckedListBox 过滤器

c# - 关于两种不同类扩展模式的问题

c# - 输入语言 Hook

c# - 如何在 .NET 4 中将程序集设置为 "fully trusted"?

winforms - 未从 Vista 中的 machine.config 读取 appSettings

c# - .net c# asp.net 使文本框文本的一部分加粗

C# Windows.Forms.WebBrowser 缩放

c# - 绘制的线不显示在组合框中

c# - 如何将 CheckedListBox 项目设置为默认选中