//带ToolTip的combox类文件
public class ComboBoxWithTooltip : ComboBox
{
//tipProperty为显示ToolTip文本的数据源的属性
private string tipProperty;
public string TipProperty
{
get { return tipProperty; }
set
{
if (tipProperty!=value)
{
tipProperty = value;
}
}
} private ToolTip toolTipX = new ToolTip(); /// <summary>
/// Initializes a new instance of the <see cref="ComboBoxWithTooltip"/> class.
/// </summary>
public ComboBoxWithTooltip()
{
DrawMode = DrawMode.OwnerDrawFixed;
} /// <summary>
/// Raises the <see cref="E:System.Windows.Forms.ComboBox.DrawItem"/> event.
/// </summary>
/// <param name="e">A <see cref="T:System.Windows.Forms.DrawItemEventArgs"/> that contains the event data.</param>
protected override void OnDrawItem(DrawItemEventArgs e)
{
; // Needed gate when DropDownStyle set to DropDownList (thanks to "Andrew" remarking on my
// StackOverflow post (stackoverflow.com/questions/680373/tooltip-for-each-items-in-a-combo-box/).
if (e.Index < ) { return; }
string tiptext = FilterItemOnProperty(Items[e.Index], TipProperty) as string;//从数据源Items[e.Index]的 TipProperty属性获取ToolTip文本
string Itemtext = GetItemText(Items[e.Index]);
e.DrawBackground();
using (SolidBrush br = new SolidBrush(e.ForeColor))
{ e.Graphics.DrawString(Itemtext, e.Font, br, e.Bounds); }
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
toolTipX.Show(tiptext, this, e.Bounds.Right, e.Bounds.Bottom);
//toolTip1.AutoPopDelay = 25000;
//toolTip1.InitialDelay = 1000;
//toolTip1.ReshowDelay = 0;
}
e.DrawFocusRectangle();
} /// <summary>
/// Raises the <see cref="E:System.Windows.Forms.ComboBox.DropDownClosed"/> event.
/// </summary>
/// <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data.</param>
protected override void OnDropDownClosed(EventArgs e)
{
toolTipX.Hide(this);
base.OnDropDownClosed(e);
}
}

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

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

实现代码如下:

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

在Winfrom 中,如何实现combox 的列表自动显示ToolTip提示 ?的更多相关文章

  1. BackgroundWorker实现的winfrom中实现异步等待加载图片显示

    BackgroundWorker简介    BackgroundWorker在winfrom中有对应控件,该有三个事件:DoWork .ProgressChanged 和 RunWorkerCompl ...

  2. C#Winfrom中,窗体加载时会自动执行一次控件的textchange事件,怎么让它不执行?

    http://zhidao.baidu.com/link?url=iTSyfa5_RJBSb37S8efdWoL5eDMrnxeAm-prhGSNBXqdP9r7PzNDQTc7gVzJgCNdzli ...

  3. 在Sharepoint2010中一种自定义调查列表的不允许再次答复提示的处理方法!

    在Sharepoint中默认创建的调查列表系统只允许答复一次,再次答复将报错误信息,这对最终用户而言是非常不友好的体验,当然你也可以在调查设置中的常规设置中设置允许多次答复,这样就会有错误提示信息,但 ...

  4. Winfrom中ListBox绑定List数据源更新问题

    Winfrom中ListBox绑定List数据源更新问题 摘自:http://xiaocai.info/2010/09/winform-listbox-datasource-update/ Winfr ...

  5. python中的字典(dict),列表(list),元组(tuple)

    一,List:列表 python内置的一种数据类型是列表:list.list是一种有序的数据集合,可以随意的添加和删除其中的数据.比如列出班里所有的同学的名字,列出所有工厂员工的工号等都是可以用到列表 ...

  6. Winfrom中的几种传值方式

    1.最常见的一种, 不知道你们是否记得构造函数,当然这是对有点基础的人说的, 再我们申明一个类的时候,可能很多时候都不会注意,因为会我们的编辑器自带会默认生成一个不带参数的构造函数, 所以不在意,但是 ...

  7. 无法从项目中获取SSIS包的列表

    一直做的SSIS项目,突然在生成项目的时候没有反应,crtl + alt +o 提示:无法从项目中获取SSIS包的列表,发现是最近的包没有设计数据源, 解决思路:检查最近的包,挨个运行一遍,看看有没有 ...

  8. 如何在Android的ListView中构建CheckBox和RadioButton列表(支持单选和多选的投票项目示例)

    引言 我们在android的APP开发中有时候会碰到提供一个选项列表供用户选择的需求,如在投票类型的项目中,我们提供一些主题给用户选择,每个主题有若干选项,用户对这些主题的选项进行选择,然后提交. 本 ...

  9. 页面中php传值后循环列表js获取点击的id

    页面中php传值后循环列表js获取点击的id值进行js操作 <script type="text/javascript" src="__PUBLIC__/js/jq ...

随机推荐

  1. 用Navicat for Mysql导入.sql文件

    1.在左边右键新建一个数据库 2.字符集选gbk(不 题) 3.打开数据库,把它变成绿色. 4.把.sql文件拖到这数据库上.会出现下边的运行SQL文件对话框,按开始,等运行完后就可以关掉了. 5.最 ...

  2. CentOS6.8下安装Redis

    1.由于Redis是使用C语言开发的,安装时需要对Redis的源码进行编译,编译依赖gcc环境,如果没有gcc,需要先安装gcc: yum install gcc-c++ 2.安装完成后,进入Redi ...

  3. 使用JBarcode生成一维码

    需要的jar包,只有jbarcode.jar 链接: https://pan.baidu.com/s/1o9oDPB8 密码: x367 public class Main { //设置条形码高度 p ...

  4. ant安装

  5. dijkstra最短路

    感觉自己太懒了,以后每天更博客激励自己吧. //时间复杂度O(n*n)的最短路算法 //首先需要设置一个访问数组v[maxn],一个数组d[maxn], memset(v,,sizeof(v)); ; ...

  6. (转载)一张表搞清楚西门子S7系列标准DB块与优化DB块

    在TIA Portal中为S7-1200/S7-1500 CPU 添加一个 DB 块时,其缺省属性为优化的 DB ,优化的 DB 块与标准的 DB 块整体对比如下表所示: 项 标准 DB 优化 DB ...

  7. mysql中if()函数使用

    博主原创,转载请注明出处: 在mysql中if()函数的用法类似于java中的三目表达式,其用处也比较多,具体语法如下: IF(expr1,expr2,expr3),如果expr1的值为true,则返 ...

  8. Centos7安装配置Apache+PHP+Mysql+phpmyadmin

    转载自: Centos7安装配置Apache+PHP+Mysql+phpmyadmin 一.安装Apache yum install httpd 安装成功后,Apache操作命令: systemctl ...

  9. 网页title左边显示网页的logo图标

    打开某一个网页会在浏览器的标签栏处显示该网页的标题和图标,当网页被添加到收藏夹或者书签中时也会出现网页的图标,怎么在网页title左边显示网页的logo图标呢? 方法1: 找一个或者作一个ico文件, ...

  10. python连接MongoDB(有密码有认证)

    from pymongo import MongoClient host = '127.0.0.1' client = MongoClient(host, ) #连接mydb数据库,账号密码认证 db ...