Listbox column sizing

The scroll bar do not show up when I use WidthActual, but if I use ColumnWidths on the listbox, the scrollbar does appear.

listbox1.ColumnWidths="100,100,..."

Or if you wanted to be able to set individual column widths, you could save the column sizes to an array, then anytime you change a width, apply the array to ColumnWidths.

Set a Property of:  colWidths(21) as string

// set the starting values for i as Integer = 0 to 21 colWidths(i) = "100" next listbox1.ColumnWidths = join(colWidths,",")

// set an individual column width and update colWidths(n) = "75" listbox1.ColumnWidths = join(colWidths,",")

Scott:

you are right (I think: I cannot check right now).

What I’ve done was concatenate the widths in the loop (If I recall properly) but I love your answer far better.

Scott, your hint made it work.

Thank you all for your help. I now have a horizontal scrollbar when i need one.

Another question: is there an easy way to reset the listbox and remove the horizontal scrollbar so that the columns shrink automatically to a size which fits the available space in the Window? I want to give the user the choice to switch between “horizontal Scrolling” and “fit to Window”. A simple Listbox.ScrollBarHorizontal = False does not solve this.

You can have a column fit in the available space by setting its width to “*”. But if you have many columns the result maybe strange.

Thanks Michel, now i can give the choice to my users with a simple button action.

I use a boolean Property ListboxScrolling.

  if ListboxScrolling = False then
    
    Listbox1.ScrollBarHorizontal = True
    
    dim columnWidths(21) as String
    
    for i as Integer = 0 to 21
      columnWidths(i) = "100"
      Listbox1.ColumnWidths = Join(columnWidths, ",")
    next
    
    ListboxScrolling = True
    
  Else
    
    Listbox1.ScrollBarHorizontal = False
    
    dim columnWidths(21) as String
    
    for i as Integer = 0 to 21
      columnWidths(i) = "*"
      Listbox1.ColumnWidths = Join(columnWidths, ",")
    next
    
    ListboxScrolling = False
    
  end if

Perfect for my needs.

Thanks all again.

Michael

Michael,
remember that using ‘*’ in ColumnWidths clears teh Horizontal ScrollBar.

What do you mean by “clears the horizontal Scrollbar” ? The Position?

No. It remove it. Just because the presence of an asterisk means you now use a variable width column.

Get back to how to add an horizontal scrollbar:
you have to set ON the property in the IDE
you have to have absolute values (in pixels) in ColumnWidths(…).

Sorry Emile. Didn’t get what you mean. My code does exactly what i was looking for. I can switch between horizontal scrolling and fitting all columns to the listbox-width.

I only added a Listbox1.ScrollPositionX = 0 to get rid of some display-issues in the Listbox when returning to non-scrolling Listbox:

  if ListboxScrolling = False then
    
    Listbox1.ScrollBarHorizontal = True
    
    dim columnWidths(21) as String
    
    for i as Integer = 0 to 21
      columnWidths(i) = "100"
      Listbox1.ColumnWidths = Join(columnWidths, ",")
    next
    
    ListboxScrolling = True
    
  Else
    
    Listbox1.ScrollBarHorizontal = False
    
    dim columnWidths(21) as String
    
    for i as Integer = 0 to 21
      columnWidths(i) = "*"
      Listbox1.ColumnWidths = Join(columnWidths, ",")
    next
    
    Listbox1.ScrollPositionX = 0
    
    ListboxScrolling = False
    
  end if

Sorry, but i did not get your point.

[quote=176532:@Michael Bzdega]Sorry Emile. Didn’t get what you mean. My code does exactly what i was looking for. I can switch between horizontal scrolling and fitting all columns to the listbox-width.
Sorry, but i did not get your point.[/quote]

If it works for you, do not listen to confuse comments. What is clearly thought out is clearly expressed.