Can't resize based on column header width...

I wrote a routine to adjust the widths of a listbox to the contents…
i.e. Each column is made wide enough to show the entire contents of the columns cells, based on the largest cell… be it the header title or the cell contents.
However it just doesn’t seem to count the Column header and I can’t figure out why!
P.S. If the column title is : then it is an invisible column…

[code]Sub AdjustColumnWidths(Extends lb as Listbox)
dim r,c as Integer
dim pic as new Picture(1,1,32)
dim g as graphics = pic.Graphics
dim w as integer
dim colmax as integer
dim cw as string = “”
dim colmin as integer = 25
dim rows, cols as integer
dim cc as string

rows = lb.ListCount-1
cols = lb.ColumnCount-1

g.TextFont = lb.TextFont
g.TextSize = lb.TextSize

for c=0 to cols-1 'The last column occupies the remaining space. *
if lb.Heading© = “:” then
colmax = 0
else
colmax = g.StringWidth(lb.Heading©)
for r=0 to rows
cc = lb.Cell(r,c)
w = g.StringWidth(cc)
if w > colmax then colmax = w
next r
if colmax < colmin then colmax = colmin
end if
if colmax <> 0 then
cw = cw + str(colmax+8) + “,”
else
cw = cw + “0,”
end if
next c
cw = left(cw, len(cw)-1) 'trim the extra , *

if lb.sortedColumn > -1 then // the listbox is sorted by a column
if lb.LastIndex > 0 then
lb.sort// sort the listbox data using the current sort settings
end if
end
lb.ColumnWidths = cw

End Sub
[/code]

Seems to work fine here as long as you remember to call it when the list changes contents etc

It the contents of the column are less (in width) than the width of the column title the column title seems to shrink and the text is truncated.

Column widths need to account for the padding necessary on either side of the text. For column data, that’s about 2 pixels on each side. Column headers need more padding, like 5 pixels on each side. Your code should be more like

colmax = g.StringWidth(lb.Heading(c)) + 10
for r=0 to rows
   cc = lb.Cell(r,c)
   w = g.StringWidth(cc) + 4
   if w > colmax then colmax = w
next r

thats about what I was going to post :stuck_out_tongue:
I ended up peeking & it looks like 10 - 12

if colmax <> 0 then cw = cw + str(colmax+10) + "," else

Did the trick.