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);
           }
  • 相关阅读:
    [转]CR, LF, CR/LF区别与关系
    [转]Maven与nexus关系
    [转]Ubuntu默认使用root用户登录并免去输入密码
    [转]PL/SQL Developer 导入导出csv文件
    [转]关于胖客户端和瘦客户端的理解
    解决win7 安装完jdk7后,再安装jdk8出现的问题 has value '1.8', but '1.7' is required.
    [转]10 Awesome Indicator Applets for Ubuntu’s Unity Desktop
    简单的多对一传输ns2仿真
    论文阅读笔记 (2007-06-09)
    一个简单的ns2实验全过程
  • 原文地址:https://www.cnblogs.com/qq260250932/p/5248877.html
Copyright © 2011-2022 走看看