Back in the olden days of RB, if you wanted to truncate your text in a ListBox cell in the middle instead of at the end, you needed to write your own truncate routine and call it from the CellTextPaint event. However, I just discovered that using modern Xojo and doing this on Windows and Linux result in a forever recursive loop That sucks the CPU until you close the containing window. Here’s the code that I believe came from someone on the old NUG way back there.
In the CellTextPaint:
If column = 1 Then
// Get text from the Cell TAG in col 0
Dim s As String = Me.CellTag(row, column)
// Truncate text at center instead of the default at the end
Me.Cell(row,column) = TruncateString(s, g.Width - 8, Me.TextFont, Me.TextSize, True)
End If
The TruncateString method:
[code]// Truncates a string to the passed width (IN PIXELS), text font and size
// adds an ellipsis (…) at end or middle
If s = “” Then
Return s
End If
// Get string width in pixels
Dim sWidth As Integer = StringWidth( s, textFont, textSize )
// If string width is less or equal than the passed width, then return the string
If sWidth <= width Then Return s
Dim chars() As String = SplitB( s, “” ) // split s into chars
Dim iMax As Integer = Ubound( chars )
Dim newString As String
// Get width of ellipsis “…”
Dim eWidth As Integer = StringWidth( “…”, textFont, textSize )
// Get halfWidth
Dim halfWidth As Integer = width / 2
For i As Integer = 0 To iMax
newString = newString + chars( i )
Dim newWidth As Integer = StringWidth( newString, textFont, textSize )
If not middle Then
If newWidth + eWidth >= width Then
Return newString + “…”
End If
Else
If newWidth + eWidth >= halfWidth Then
newString = newString + “…”
Exit // exit loop
End If
End If
Next
If middle Then
// Get the end of the string and append it to new string
Dim endString As String
Dim leftWidth As Integer = StringWidth( newString, textFont, textSize )
For i As Integer = iMax DownTo 0
endString = chars( i ) + endString
Dim endWidth As Integer = StringWidth( endString, textFont, textSize )
If endWidth + leftWidth >= width Then
Return newString + endString
End If
Next
End If
Return s[/code]
A: Is this still required? Has an option been added to set the truncation to the middle?
2: Since the CellTextPaint is now a no-way option, does anyone know of a proper solution?
Or, if neither, does anyone know of a feature request for setting the truncate location? I don’t find anything, but I no longer assume that Feedback’s search is honest with me :D.