Double Tooltip when handling DataGridViewCellToolTipTextNeeded event with custom tooltip

Matthew Busselberg 21 Reputation points
2021-06-23T06:41:03.717+00:00

Please note: This only happens when handling tooltips for a combobox column.

I have a datagridview with several columns and am using the HTMLRenderer.Winforms Nuget Package to handle most of my cell based tooltips. To do this I added a handler for DataGridViewCellToolTipTextNeeded that goes as such:

private void OnCellToolTipTextNeeded(object sender, DataGridViewCellToolTipTextNeededEventArgs e)
{
    if (e.RowIndex == -1)
        return;

    Size maxSize = Size.Empty;
    int customColumnoffset = 0;

    Mod mod = (Mod)configurationGrid.Rows[e.RowIndex].DataBoundItem;
    StringBuilder html = null;

    //... Skipping irrelevant sections

    else if (e.ColumnIndex == userTagColumn)
    {
        if (html == null)
            html = new StringBuilder();

        List<string> tags = mod.UserTags;
        if ((tags.Count > 0) && (tags[0].Length > 0))
        {
            html.Append("<b>");
            html.Append(tags[0]);
            html.Append("</b>");
            for (int i = 1; i < tags.Count; ++i)
                html.AppendChain(", ").Append(tags[i]);
        }
        customColumnoffset = 8;//= int.MinValue;

        // SHOWING TWO TOOLTIPS.
        // THINGS I TRIED WHICH FAILED TO FIX PROBLEM:
        //configurationGrid[e.ColumnIndex, e.RowIndex].ToolTipText = string.Empty;
        //e.ToolTipText = string.Empty;
    }

    //... Skipping irrelevant sections

    if (html != null)
    {
        Rectangle cellRect = configurationGrid.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);
        modConfigCellToolTip = new HtmlToolTip();
        if (maxSize != Size.Empty)
        {
            defaultTooltipSize = modConfigCellToolTip.MaximumSize;
            modConfigCellToolTip.MaximumSize = maxSize;
        }

        Size size = (HtmlRender.Measure(configurationGrid.CreateGraphics(), html.ToString(), (float)modConfigCellToolTip.MaximumSize.Width)).ToSize();
        //if (customColumnoffset == int.MinValue)
        //  customColumnoffset = 4 - (2 * size.Width);
        Point tcfgL = new Point(tabControl1.Location.X + configurationGrid.Location.X + customColumnoffset, +toolStrip1.Height * 2 + 3 + tabControl1.Location.Y + configurationGrid.Location.Y);

        // make a tooltip css class with smaller padding/margins to improve look.  
        // modConfigCellToolTip is of type HTMLRenderer.Winforms.HTMLToolTip
        modConfigCellToolTip.Show(html.ToString(), this, tcfgL.X + cellRect.X + cellRect.Size.Width,
             tcfgL.Y + cellRect.Y - size.Height / 3, 10000);
    }
}

The problem is that I get two tooltips which appear for the cell. I am assuming that the column nature of my cell is causing the second one to come up (it doesn't happen for other non-combobox cells) but even when the combo-box text fits in the given cell, it STILL raises two. As mentioned in my comments I have tried two different ways of altering the cell.ToolTipText to prevent the second one from appearing and neither method worked. How can I keep this from happening?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,650 questions
0 comments No comments
{count} votes