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

**URL:** <https://forum.xojo.com/t/cant-resize-based-on-column-header-width/12589>\
**Category:** General\
**Created:** [November 25, 2013, 11:00pm UTC](https://forum.xojo.com/t/cant-resize-based-on-column-header-width/12589 "2013-11-25T23:00:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian\_O\_Brien](https://forum.xojo.com/user_avatar/forum.xojo.com/brian_o_brien/32/20096_2.png) [@Brian\_O\_Brien](https://forum.xojo.com/u/Brian_O_Brien)\
**Post date:** [November 25, 2013, 11:00pm UTC](https://forum.xojo.com/t/cant-resize-based-on-column-header-width/12589/1 "2013-11-25T23:00:24Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 4:57pm UTC](https://forum.xojo.com/t/cant-resize-based-on-column-header-width/12589/2 "2020-10-29T16:57:33Z")

</div>


