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