0

我正在使用 onDraw 函数为组合框项目添加工具提示,它工作正常。但是,我的组合框位于拆分器形式上。因此,当您移动拆分器时,这可能会强制触发 OnDrawItem 事件。当发生这种情况时,它会记住最后一个工具提示并显示它。如何在拆分器表单上处理这种类型的组合框?

4

1 回答 1

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
于 2013-05-21T09:23:33.917 回答