zoukankan      html  css  js  c++  java
  • 在Winfrom 中,如何实现combox 的列表自动显示ToolTip提示 ?

     1  //带ToolTip的combox类文件
     2  public class ComboBoxWithTooltip : ComboBox
     3     {
     4     //tipProperty为显示ToolTip文本的数据源的属性
     5         private string tipProperty;
     6         public string TipProperty
     7         {
     8             get { return tipProperty; }
     9             set
    10             {
    11                 if (tipProperty!=value)
    12                 {
    13                     tipProperty = value;
    14                 }
    15             }
    16         }
    17 
    18        private ToolTip toolTipX = new ToolTip(); 
    19     
    20         /// <summary>
    21         /// Initializes a new instance of the <see cref="ComboBoxWithTooltip"/> class.
    22         /// </summary>
    23         public ComboBoxWithTooltip()
    24         {   
    25             DrawMode = DrawMode.OwnerDrawFixed;
    26         }
    27 
    28         /// <summary>
    29         /// Raises the <see cref="E:System.Windows.Forms.ComboBox.DrawItem"/> event.
    30         /// </summary>
    31         /// <param name="e">A <see cref="T:System.Windows.Forms.DrawItemEventArgs"/> that contains the event data.</param>
    32         protected override void OnDrawItem(DrawItemEventArgs e)
    33         {
    34              ; 
    35 
    36             // Needed gate when DropDownStyle set to DropDownList (thanks to "Andrew" remarking on my 
    37             // StackOverflow post (stackoverflow.com/questions/680373/tooltip-for-each-items-in-a-combo-box/).
    38             if (e.Index < 0) { return; }
    39             string tiptext = FilterItemOnProperty(Items[e.Index], TipProperty) as string;//从数据源Items[e.Index]的 TipProperty属性获取ToolTip文本
    40            string Itemtext = GetItemText(Items[e.Index]);
    41             e.DrawBackground();
    42             using (SolidBrush br = new SolidBrush(e.ForeColor))
    43             { e.Graphics.DrawString(Itemtext, e.Font, br, e.Bounds); }
    44             if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
    45             {
    46                 toolTipX.Show(tiptext, this, e.Bounds.Right, e.Bounds.Bottom);                
    47                 //toolTip1.AutoPopDelay = 25000;
    48                 //toolTip1.InitialDelay = 1000;
    49                 //toolTip1.ReshowDelay = 0; 
    50             }
    51             e.DrawFocusRectangle();
    52         }
    53 
    54         /// <summary>
    55         /// Raises the <see cref="E:System.Windows.Forms.ComboBox.DropDownClosed"/> event.
    56         /// </summary>
    57         /// <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data.</param>
    58         protected override void OnDropDownClosed(EventArgs e)
    59         {
    60             toolTipX.Hide(this);
    61             base.OnDropDownClosed(e);
    62         }
    63     }

    ===================================================

    上边是实现后的结果,找了好长时间,才找到,做个记录。

    实现代码如下:

    ToolTip tt = null;
           ComboBox cb = null;
           private void Form1_Load(object sender, EventArgs e)
           {
               cb = new ComboBox();
               cb.Items.Insert(0,"第一");
               cb.Items.Insert(1,"第二");
               cb.Items.Insert(2,"第三");
               cb.Items.Insert(3,"第四");
               cb.DrawMode = DrawMode.OwnerDrawFixed;
               cb.DrawItem+=new DrawItemEventHandler(cb_DrawItem);
               cb.DropDownClosed+=new EventHandler(cb_DropDownClosed);
               this.Controls.Add(cb);
               cb.SelectedIndex = 1;
               tt = new ToolTip();
               tt.SetToolTip(cb, "zj");
           }
    void cb_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
           {
               // 绘制背景
               e.DrawBackground();
               //绘制列表项目
               e.Graphics.DrawString(cb.Items[e.Index].ToString(), e.Font, System.Drawing.Brushes.Black, e.Bounds);
               //将高亮的列表项目的文字传递到toolTip1(之前建立ToolTip的一个实例)
               if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
                   tt.Show(cb.Items[e.Index].ToString(), cb, e.Bounds.X + e.Bounds.Width, e.Bounds.Y + e.Bounds.Height);
               e.DrawFocusRectangle();
           }
           void cb_DropDownClosed(object sender, EventArgs e)
           {
               tt.Hide(cb);
           }
  • 相关阅读:
    Shell脚本精选 规格严格
    转载Http一篇文章 规格严格
    Eclipse插件安装 规格严格
    Jad用法(转) 规格严格
    Java通过代理服务器访问外部网络 规格严格
    hwclock(Linux) 规格严格
    Linux下的多线程编程
    30岁前男人需要完成的事
    FTP主动模式及被动模式
    6年软件开发经验总结
  • 原文地址:https://www.cnblogs.com/qq260250932/p/5248877.html
Copyright © 2011-2022 走看看