Home > Back-end >  Is it possible to enhance the duration of showing a DataGridView Cell Tooltip?
Is it possible to enhance the duration of showing a DataGridView Cell Tooltip?

Time:09-23

Based on this How to: Add ToolTips to Individual Cells in a Windows Forms DataGridView Control - I implemented a specific ToolTip for a single cell in my 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";
    }             
 }  

Is it possible to modify the duration to show my ToolTip longer or do I have to create ToolTip object and use properties like ToolTip.AutomaticDelay etc. ?

CodePudding user response:

You can use Reflection to access the internal ToolTip component which is a member of an internal private class named DataGridViewToolTip. The class has a public read-only property returns the ToolTip instance that you can access to get or set its properties and/or execute the instance methods.

Here's extension method example.

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

Call the GetInternalToolTip extension method and set the component's properties.

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

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

For your specific problem, you just need to adjust the AutoPopDelay property. So:

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