Tooltip only when ellipsis is shown

Mar 3, 2011 at 8:34 AM

Hi,

How can I show a tooltip on a normal cell's text when the text in the cell is too long to fit and is shown with an ellipsis at the end? I tried it with a custom controller and

public override void OnMouseMove(SourceGrid.CellContext sender, MouseEventArgs e)
{
base.OnMouseMove(sender, e);

string displayText = sender.DisplayText;

but the displayText is the same as the full text of the cell, not the text as shown with the ellipses. Any idea?

 

Many thanks,

Pieter

Coordinator
Mar 4, 2011 at 10:45 AM

Sorry, i am not following. What are the ellipses that you mention?

Mar 4, 2011 at 10:55 AM

Dear Darius,

A "text with ellipsis" is something like "This is a long sentence that..." while the real text is "This is a long sentence that will not fit because the cell is too narrow". But the DisplayText and Value properties give me the full text, so I can not decide if the text will not fit. Do I have to calculate the length of the text in pixels and see if it is too long for the cell?

By the way, I find SourceGrid fantastic!

Pieter

Coordinator
Mar 8, 2011 at 9:59 AM

I think that's the only option - count the length and truncate the text. You could try look how SourceGrid is actually doing that for a hint

Apr 1, 2011 at 10:52 AM

Dear Darius,

Because of other programming activities, I found no time to write an answer earlier. I rewrote my code:

private class MouseOnCellController : SourceGrid.Cells.Controllers.ControllerBase
{
    public override void OnMouseMove(SourceGrid.CellContext sender, MouseEventArgs e)
    {
        base.OnMouseMove(sender, e);

        if (sender.Grid.Rows.Count > 0 && sender.Grid.Columns.Count > 0)
        {
            int mouseOverCol = sender.Position.Column;
            int mouseOverRow = sender.Position.Row;
            string tooltipText = sender.DisplayText;

            if (mouseOverCol >= 2 && tooltipText != string.Empty)
            {
                Size textSize = TextRenderer.MeasureText(tooltipText, sender.Grid.Font);
                if (textSize.Width > (sender.Grid.Columns.GetWidth(mouseOverCol) - 5| tooltipText.Contains(Environment.NewLine))
                {
                    sender.Grid.ToolTip.AutomaticDelay = 500;
                    sender.Grid.ToolTipText = tooltipText;
                }
            }
            else
            {
                sender.Grid.ToolTipText = string.Empty;
            }
        }
    }

    public override void OnMouseLeave(SourceGrid.CellContext sender, EventArgs e)
    {
        base.OnMouseLeave(sender, e);
        sender.Grid.ToolTipText = string.Empty;
    }
}

Pieter