提示为在组合框中每个项目组合、框中、提示、项目

2023-09-02 23:47:02 作者:帅到天理不容

我只是想在组合框中添加一个提示为每个项目。我使用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();

有什么办法补充工具提示呢?

Is there any way to add tooltip it ?

推荐答案

其实有一对夫妇合理的解决这个问题。一个MSDN论坛上有一个ComboBox项目亮点事件后,它包含两种可能性, 一个来自nobugz,一个来自agrobler。他们每个人都提供了code子类组合框是应该处理在组合框的下拉列表中各个项目的工具提示。 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.

这两种解决方案的出现,以允许分配到各个项目的任意工具提示。更具体的,但更常见的情况,在这里你只是想工具提示以反映该项目的文本,当你知道你可能是太长,不适合组合框的宽度项目。就我自己来说,我有一个组合框,保存完整的文件路径,所以很容易看到的内容可能会超过组合框的宽度的一个实例。

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.

智鑫烨,在MSDN论坛上发帖Windows下拉问题时,提供了一个解决方案来解决这个更具体的问题,并且要简单得多。我在这里重现code的全部内容。 (请注意,此code presupposes你已经创建了一个名为Form1窗体,并迷上了显示处理器的负载,并且还增加了一个组合框名为ComboBox1的和刀尖处理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();
}

虽然简单明了,这code也从一个缺陷遭受(正如指出的回复对上述MSDN线程):当你移动鼠标(不点击)从一个下拉列表项下,只有每一个的等的一张是一个持久的提示!此修复程序仅暗示由该线程又一个条目,所以我认为这将是有益的,以提供充分,更正code在这里:

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

除了除去code中的主要差别是移动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增编

只是想提一提,因为我已经创建了一个复合组合框,所以如果你用我的图书馆,你没有code写的,在所有采用该工具提示功能。只要将一个ComboBoxWithTooltip到Visual Studio设计和你做。向下钻取到ComboBoxWithTooltip我 API页面或的下载我的开源C#库开始。 (请注意,补丁的漏洞安德鲁陷入将在发布04年1月1日,出来很快就到期了。)

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