上次写到datagridview通过重写DataGridView的OnCellMouseEnter事件来实现内容完全显示。
地址:https://segmentfault.com/a/11...

但发现有部分需要悬浮的信息很长,悬浮信息几秒钟之后就消失了,来不及把所有信息看完。找了很多 设置 datagridview 的tooptiptext显示时间的方式,未果。

只好考虑自己在datagridview上增加一个ToolTip,自己手工设置ToolTip来实现。具体如下
1、拖一个ToolTip到datagridview上。默认名字toolTip1。
2、在OnCellMouseEnter事件中把原来的tooltiptext显示设置成false.
3、设置tooltip1的属性

    protected override void OnCellMouseEnter(DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex < 0 || e.RowIndex < 0 || this.Rows.Count <= 0)
        {
        }
        else
        {
            this.ShowCellToolTips = false;//默认的cellTollTips没找到设置显示时间的地方,在这里屏蔽,使用一下自己加的toolTip1来设置显示
            toolTip1.AutomaticDelay = 1;
            toolTip1.AutoPopDelay = 1000 * 30;//这里貌似是 不能超过32767 Int16的最大值,否则会默认成5000
            toolTip1.UseAnimation = true;
            toolTip1.UseFading = true;
            string toolTipText = (this.Rows[e.RowIndex].Cells[e.ColumnIndex].Value ?? string.Empty).ToString();
            toolTip1.SetToolTip(this, toolTipText);
        }  
    }

这里的AutoPopDelay需要说明一下,C#的API上介绍是 :

    //
    // 摘要:
    //     获取或设置当指针在具有指定工具提示文本的控件内保持静止时,工具提示保持可见的时间期限。
    //
    // 返回结果:
    //     当指针在控件上保持静止时,System.Windows.Forms.ToolTip 保持可见的时间期限(以毫秒为单位)。默认值为 5000。

这里额外说明一下,设置长度不能超过32767,否则应该按默认的5000.所以最多也只能停留32秒。


OldQ
1 声望1 粉丝