简体   繁体   中英

Add tooltip for combobox items

I am using the onDraw function to add a tooltip for combobox items and it is working fine. However my combobox is on a spliter form. Therefore, when you move the spliter this can force a OnDrawItem event. When this happends it remenbers the last tooltip and shows it. How do I handle this type of combox on a spliter form ?

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Data;

namespace MyForm
{
    public class MyComboBox : ComboBox
    {
        private ToolTip toolTip;
        private String toolTipMember;
        private List<String> toolTipString;
        private DataTable myDataSource;

        public MyComboBox()
            : base()
        {
            toolTipString = new List<String>();
        }

        public String ToolTipMember
        {
            set
            {
                this.toolTipMember = value;
                if (myDataSource != null)
                initTootTip(this.toolTipMember);
            }
        }

        public DataTable MyDataSource
        {
            set
            {
                myDataSource = value;
                this.DataSource = myDataSource;
            }
        }

        private void initTootTip(string toolTipMember)
        {
            toolTip = new ToolTip();
            if ((this.DataSource != null) && (toolTipMember != null))
            {
                foreach (DataRow row in myDataSource.Rows)
                {
                    toolTipString.Add(row[toolTipMember].ToString());
                }
            }
        }
        protected override void OnDataSourceChanged(EventArgs e)
        {
            base.OnDataSourceChanged(e);
            if (myDataSource != null)
            initTootTip(toolTipMember);
        }
        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            string text = this.GetItemText(Items[e.Index]);
            base.OnDrawItem(e);
            e.DrawBackground();
            using (SolidBrush br = new SolidBrush(e.ForeColor))
            {
                e.Graphics.DrawString(text, e.Font, br, e.Bounds);
            }
            int index = e.Index;
            //Console.WriteLine("state : " + e.State.ToString());
            if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
            {
                try
                {
                    text = toolTipString[index];
                    this.toolTip.Show(text,
                        this, e.Bounds.Right, e.Bounds.Bottom);
                    //Console.WriteLine("show : " + text);
                }
                catch (Exception)
                {
                }
            }
            e.DrawFocusRectangle();
        }
        protected override void OnDropDownClosed(EventArgs e)
        {
            base.OnDropDownClosed(e);
            toolTip.Hide(this);
        }
    }
}

**UsethisclassandaddMyComboBoxComponent<br>
withproperties**
MyComboBoxcomboBox1
DataTabletable=newDataTable()
table.Columns.Add("value_member",typeof(int))
table.Columns.Add("display_member",typeof(string))
table.Columns.Add("tooltip_member",typeof(string))
comboBox1.MyDataSource=table
comboBox1.DisplayMember="display_member"
comboBox1.ValueMember="value_member"
comboBox1.ToolTipMember="tooltip_member"
comboBox1.DrawMode=DrawMode.OwnerDrawFixed
comboBox1.SelectedIndex=-1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM