10163 : How can I check that text size (in pixels) are exceeding the column width (in pixles).

Question

I am showing tooltip on mouse over on a grid cell by using following code:

    Private Sub gantt1_OnGridMouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Gantt1.OnGridMouseMove
        Try
            Dim cell As cell = Gantt1.Grid.GridStructure.CellFromXY(e.X, e.Y)
            Dim tooltiptext As String = “”
            If Not cell Is Nothing Then
                If Not cell.Content.UserReference Is Nothing Then
                    tooltiptext = cell.Content.UserReference
                End If
            End If
            Me.TimelineTip.Active = True
            Me.TimelineTip.SetToolTip(Gantt1.Grid, tooltiptext)
        Catch ex As Exception
            GetLogger(Me).Error(ex)
        End Try
    End Sub

Question: This display tooltip on desired cells perfectly but now I want to show tooltip ONLY when the node text in the cell is not fit within. In other words I want to show tooltip ONLY when cell text is not completely visible and have to resize column to see alll text. How can I check that text size (in pixels) are exceeding the column width (in pixles).

Answer

You can meassure the text and compare it to the Grid.Width;

// Get handle to form.
IntPtr hwnd = new IntPtr();
hwnd = thisForm.Handle;
// Create new graphics object using handle to window.
Graphics newGraphics = Graphics.FromHwnd(hwnd);

SizeF textSize = new SizeF();
textSize=newGraphics.MeasureString(Cell.Content.Value,Cell.Layout.Font);

// Dispose of new graphics.
newGraphics.Dispose();

Note: it will probably be a good idea to get the hwnd, or even the Graphics object, one time and keep it, and not ask for it for every measure…

 

Leave a Reply