c# 检查列表框 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
。
此类问题的解决方案是什么?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 MouseHover 事件中,您可以使用 Cursor.Position属性 并将其转换为客户端位置并传递给 IndexFromPoint() 以确定它是否包含在哪个列表项中。
例如。
这对于其他事件也很有用,在这些事件中,您没有在事件参数中给出鼠标位置。
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.
This is useful for other events also, where you are not given the mouse position in the event parameters.
问题是因为 SelectedItem <> selectedItem,selected表示有另一个背景,checked表示左侧有勾选。
而不是
您应该使用:
然后显示您想要的内容(如果选择了它)...
The problem is because SelectedItem <> checkedItem, selected means have another background, checked means have check on the left side.
instead of
you should use:
then show what you want if it selected...