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

发布于 2024-08-07 14:17:07 字数 1665 浏览 9 评论 0原文

我使用以下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

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

I am using the following MouseMove event-handler to show the text-file content as a tooltip on CheckedListBox and there is a text-file object tagged to each 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... ... ...");
                }
            }
        }

The problem is, my application is slowing down because of frequent mouse movements on the checkedListBox.

As an alternative, I thought, I should use MouseHover event and its handler. But I could not find out which checkedListBoxItem my musePointer is currently on. Like this:

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... ... ...");
                }
            }
        }

Here int index is returning -1 and chk.SelectedItem is returning null.

What can be the solution of this type of problem?

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

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

发布评论

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

评论(2

紫南 2024-08-14 14:17:07

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

例如。

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

这对于其他事件也很有用,在这些事件中,您没有在事件参数中给出鼠标位置。

In the MouseHover event you could use the Cursor.Position property and convert it to a client position and pass to IndexFromPoint() to determine if which list item it is contained within.

eg.

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

This is useful for other events also, where you are not given the mouse position in the event parameters.

横笛休吹塞上声 2024-08-14 14:17:07

问题是因为 SelectedItem <> selectedItem,selected表示有另一个背景,checked表示左侧有勾选。

而不是

 int index = chk.SelectedIndex;

您应该使用:

int itemIndex = checkedListBox1.IndexFromPoint(new Point(e.X, e.Y));
bool selected = checkedListBox1.GetItemChecked(itemIndex );

然后显示您想要的内容(如果选择了它)...

The problem is because SelectedItem <> checkedItem, selected means have another background, checked means have check on the left side.

instead of

 int index = chk.SelectedIndex;

you should use:

int itemIndex = checkedListBox1.IndexFromPoint(new Point(e.X, e.Y));
bool selected = checkedListBox1.GetItemChecked(itemIndex );

then show what you want if it selected...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文