Tooltip for each items in a combo box

2019-01-17 04:14发布

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

There is no option like

combobox.items[1].tooltip();

Is there any way to add tooltip it ?

9条回答
Summer. ? 凉城
2楼-- · 2019-01-17 05:14

My solution:

ToolTip toolTip = new ToolTip() { AutoPopDelay = 0, InitialDelay = 0, ReshowDelay = 0, ShowAlways = true, };
comboBox.DrawMode = DrawMode.OwnerDrawFixed;
comboBox.DrawItem += (s, e) =>
{
    e.DrawBackground();
    string text = comboBox.GetItemText(comboBox.Items[e.Index]);
    using (SolidBrush br = new SolidBrush(e.ForeColor))
        e.Graphics.DrawString(text, e.Font, br, e.Bounds);
    if ((e.State & DrawItemState.Selected) == DrawItemState.Selected && comboBox.DroppedDown)
        toolTip.Show(text, comboBox, e.Bounds.Right, e.Bounds.Bottom + 4);
    e.DrawFocusRectangle();
};
comboBox.DropDownClosed += (s, e) =>
    toolTip.Hide(comboBox);
查看更多
爷的心禁止访问
3楼-- · 2019-01-17 05:14

With WPF use a ComboBox.ItemTemplate

<ComboBox               
    ItemsSource="{Binding Path=ComboBoxItemViewModels}"
    SelectedValue="{Binding SelectedComboBoxItem, 
    SelectedValuePath="Name"                
>
  <ComboBox.ItemTemplate>
    <DataTemplate>
      <TextBlock Text="{Binding Path=Name}" ToolTip="{Binding Path=Description}"/>
    </DataTemplate>
  </ComboBox.ItemTemplate>
</ComboBox>
查看更多
Ridiculous、
4楼-- · 2019-01-17 05:19

Building upon the solution from Michael Sorens (fixed a few bugs and added features). A few things this does:

  • It will display a preview of a file associated with the drop down (in this case a book title from an XML file, or you can add more descriptions in the tooltip, or display something completely different).
  • It does not display tooltip for the "0" positon in the drop down (I had a placeholder, but you can simply remove the e.index>0 in the second if statement).
  • It does not display the tooltip when the dropdown is CLOSED.

    private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
    {
        ComboBox comboBox1 = (ComboBox)sender;
        if (e.Index >= 0)
        {//Draws all items in drop down menu
            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 && e.Index > 0 && comboBox1.DroppedDown)
            {//Only draws tooltip when item 1+ are highlighted.  I had a "--" placeholder in the 0 position
                try
                {
                    XmlDocument doc;
                    XmlNode testNode;
                    doc = new XmlDocument();
                    String testXMLDoc = String.Format(@"{0}\{1}.xml", filePath, fileName);//global variables
                    String toolTip = "---Preview of File---";
                    doc.Load(testXMLDoc);
                    testNode = doc.SelectSingleNode("/Books");
                    if (testNode.HasChildNodes)
                    {
                        XmlNodeList nodeList = testNode.SelectNodes("Book");
                        foreach (XmlNode xmlNode in nodeList)
                        {
                            toolTip += "\r\n" + xmlNode.SelectSingleNode("Title").InnerXml;
                        }
                    }
                    this.toolTipHelp.Show(toolTip, comboBox1, e.Bounds.Right, e.Bounds.Bottom);
                }
                catch (Exception tp)
                {
                    Debug.WriteLine("Error in comboBox1 tooltip: " + tp);
                }
            }
            else
            {
                this.toolTipHelp.Hide(comboBox1);
            }
        }
        else
        {
            this.toolTipHelp.Hide(comboBox1);
        }
        e.DrawFocusRectangle();
    }
    
查看更多
登录 后发表回答