CheckedListBox项目的工具提示?

当用户的鼠标放在CheckedListBox中的项目上时,是否有一种直接的方法来设置其他文本出现在工具提示中?

希望能够在代码中做到的是:

uiChkLstTables.DisplayOnHoverMember = "DisplayOnHoverProperty"; //Property contains extended details 

任何人都能指出我正确的方向吗? 我已经找到了一些文章,涉及检测鼠标当前所在的项目并创建一个新的工具提示实例,但这听起来有点过于人为,不是最好的方法。

提前致谢。

向表单添加一个Tooltip对象,然后为CheckedListBox.MouseHover添加一个调用方法ShowToolTip()的事件处理程序; 添加CheckedListBox的MouseMove事件,其中包含以下代码:

 //Make ttIndex a global integer variable to store index of item currently showing tooltip. //Check if current location is different from item having tooltip, if so call method if (ttIndex != checkedListBox1.IndexFromPoint(e.Location)) ShowToolTip(); 

然后创建ShowToolTip方法:

 private void ShowToolTip() { ttIndex = checkedListBox1.IndexFromPoint(checkedListBox1.PointToClient(MousePosition)); if (ttIndex > -1) { Point p = PointToClient(MousePosition); toolTip1.ToolTipTitle = "Tooltip Title"; toolTip1.SetToolTip(checkedListBox1, checkedListBox1.Items[ttIndex].ToString()); } } 

或者,您可以使用带复选框的ListView 。 此控件内置了工具提示的支持。

是否受挫; 那是什么……

我不知道比你已经描述过的更简单的方法(尽管我可能会重新使用工具提示实例,而不是一直创建新工具)。 如果你有文章显示这个,那么使用它们 – 或使用支持这种本地的第三方控件(没有想到的跳跃)。

我想扩展Fermin的答案,以便让他的精彩解决方案更加清晰。

在您正在使用的forms(可能在.Designer.cs文件中),您需要将一个MouseMove事件处理程序添加到CheckedListBox(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()); } } } 

在项目的复选框列表中运行ListItems并将相应的文本设置为项目’title’属性,它将在hover时显示…

 foreach (ListItem item in checkBoxList.Items) { //Find your item here...maybe a switch statement or //a bunch of if()'s if(item.Value.ToString() == "item 1") { item.Attributes["title"] = "This tooltip will display when I hover over item 1 now, thats it!!!"; } if(item.Value.ToString() == "item 2") { item.Attributes["title"] = "This tooltip will display when I hover over item 2 now, thats it!!!"; } }