[Listbox] Slow populating using LB.Column(LoopIdx).WidthActual = Integer

I was fooling around in a SQLite reporting project and changed bits here and there.

Fortunately, I made backups and Comments “previous” code before changes…

The code below is far slower than the other (and prior) variant, bar bottom.

The deal is to set the same width to all Columns to set the Listbox in mode 1 (or is it mode 0 ?) : enable the horizontal ScrollBar.

Slow code:

[code] LB_Records.ColumnCount = rc

// Fills the ListBox Headings
For LoopIdx = 0 To rc
// Read and set the current Record
LB_Records.Heading(LoopIdx) = rs.IdxField(1).StringValue // Only PNom, Nom, Wife, etc. is available here…

// Get the next Record
rs.MoveNext

// Change the current Column width to 100 pixels
LB_Records.Column(LoopIdx).WidthActual = 100

Next[/code]

Faster code:

[code]LB_Records.ColumnCount = rc

// Fills the ListBox Headings
For LoopIdx = 0 To rc
// Read and set the current Record
LB_Records.Heading(LoopIdx) = rs.IdxField(1).StringValue

// Get the next Record
rs.MoveNext

// [2016-07-22] Compute the Listbox ColumnWidth string
ColWidths = ColWidths + "100,"

Next

// -------- -------- -------- -------- -------- -------- -------- --------
// [2016-07-22] Added the ColumnWidths value [to change the Listbox mode]
// Activate the horizontal ScrollBar

// [2016-07-22] Remove the ending comma
ColWidths = Left(ColWidths,Len(ColWidths)-1)

// [2016-07-22] Set the Listbox ColumnWidth string
LB_Records.ColumnWidths = ColWidths[/code]

Isn’t it strange ?

I commented my slow code and keep the old one !

Nope, not at all. String concatenation is not fast. Instead, append the values to a string array and join them at the end.

Ups ! My fault ! Wrong report.

The string concatenation is faster than the one liner:

LB_Records.Column(LoopIdx).WidthActual = 100
  // Change the current Column width to 100 pixels
    LB_Records.Column(LoopIdx).WidthActual = 100

I would think this would be slower because it resets the width of each column multiple times, and each one forces a listbox redraw if I’m not mistaken. While you second method caches that values until the end… and applies and redraws once.

Dave:

thanks…

and your comment looks like what I saw !

I may misuse the single line command (or find the case it must not be used…)