2009-04-29 83 views
11

当用户的鼠标悬停在CheckedListBox中的某个项目上时,是否有一种直接的方式来设置附加文本以显示在工具提示中?CheckedListBox项目的工具提示?

我会期望能在代码做的是:

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

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

在此先感谢。

回答

12

将Tooltip对象添加到窗体中,然后为CheckedListBox.MouseHover添加一个调用方法ShowToolTip()的事件处理程序; 添加MouseMove事件您CheckedListBox它具有以下代码:

//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()); 

     } 
    } 
+1

的`点p`线不是必需的 – Maslow 2014-09-25 17:23:06

0

有或没​​有;这是什么...

我不知道比你已经描述的更简单的方法(虽然我可能会重新使用工具提示实例,而不是始终创建新的)。如果你有文章显示这个,然后使用它们 - 或者使用第三方控件来支持这个本地(无需介意)。

5

另外,还可以使用带有一个复选框,而不是ListView。此控件有 内置支持工具提示

+0

感谢您的建议,没有看到。 – 2009-04-29 13:28:14

0

我想扩大弗明的答案,以便可能使他的美妙的解决方案稍微更清晰。

在您工作的表单中(很可能在.Designer.cs文件中),您需要向您的CheckedListBox添加一个MouseMove事件处理程序(Fermin最初建议使用MouseHover事件处理程序,但这不适用于我)。

this.checkedListBox.MouseMove += new System.Windows.Forms.MouseEventHandler(this.showCheckBoxToolTip); 

接着,添加两个类属性到表单,工具提示对象和整数跟踪过去的复选框,其刀尖被证明

private ToolTip toolTip1; 
private int toolTipIndex; 

最后,你需要实现showCheckBoxToolTip () 方法。除了我将事件回调方法与ShowToolTip()方法相结合之外,此方法与Fermin的答案非常相似。另请注意,其中一个方法参数是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()); 
     } 
    } 
} 
0
通过项目的复选框列表中时listItems

运行,并设置相应的文本作为该项目“标题”属性,它会显示在悬停......

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!!!"; 
        } 
       } 
注意
相关问题