DrawString & text truncation

rickarde

Newcomer
Joined
Apr 8, 2004
Messages
14
Location
Sweden
Hi!

I have a question about the drawString method of a Graphic object. The SDK doc says "...If the text does not fit inside the rectangle, it is truncated." But this don't seem to work for me. I'm implementing this method in an overloaded version of the Paint method in an inherited class of the DataGridTextBoxColumn class. In my datagrid the text, when it fills up more than the width of the cell (rectangle), is "multilined", in other words - it's placed in two rows.
The code I use in the paint method is as follows:'

Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal source As CurrencyManager, ByVal rowNum As Integer, ByVal backBrush As Brush, ByVal foreBrush As Brush, ByVal alignToRight As Boolean)
Dim rect As New RectangleF(bounds.X, bounds.Y, bounds.Width, bounds.Height)
Dim sf As New StringFormat
sf.LineAlignment = StringAlignment.Center
sf.Alignment = StringAlignment.Near

Dim e As DataGridFormatCellEventArgs
e = New DataGridFormatCellEventArgs(rowNum, Me._col, Me.DataGridTableStyle.DataGrid.Font, backBrush, foreBrush)
RaiseEvent SetCellFormat(Me, e)
If e.UseBaseClassDrawing Then
MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight)
Else
g.FillRectangle(e.BackBrush, bounds)
g.DrawString(Me.GetColumnValueAtRow(source, rowNum).ToString, e.TextFont, e.ForeBrush, rect, sf)

End If
If (e.TextFont Is Me.DataGridTableStyle.DataGrid.Font) = False Then
e.TextFont.Dispose()
End If

End Sub

Could the problem depend upon the StringFormatter that's used ?

Can anyone tell me how to get the text truncated instead of having it in extend the no of lines ?

One solution could be to measure the pixels of the text and then truncate it, when needed, and use an invisible data grid column containing the full text. I need the full text value since I'm showing a tool tip when hovering the mouse over the cell. But I was hoping for a smother solution...

/Rickard
 
Back
Top