Copying an array into a ListBox easily? (VB6)



  • Is there an easy way to copy the contents of a dynamic array (which may be empty) into a ListBox? Tried lstBuddyList.List = gsSets(i).OLNames but that didn't work (since then, I've realised that arrays + Collections = FILE_NOT_FOUND).

    Preferably, it should also be able to be used in reverse.. I really don't want to use For loops, as I have a few of these I need to do and I don't want to overcomplicate my settings dialog too much.



  • I don't think there is any way to do this without a loop in VB6. You can use a For Each loop instead of a For loop though.



  • @PSWorx said:

    I don't think there is any way to do this without a loop in VB6. You can use a For Each loop instead of a For loop though.

    Okay, thanks. Although I haven't been able to test it because my code is rather complicated, does assigning user-defined types work in the same way I suggested, as in "gsSets(1) = gsLoadedSets(2)"?



  • Sorry for double-posting, but I've hit another problem.

    Is there a way to assign an ItemData to an item added to a ListBox, which has Sorted set to True?

    I tried this:

    Public Sub AddFilter(FilterText As String, WhatType As Byte)
      lstTextFilters.Sorted = False
      lstTextFilters.AddItem FilterText
      lstTextFilters.ItemData(lstTextFilters.ListCount - 1) = WhatType
      lstTextFilters.Sorted = True
    End Sub

    But it failed horribly. :( Is there any other way to do it? 



  • Didn't addItem return a reference to the added item? Or was that only for the common controls treeview? (Which you should use anyhow, as it has a ton of extra options, even if you only want lists)



  • @Daid said:

    Didn't addItem return a reference to the added item? Or was that only for the common controls treeview? (Which you should use anyhow, as it has a ton of extra options, even if you only want lists)

    Thanks anyway, but I managed to dig up how to do this (the NewIndex property name sure is descriptive!). This is only for a list of text filters in the Settings dialog, so I didn't feel a full-fledged treeview was necessary. (I will be using it for something else where I need graphical icons on each list item, though.) 



  • As for the user defined type question, I don't really know what you suggested but you can't use them like arrays. If you want to copy a specific property from one variable to another, use [code]var1.someprop = var2.someprop[/code]. If you want to copy UDT variables as a whole, I think you can simply write [code]var1 = var2[/code].

    If you feel that your program gets too complex because you have to do some non-trivial task in multiple places, that's the point where you need to introduce more subroutines. In the example above, you could for example define a [code]Sub FillListFromArray(a() as String, l as ListBox)[/code] and tuck away your list filling code there. That's good style in any case.



  • @PSWorx said:

    As for the user defined type question, I don't really know what you suggested but you can't use them like arrays. If you want to copy a specific property from one variable to another, use [code]var1.someprop = var2.someprop[/code]. If you want to copy UDT variables as a whole, I think you can simply write [code]var1 = var2[/code].

    If you feel that your program gets too complex because you have to do some non-trivial task in multiple places, that's the point where you need to introduce more subroutines. In the example above, you could for example define a [code]Sub FillListFromArray(a() as String, l as ListBox)[/code] and tuck away your list filling code there. That's good style in any case.

    Thanks, but I already knew that (my program uses arrays for settings types, such as "Public gsSets(1 To 20) As GSettings")

    I did actually make a Sub that converted ListBox to Arrays, and back. (I have several Modules full of subroutines) 



  • Aah, alright then. Sorry for misunderstanding :)


Log in to reply