ListView control - truncated text in columns - how can I tell?

  • Thread starter Thread starter JTeagle
  • Start date Start date
J

JTeagle

I have a list view control which, when too narrow widthways, truncates
the items automatically with '...'. This is exactly how I want it to
behave when it's not wide enough, but I wondered if anyone knew a way
to tell if it *has* truncated an item in a column for this reason? (I
want to widen the form it sits on as much as necessary to fit
everything in without truncating, if possible, and if the user wants
that behaviour.)

The long-winded way would be to grab the text, grab the font, measure
the text, and compare against the rectangle of the cell - but I was
hoping there was a less cumbersome way in VB!
 
The way which you described as "long-winded" seems to be the best solution
for this. I would hardly describe the method as "long-winded". It can be
achieved fairly simple...

Function TextFitColumn(ByVal CheckListView As ListView, ByVal Text As
String, ByVal ColumnIndex As Integer) As Boolean
Dim Measured As Size = TextRenderer.MeasureText(Text,
CheckListView.Font)
Return (Measured.Width < CheckListView.Columns(ColumnIndex).Width)
End Function

Returns True if 'Text' will fit within the width of 'ColumnIndex' in
'CheckListView', otherwise False.

Hope this helps,
Andrew
 
Back
Top