我想扩展费尔明的答案,以便让他的奇妙解决方案更加清晰。
在您正在使用的表单中(可能在 .Designer.cs 文件中),您需要向 CheckedListBox 添加一个 MouseMove 事件处理程序(Fermin 最初建议使用 MouseHover 事件处理程序,但这对我不起作用)。
this.checkedListBox.MouseMove += new System.Windows.Forms.MouseEventHandler(this.showCheckBoxToolTip);
接下来,向表单添加两个类属性,一个 ToolTip 对象和一个整数,以跟踪显示工具提示的最后一个复选框
private ToolTip toolTip1;
private int toolTipIndex;
最后,您需要实现 showCheckBoxToolTip() 方法。这个方法和 Fermin 的回答很相似,只是我把事件回调方法和 ShowToolTip() 方法结合起来了。此外,请注意方法参数之一是 MouseEventArgs。这是因为 MouseMove 属性需要一个 MouseEventHandler,然后它提供 MouseEventArgs。
private void showCheckBoxToolTip(object sender, MouseEventArgs e)
{
if (toolTipIndex != this.checkedListBox.IndexFromPoint(e.Location))
{
toolTipIndex = checkedListBox.IndexFromPoint(checkedListBox.PointToClient(MousePosition));
if (toolTipIndex > -1)
{
toolTip1.SetToolTip(checkedListBox, checkedListBox.Items[toolTipIndex].ToString());
}
}
}