Automatic size of the columns of a listbox

Hi all!

Is there possible to automatically resize the width of the columns in a listbox, depending on the text length on each cell?

Thanks

I use this code (someone here supplied most of this, and I tweaked it a little) and it works great. Replace Listbox1 at the top with your Listbox name, of course and put this code in your Window’s Open area.

[code]dim source As Listbox = Listbox1 // your ListBox Name
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 = source.ListCount-1
cols = source.ColumnCount-1

g.TextFont = source.TextFont
g.TextSize = source.TextSize

for c=0 to cols

if cw <> "" then cw = cw + ","

if source.Heading(c) = ":"  then
  colmax = 0
else
  colmax = g.StringWidth(source.Heading(c))
  for r=0 to rows
    cc = source.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+15)
else
  cw = cw + "0"
end if

next c
source.ColumnWidths = cw[/code]

I know this is an old thread, but it has pointed me in the right direction.

I have managed to simplify the code a bit below, but even with the code above, I can’t figure out how to account for the indenting when a row is a subfolder/subitem of a hierarchal folder row.

For example, accounting for Item two and item three in the example below:

Item one
Item two
Item three

Here is my current code:

dim picBuffer as new picture(1, 1, 32) dim g as Graphics = picBuffer.Graphics g.textFont = lb.TextFont g.textSize = lb.TextSize dim txt as String dim y, yct, x, ct, MaxWidth as integer dim ColWidth, txtWidth as integer ct = lb.ColumnCount - 1 yct = lb.ListCount - 1 for x = 0 to ct ColWidth = lb.Column(x).WidthActual for y = 0 to yct txt = lb.Cell(y, x).Trim txtWidth = g.StringWidth(txt) if txtWidth > MaxWidth then MaxWidth = txtWidth end if next y if MaxWidth > ColWidth then lb.Column(x).WidthActual = MaxWidth end if MaxWidth = 0 next x

Note that will possibly redraw the listbox once for each column…
I found it best to accumulate the column width values and set them ONCE at the end of the outer loop

Why reinvent a square wheel when others have given you a round, pneumatic tire?