组合框中每个项目的工具提示 [英] Tooltip for each items in a combo box

查看:19
本文介绍了组合框中每个项目的工具提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想为组合框中的每个项目添加工具提示.我正在使用 c#.net windows 应用程序.

I just want to add a tooltip for each item in a combo box. i am using c#.net windows application.

没有类似的选项

combobox.items[1].tooltip();

combobox.items[1].tooltip();

有什么办法可以添加tooltip吗?

Is there any way to add tooltip it ?

推荐答案

对于这个问题,实际上有几个合理的解决方案.MSDN 论坛有一个 ComboBox项目高亮事件 包含两种可能性的帖子,一种来自 nobugz,一种来自 agrobler.它们中的每一个都提供了对 ComboBox 进行子类化的代码,该 ComboBox 应该处理 ComboBox 下拉列表中各个项目的工具提示.Agrobler 的解决方案看起来更加精致,因为他/她甚至包括一些漂亮的插图,但不幸的是,不清楚(至少对我而言)如何填充控件的关键 ToolTipMember 属性.

There are actually a couple reasonable solutions to this question. An MSDN forum has a ComboBox Item highlight event post that contains two possibilities, one from nobugz and one from agrobler. Each of them provides code to subclass a ComboBox that is supposed to handle tool tips on individual items in the ComboBox's dropdown. Agrobler's solution looks more polished, in that he/she even includes some nice illustrations, but unfortunately it is not clear (at least to me) how to populate the crucial ToolTipMember property of the control.

这两种解决方案似乎都允许将任意工具提示分配给单个项目.一个更具体但更常见的情况是,当您知道可能有太长而无法适应 ComboBox 宽度的项目时,您只需要工具提示来反映项目的文本.在我自己的例子中,我有一个包含完整文件路径的 ComboBox 实例,因此很容易看到内容可能超出 ComboBox 宽度的位置.

Both of these solutions appear to allow arbitrary tooltips assigned to individual items. A more specific, but more common case, is where you simply want the tooltip to mirror the text of the item, when you know you may have items that are too long to fit the width of the ComboBox. In my own case, I have an instance of a ComboBox that holds complete file paths so it is easy to see where the contents could exceed the ComboBox's width.

Zhi-Xin Ye,在 MSDN 论坛帖子 Windows 下拉问题,提供了解决这个更具体问题的解决方案,并且简单得多.我在这里完整地复制了代码.(请注意,此代码假定您已经创建了一个名为 Form1 的 Form 并连接了所示的加载处理程序,并且还添加了一个名为 comboBox1 的 ComboBox 和一个工具提示处理程序 toolTip1.)

Zhi-Xin Ye, in the MSDN forum post Windows Dropdown question, provides a solution that addresses this more specific problem and is much simpler. I reproduce the code here in its entirety. (Note that this code presupposes you have created a Form called Form1 and hooked up the load handler shown, and also added a ComboBox named comboBox1 and a tool tip handler toolTip1.)

private void Form1_Load(object sender, EventArgs e)
{
    this.comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
    this.comboBox1.DrawItem += new DrawItemEventHandler(comboBox1_DrawItem);
}

void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
    string text = this.comboBox1.GetItemText(comboBox1.Items[e.Index]);
    e.DrawBackground();
    using (SolidBrush br = new SolidBrush(e.ForeColor))
    { e.Graphics.DrawString(text, e.Font, br, e.Bounds); }

    if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
    { this.toolTip1.Show(text, comboBox1, e.Bounds.Right, e.Bounds.Bottom); }
    else { this.toolTip1.Hide(comboBox1); }
    e.DrawFocusRectangle();
}

虽然简单明了,但这段代码确实存在一个缺陷(如上述 MSDN 线程的回复中所指出的那样):当您将​​鼠标(不单击)从一个下拉项移动到下一个时,只有每个 other 显示一个持久的工具提示!该修复程序仅由该线程上的另一个条目暗示,因此我认为在此处提供完整的、更正的代码会很有用:

While simple and concise, this code does suffer from one defect (as is pointed out in a reply on the above MSDN thread): as you move the mouse (without clicking) from one dropdown item to the next, only every other one shows a persistent tooltip! The fix is only hinted at by yet another entry on that thread, so I thought it would be useful to provide the full, corrected code here:

private void Form1_Load(object sender, EventArgs e)
{
    comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
    comboBox1.DrawItem += comboBox1_DrawItem;
    comboBox1.DropDownClosed += comboBox1_DropDownClosed;
}

private void comboBox1_DropDownClosed(object sender, EventArgs e)
{
    toolTip1.Hide(comboBox1);
}

private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
    if (e.Index < 0) { return; } // added this line thanks to Andrew's comment
    string text = comboBox1.GetItemText(comboBox1.Items[e.Index]);
    e.DrawBackground();
    using (SolidBrush br = new SolidBrush(e.ForeColor))
    { e.Graphics.DrawString(text, e.Font, br, e.Bounds); }
    if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
    { toolTip1.Show(text, comboBox1, e.Bounds.Right, e.Bounds.Bottom); }
    e.DrawFocusRectangle();
}

除了删除一些冗余的代码部分(例如this"限定符)之外,主要区别在于将 toolTip1.Hide 调用移动到 DropDownClosed 事件处理程序中.将其从 DrawItem 处理程序中取出,消除了上述缺陷;但是你需要在下拉菜单关闭时关闭它,否则最后显示的工具提示将保留在屏幕上.

Besides removing a few redundant portions of code (e.g. the "this" qualifier) the primary difference is moving the toolTip1.Hide call into the DropDownClosed event handler. Taking it out of the DrawItem handler eliminates the defect mentioned above; but then you need to close it when the drop down closes, otherwise the last displayed tooltip will remain onscreen.

2012.07.31 附录

只想提一下,我已经创建了一个包含此工具提示功能的复合 ComboBox,因此如果您使用我的库,则根本无需编写代码.只需将 ComboBoxWithTooltip 拖到 Visual Studio 设计器上即可.在我的 API 页面下载 我的开源 C# 库以开始使用.(请注意,针对 Andrew 发现的错误的补丁将在 1.1.04 版中发布,即将发布.)

Just wanted to mention that I have since created a composite ComboBox that incorporates this tooltip capability so if you use my library you have no code to write at all. Just drag a ComboBoxWithTooltip onto the Visual Studio designer and you are done. Drill down to ComboBoxWithTooltip on my API page or download my open-source C# library to get started. (Note that the patch for the bug Andrew caught will be in release 1.1.04, due out soon.)

这篇关于组合框中每个项目的工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆