Tooltips for CheckedListBox items?

社会主义新天地 提交于 2019-12-01 15:05:31

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

        }
    }

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

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).

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

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!!!";
                    }
                }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!