添加组合框项目的工具提示

时间:2013-02-05 20:03:10

标签: .net user-controls combobox

我正在使用onDraw函数添加组合框项目的工具提示,它工作正常。然而,我的组合框是分离器形式。因此,当您移动分割器时,这可以强制OnDrawItem事件。当发生这种情况时,它会删除最后一个工具提示并显示它。如何在spliter表单上处理这种类型的combox?

1 个答案:

答案 0 :(得分:0)

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