Automatic resizing columns in a listbox


I’ve been trying to get this to work. The columns resize perfectly as the user writes, but the text field doesn’t. Only when the focus is lost does the text field for that column get resized to be the size of the column. I’d like to know if I can change the width of the textfield on the fly during editing.

I’ve already tried setting the width of the Listbox.ActiveCell property to no avail. I’ve also tried refreshing, invalidating and even using EditCell to edit the cell ahead before switching back to the original cell in an attempt to get it to refresh. So far nothing has worked.

Anyone have some ideas?

I created this for my WebListBoxes:

Sub setListBoxColumnWidthsWAD(myWebListBox As WebListBox)
'set column widths proportional to their content length that add to 100% so the PDF printing works!
'there is a similar method for setting PDF file column widths

Dim columnCharacters(-1) As Integer 'wipe all array data
Dim totalCharacterWidth As Integer 'width of all column maximums to get 100%
Dim tempString As String

ReDim columnCharacters(myWebListBox.ColumnCount) 'set as 1-based array

'set a minimum size of one character so %'s work and empty columns don’t have zero width
for tempInt As Integer = 0 to myWebListBox.ColumnCount - 1
columnCharacters(tempInt + 1) = Max(10, len(myWebListBox.Heading(tempInt)))

'populate the array with the longest number of characters for each column
for tempInt As Integer = 0 to myWebListBox.RowCount - 1
for tempInt2 As Integer = 0 to myWebListBox.ColumnCount - 1
columnCharacters(tempInt2 + 1) = Max(columnCharacters(tempInt2 + 1), len(myWebListBox.Cell(tempInt, tempInt2)))

'get the total max widths to determine the % for each column
totalCharacterWidth = 0
for tempInt As Integer = 0 to myWebListBox.ColumnCount - 1
totalCharacterWidth = totalCharacterWidth + columnCharacters(tempInt + 1)

'determine the percentage width for each column
tempString = “”
for tempInt As Integer = 0 to myWebListBox.ColumnCount - 1
myWebListBox.ColumnWidth(tempInt) = Format(columnCharacters(tempInt + 1)/totalCharacterWidth * 100, “##0.00”) + “%” 'set the Listbox column width

End Sub

Sorry, but I don’t understand what your code is doing.

You’re rewriting the entire listbox there?

You take any existing WebListBox full of data and pass it to the method. The method will examine every row and column of the weblistbox’s cells and resize the columns to match the maximum amount of data in each column. For example, I fill a WebListBox from a recordSet then run this method to make it display the data proportional to it recordSet fields.

Interesting, thank you.