Using an Array to populate a ListBox (1 Viewer)

Bilbo_Baggins_Esq

Registered User.
Local time
Yesterday, 18:37
Joined
Jul 5, 2007
Messages
586
I have some code that successfully gathers some items and adds these items to a listbox, one item at a time using .AddItem
And it works ok.

The problem is, when there are a bunch of items to add, everytime the .AddItem runs, the form redraws.

I don't mind this terribly when there is only a few items.
but sometimes there are a bunch and this leads to a lot of flicking while the form redraws however many times .AddItem adds an item.

I've had the idea of building an array and then assigning the array to the listbox.

I can build the arrray no problem but i have not been able to assign the array to the listbox all at once.
All I can figure out how to do is add items to the listbox from the array one specific item ListArray(i) at a time.

Obviously this would work, but gains me nothing in terms of the visual effect in the form becuase it is still adding to the listbox one item at a time.

Is there some way to assign an array to a list box all at once, without doing it one item at a time?
 

jdraw

Super Moderator
Staff member
Local time
Yesterday, 19:37
Joined
Jan 23, 2006
Messages
15,379
If you know the items you're going to have in the listbox, why not create a list in vba and assign it as RowSource on form load, or other event? It would not do the add 1 item at a time. It might be easier and more focused if we saw the code you are currently using or contemplating.

Are you currently using a Requery after each Additem?
 

Bilbo_Baggins_Esq

Registered User.
Local time
Yesterday, 18:37
Joined
Jul 5, 2007
Messages
586
So, yes, the listbox is originally populated in the form OnLoad.

However, the initial lists of files is not refined.
Once the form is opened, there is a ComboBox for the user to select different file types and further refine the list.

Additionally, there default Drive originally searched in the form’s OnLoad may be also changed after the form is loaded by use of another ComboBox to change drives.

So, yes, initially, this use of a string variable or array for .AddItem does not matter.

The problem is once the form is opened, and changes made to the two ComboBoxes is where the problem lies.
Here is the code for the string method
Code:
Sub UpdateFilesListString()
On Error GoTo Error_In_Sub

Dim fso As New FileSystemObject
Dim fsoRoot As Folder
Dim str_Root As String, FileName As String

stg_Root = Me.txt_Path
Me.lst_Files.RowSource = ""

Set fsoRoot = fso.GetFolder(str_Root)

FileName = Dir(fso.BuildPath(fsoRoot.Path, Me.cmb_File_Type.Value), vbNormal Or vbHidden Or vbSystem Or vbReadOnly)

While Len(FileName) <> 0
    Me.lst_Files.AddItem FileName
    FileName = Dir()
    DoEvents
Wend
Exit Sub

Error_In_Sub:
MsgBox Err.Number & vbLf & Err.Description
End Sub
Here is the code for the Array method
Code:
Sub UpdateFilesListArray()
    On Error GoTo Error_In_Sub
    
    Dim ListArray() As Variant
    Dim fso As New FileSystemObject
    Dim fsoRoot As Folder
    Dim i As Integer
    Dim stg_Root As String, FileName As String
    
    
'    stg_Root = Me.txt_Path
    Me.lst_Files.RowSource = ""
    
Set fsoRoot = fso.GetFolder(stg_Root)
    
FileName = Dir(fso.BuildPath(fsoRoot.Path, Me.cmb_File_Type.Value), vbNormal Or vbHidden Or vbSystem Or vbReadOnly)

ReDim ListArray(1 To fsoRoot.Files.Count)
While Len(FileName) <> 0
     i = i + 1
     ListArray(i) = FileName
     Me.lst_Files.AddItem ListArray(i)
     FileName = Dir()
     DoEvents
Wend
ReDim Preserve ListArray(1 To fsoRoot.Files.Count)

Exit Sub
    
Error_In_Sub:
MsgBox Err.Number & vbLf & Err.Description

End Sub
So, obviously, I totally get, in this code, using an Array is pointless vs. just using the string.
However, the object here was trying to find a way to populate the ListBox ALL AT ONCE, potentially using an Array, after the matching items have been added to the array.
If an Array is not an option, then if there is some other type you might steer me towards, that would be cool too.
 

Users who are viewing this thread

Top Bottom