16

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

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

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

谁能指出我正确的方向来做到这一点?我已经找到了几篇文章,其中涉及检测鼠标当前在哪个项目上并创建一个新的工具提示实例,但这听起来有点做作,不是最好的方法。

提前致谢。

4

5 回答 5

20

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

        }
    }
于 2009-04-29T13:10:28.560 回答
6

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

于 2009-04-29T13:15:18.940 回答
0

做作与否;就是这样……

我不知道比您已经描述的更简单的方法(尽管我可能会重新使用工具提示实例,而不是一直创建新的)。如果您有显示此内容的文章,请使用它们 - 或使用本机支持此内容的 3rd 方控件(没有想到)。

于 2009-04-29T13:00:13.233 回答
0

我想扩展费尔明的答案,以便让他的奇妙解决方案更加清晰。

在您正在使用的表单中(可能在 .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());
        }
    }
}
于 2015-03-22T20:58:50.210 回答
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!!!";
                    }
                }
于 2016-02-06T00:29:43.373 回答