C# CheckedListBox 项目的工具提示?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/802213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tooltips for CheckedListBox items?
提问by Paul Suart
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?
当用户的鼠标悬停在 CheckedListBox 中的项目上时,是否有一种直接的方法来设置附加文本以显示在工具提示中?
What I would expectto 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.
提前致谢。
采纳答案by Fermin
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:
向表单添加一个 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();
Then create the ShowToolTip method:
然后创建 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());
}
}
回答by Marc Gravell
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).
我不知道比您已经描述的更简单的方法(尽管我可能会重新使用工具提示实例,而不是一直创建新的)。如果您有显示此内容的文章,请使用它们 - 或使用本机支持此功能的 3rd 方控件(没有想到)。
回答by bruno conde
回答by Zach Posten
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).
在您使用的表单中(可能在 .Designer.cs 文件中),您需要向 CheckedListBox 添加一个 MouseMove 事件处理程序(Fermin 最初建议使用 MouseHover 事件处理程序,但这对我不起作用)。
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
接下来,向表单添加两个类属性,一个 ToolTip 对象和一个整数以跟踪显示其工具提示的最后一个复选框
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.
最后,您需要实现 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());
}
}
}
回答by user2784648
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...
在您的复选框项目列表中运行您的 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!!!";
}
}