Winfrom 中如何实现combox 的列表自动显示ToolTip提示

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

实现代码如下:

 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);
        }

  

转载于:https://www.cnblogs.com/zjBoy/archive/2012/09/10/2678431.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值