繁体   English   中英

是否可以增加显示 DataGridView 单元格工具提示的持续时间?

[英]Is it possible to enhance the duration of showing a DataGridView Cell Tooltip?

基于此如何:将工具提示添加到 Windows Forms DataGridView 控件中的单个单元格 - 我为 DataGridView 中的单个单元格实现了特定的工具提示。

 void init() {
   dgv.CellFormatting += CellToolTip;
 }

 void CellToolTip(object sender, DataGridViewCellFormattingEventArgs e) {
    if ((e.ColumnIndex == dgv.Columns["xxx"].Index) && e.Value != null)
    {
      [...]
      DataGridViewCell cell = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex];
      cell.ToolTipText = "test";
    }             
 }  

是否可以修改持续时间以延长我的 ToolTip 显示时间,或者我是否必须创建 ToolTip object 并使用 ToolTip.AutomaticDelay 等属性?

您可以使用Reflection访问内部ToolTip组件,该组件是名为DataGridViewToolTip的内部私有 class 的成员。 class 有一个公共的只读属性返回ToolTip实例,您可以访问该实例以获取或设置其属性和/或执行实例方法。

这是扩展方法示例。

// +
using System.Reflection;
// ...

internal static class DataGridViewExt
{
    internal static ToolTip GetInternalToolTip(this DataGridView dgv)
    {
        var ttcName = "toolTipControl";
        var ttpName = "ToolTip";
        var ttc = dgv
            .GetType()
            .GetField(ttcName, BindingFlags.NonPublic | BindingFlags.Instance)
            .GetValue(dgv);
        var ttp = ttc
            .GetType()
            .GetProperty(ttpName, BindingFlags.Public | BindingFlags.Instance)
            .GetValue(ttc);

        return ttp as ToolTip;
    }
}

调用GetInternalToolTip扩展方法并设置组件的属性。

private  void SomeCaller()
{
    var ttp = dataGridView1.GetInternalToolTip();

    ttp.AutoPopDelay = 3000;
    ttp.ToolTipTitle = "Tip";
    ttp.ToolTipIcon = ToolTipIcon.Info;
}

对于您的具体问题,您只需要调整AutoPopDelay属性。 所以:

private void SomeCaller() => dataGridView1.GetInternalToolTip().AutoPopDelay = 3000;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM