CheckedListBox项目的工具提示?

18

在CheckedListBox中,当用户将鼠标悬停在一个项目上时,有没有一种简单的方法可以设置额外的文本出现在工具提示中?

我期望在代码中能够做到以下操作:

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

有没有人能指导我如何做到这一点?我已经找到了几篇文章,涉及检测鼠标当前所在的项目并创建新的工具提示实例,但这听起来有些牵强,不是最好的方法。

提前感谢。

5个回答

21

在您的表单上添加一个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());

        }
    }

3
注意,Point p 这行代码不是必需的。 - Maslow

6

另外,你可以使用带有复选框的ListView。此控件具有内置的工具提示支持。


谢谢您的建议,我之前没有看到过。 - Paul Suart
烦人的是,ListView不支持数据绑定(或者是我遗漏了什么?) - Jeremy Wiebe

0
我想在 Fermin 的回答上加以扩展,以便使他的精彩解决方案更加清晰明了。
在你正在工作的表单中(很可能是在.Designer.cs文件中),你需要为你的CheckedListBox添加一个MouseMove事件处理程序(Fermin最初建议使用MouseMove事件处理程序,但这对我来说没有起作用)。
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());
        }
    }
}

0

遍历复选框项的列表项目,并将适当的文本设置为项目的“title”属性,这样它就会在悬停时显示...

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!!!";
                    }
                }

0

无论是刻意的还是不刻意的,那就是现状...

我不知道比你已经描述的更简单的方法(虽然我可能会重复使用工具提示实例,而不是一直创建新的)。如果您有展示这个的文章,那么请使用它们 - 或者使用支持本地化的第三方控件(目前没有想到任何一个)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接