C# AutoEllipsis property for Label not showing tooltip on Add-Ins?

5548
2
05-03-2011 12:02 PM
JeffreyHamblin
New Contributor III
I am hoping someone can confirm (or deny) this problem I am seeing:

I am using VS C# 2008 Express.

On all my Add-Ins that display a custom form, I have noticed that for any labels I have set AutoEllipsis to TRUE and AutoSize to FALSE, if the text overflows then the ellipsis is appended to the end of the truncated text -- however, the expected tooltip containing the complete text DOES NOT display.

If I call the same form from a test exe (not an Add-In), then it works as expected.

(I am also seeing some other subtle font painting differences, but one thing at a time...)

-Jeff H
0 Kudos
2 Replies
JeffreyHamblin
New Contributor III
Additional information:

Setting the UseCompatibleTextRendering property for a Label to TRUE (the default is FALSE), causes the Label to have the same problem whether running within the context of an Add-In or on a form in a regular exe. (and it also makes some other subtle font painting differences go away.)

This makes me wonder if Add-Ins are inheriting a global setting of Application.SetCompatibleTextRenderingDefaul(true) from ArcGIS.

And the .NET documentation states: "You should never call this method if your Windows Forms code is hosted in another application, such as Internet Explorer. Only call this method in stand-alone Windows Forms applications."
... so I don't think we can change the setting back to FALSE for an Add-In.

-Jeff H
0 Kudos
JeffreyHamblin
New Contributor III
Here is more information and a work around for anyone else that runs into this issue.

This is not directly an ArcGIS issue, more of a .NET issue, but it does affect Add-Ins that use Windows Forms.

After more reading today, I believe ArcObjects 10.0 for .NET is using the older GDI+ (from .NET 1.0/1.1) and the Graphics class rather than the newer GDI and TextRenderer class introduced in .NET 2.0.

More info here on MSDN.

It seems the autoellipsis tooltip for overflowed text in Label controls is indeed broken by setting either the control's UseCompatibleTextRendering property to true, or the global Application.SetCompatibleTextRenderingDefault(true). I have tested this in both VS C# 2008 and .NET 3.5, and VS C# 2010 with .NET 4.0, and on Windows 7 and Vista.

A quick workaround is to just add some code to the Label's Paint event, which can be shared among any number of Label controls (set AutoEllipsis=true, AutoSize=false):

private void someLabel_Paint(object sender, PaintEventArgs e)
{
    // This code requires a ToolTip control on the form named toolTip.

    Label thisLabel = sender as Label;
    // Set a rectangle size with same width and larger height than label's.
    SizeF layoutSize = new SizeF(thisLabel.Width, thisLabel.Height + 1);
    // Get the actual size of rectangle needed for all of text.
    SizeF fullSize = e.Graphics.MeasureString(thisLabel.Text, thisLabel.Font, layoutSize);
    // Set a tooltip if not all text fits in label's size.
    if (fullSize.Width > thisLabel.Width || fullSize.Height > thisLabel.Height)
    {
        toolTip.SetToolTip(thisLabel, thisLabel.Text);
    }
    else
    {
        toolTip.SetToolTip(thisLabel, null);
    }
}


Another workaround is to create a new label control that inherits Label, and overrides the OnPaint method with similar code, which is what I ended up doing.

-Jeff H
0 Kudos