Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# CheckedListBox项目的工具提示?_C#_Winforms_User Interface_Tooltip_Checkedlistbox - Fatal编程技术网

C# CheckedListBox项目的工具提示?

C# CheckedListBox项目的工具提示?,c#,winforms,user-interface,tooltip,checkedlistbox,C#,Winforms,User Interface,Tooltip,Checkedlistbox,当用户将鼠标悬停在CheckedListBox中的项目上时,是否有一种简单的方法来设置工具提示中显示的其他文本 我希望在代码中能够做到的是: uiChkLstTables.DisplayOnHoverMember = "DisplayOnHoverProperty"; //Property contains extended details 有人能给我指出正确的方向吗?我已经找到了几篇文章,涉及到检测鼠标当前在哪个项目上,并创建一个新的工具提示实例,但这听起来有点做作,不是最好的方法 提前感谢

当用户将鼠标悬停在CheckedListBox中的项目上时,是否有一种简单的方法来设置工具提示中显示的其他文本

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

uiChkLstTables.DisplayOnHoverMember = "DisplayOnHoverProperty"; //Property contains extended details
有人能给我指出正确的方向吗?我已经找到了几篇文章,涉及到检测鼠标当前在哪个项目上,并创建一个新的工具提示实例,但这听起来有点做作,不是最好的方法


提前感谢。

无论是否有意;这就是存在的


我不知道有比您描述的更简单的方法(尽管我可能会重复使用工具提示实例,而不是一直创建新实例)。如果您的文章显示了这一点,请使用它们,或者使用本机支持这一点的第三方控件(请不要忘记)。

将工具提示对象添加到表单中,然后为调用方法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());

        }
    }

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

我想对Fermin的答案进行扩展,以使他的精彩解决方案更加清晰

在您正在使用的表单中(可能在.Designer.cs文件中),您需要将MouseMove事件处理程序添加到CheckedListBox(Fermin最初建议使用MouseHover事件处理程序,但这对我不起作用)

接下来,向表单添加两个类属性,一个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());
        }
    }
}

在项目复选框列表中运行ListItems,并将适当的文本设置为项目“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!!!";
                    }
                }

谢谢你的建议,我还没有看到。恼人的是ListView不支持数据绑定(或者我遗漏了什么?)。值得注意的是,
点p
行是不必要的
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!!!";
                    }
                }