簡體   English   中英

添加組合框項目的工具提示

[英]Add tooltip for combobox items

我正在使用onDraw函數為組合框項目添加工具提示,並且工作正常。 但是我的組合框是拆分器形式。 因此,當您移動拆分器時,這可以強制執行OnDrawItem事件。 發生這種情況時,它會重新提示並顯示最后一個工具提示。 如何在拆分器表單上處理這種類型的combox?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM