CheckedListBox 项目的工具提示?

发布于 2024-07-18 09:12:36 字数 331 浏览 9 评论 0原文

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

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

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

任何人都可以为我指出正确的方向吗? 我已经找到了几篇文章,其中涉及检测鼠标当前位于哪个项目上并创建新的工具提示实例,但这听起来有点过于做作,不是最好的方法。

提前致谢。

Is there a straighforward way to set additional text to appear in a tooltip when a user's mouse is held over an item in a CheckedListBox?

What I would expect to be able to do in code is:

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

Can anyone point me in the right direction to do this? I've already found a couple of articles that involve detecting which item the mouse is currently over and creating a new tooltip instance, but this sounds a little too contrived to be the best way.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小梨窩很甜 2024-07-25 09:12:36

将 Tooltip 对象添加到表单中,然后为调用 ShowToolTip() 方法的 CheckedListBox.MouseHover 添加事件处理程序;
添加 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());

        }
    }

Add a Tooltip object to your form and then add an event handler for the CheckedListBox.MouseHover that calls a method ShowToolTip();
Add MouseMove event of your CheckedListBox which has the following code:

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

Then create the ShowToolTip method:

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

        }
    }
各自安好 2024-07-25 09:12:36

或者,您可以使用 ListView 改为复选框。 该控件有
内置支持工具提示

Alternately, you could use a ListView with checkboxes instead. This control has
builtin support for tooltips.

萝莉病 2024-07-25 09:12:36

我想扩展费明的答案,以便也许使他的精彩解决方案更加清晰。

在您正在使用的表单中(可能在 .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());
        }
    }
}

I would like to expand upon Fermin's answer in order to perhaps make his wonderful solution slightly more clear.

In the form that you're working in (likely in the .Designer.cs file), you need to add a MouseMove event handler to your CheckedListBox (Fermin originally suggested a MouseHover event handler, but this did not work for me).

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

Next, add two class attributes to your form, a ToolTip object and an integer to keep track of the last checkbox whose tool tip was shown

private ToolTip toolTip1;
private int toolTipIndex;

Finally, you need to implement the showCheckBoxToolTip() method. This method is very similar to Fermin's answer, except that I combined the event callback method with the ShowToolTip() method. Also, notice that one of the method parameters is a MouseEventArgs. This is because the MouseMove attribute requires a MouseEventHandler, which then supplies 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());
        }
    }
}
戏舞 2024-07-25 09:12:36

在项目的复选框列表中运行 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!!!";
                    }
                }

Run through your ListItems in your checkbox list of items and set the appropriate text as the item 'title' attribute, and it will display on 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!!!";
                    }
                }
走走停停 2024-07-25 09:12:36

做作与否; 这就是……

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

Contrived or not; that's what there is...

I'm not aware of an easier way than you have already described (although I'd probably re-use a tooltip instance, rather than creating new all the time). If you have articles that show this, then use them - or use a 3rd party control that supports this natively (none leap to mind).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文