Liberty Forge logo


 

Liberty Forge

Where Windows programs are hammered into
shape on the Liberty BASIC anvil

Tips & Snips


Manipulating multiple listbox arrays in a single sub is easier than you think

I devised this method when Liberty BASIC was still in version 1.4. Everything was done in gosubs then, It was a simple proceedure to adapt it for use with subs or functions.

Lets start with some code to preload our listbox arrays and some replacement info for the arrays...


Global replaceWord$
Dim array0$(25), array1$(25), array2$(25), array3$(25), array4$(25)

    For x=101 To 200
        If x=200 Then
            replaceWord$=replaceWord$+Str$(x)
        Else
            replaceWord$=replaceWord$+Str$(x)+","
        End If
    Next x

    For x=1 To 4
        For y=1 To 25
            Select Case x
                Case 1
                    array1$(y)=str$(y)
                Case 2
                    array2$(y)=str$((25*(x-1))+y)
                Case 3
                    array3$(y)=str$((25*(x-1))+y)
                Case 4
                    array4$(y)=str$((25*(x-1))+y)
            End Select
        Next y
    Next x

        

This fills array1( ) with the values 1 thru 25,
array2( ) with the values 26 thru 50,
array3( ) with 51 thru 75,
array4( ) with 76 thru 100.

Now we need our listboxes and we will open the window.


WindowWidth=455
WindowHeight=400
    ListBox #main.list1, array1$(), swapArray,  10, 10, 100, 350
    ListBox #main.list2, array2$(), swapArray, 120, 10, 100, 350
    ListBox #main.list3, array3$(), swapArray, 230, 10, 100, 350
    ListBox #main.list4, array4$(), swapArray, 340, 10, 100, 350
Open "Array Window" For Window As #main
    Print #main.list1, "SingleClickSelect"
    Print #main.list2, "SingleClickSelect"
    Print #main.list3, "SingleClickSelect"
    Print #main.list4, "SingleClickSelect"
    Print #main, "TrapClose quit"

Wait
        

next is our exit sub


Sub quit handle$
    Close #main
    End
End Sub
        

Finally our array handoff sub, Ahhhh I said handoff not handler, this is the secret to our array handling. What we are going to do is hand off our array's values to a neutral array, (by neutral I mean one that is not associated with a list box). Then we will process them in the neutral array and hand them back to the listbox array.


Sub swapArray handle$
    Select Case handle$
        Case "#main.list1"
            Print #main.list1, "SelectionIndex? index"
            setup=1
            For x=1 To 25
                array0$(x)=array1$(x)
            Next x
        Case "#main.list2"
            Print #main.list2, "SelectionIndex? index"
            setup=2
            For x=1 To 25
                array0$(x)=array2$(x)
            Next x
        Case "#main.list3"
            Print #main.list3, "SelectionIndex? index"
            setup=3
            For x=1 To 25
                array0$(x)=array3$(x)
            Next x
        Case "#main.list4"
            Print #main.list4, "SelectionIndex? index"
            setup=4
            For x=1 To 25
                array0$(x)=array4$(x)
            Next x
    End Select

    Call changeArrayValue setup, index

    Select Case handle$
        Case "#main.list1"
            For x=1 To 25
                array1$(x)=array0$(x)
            Next x
            Print #main.list1, "Reload"
            Print #main.list1, "Selectindex 0"
        Case "#main.list2"
            For x=1 To 25
                array2$(x)=array0$(x)
            Next x
            Print #main.list2, "Reload"
            Print #main.list2, "Selectindex 0"
        Case "#main.list3"
            For x=1 To 25
                array3$(x)=array0$(x)
            Next x
            Print #main.list3, "Reload"
            Print #main.list3, "Selectindex 0"
        Case "#main.list4"
            For x=1 To 25
                array4$(x)=array0$(x)
            Next x
            Print #main.list4, "Reload"
            Print #main.list4, "Selectindex 0"
    End Select
End Sub
        

Last but not least, (to show that this works), our array handler, (where changes are actually made). You will see that I pass setup, (which tells us which listbox has called the Sub), and index, (the item actually selected in the listbox).
Just for the fun of it, I am placing the original contents of the listbox into replaceWord$ where the replacement word is found. So that if you click the same item a second time the original contents will be placed back in the listbox.


Sub changeArrayValue s, i
    a$=array0$(i)
    array0$(i)=Word$(replaceWord$, ((s-1)*25)+i, ",")
    For x=1 To 100
        If x=((s-1)*25)+i Then
            newWord$=newWord$+","+a$
        Else
            newWord$=newWord$+","+Word$(replaceWord$, x, ",")
        End If
    Next x
    replaceWord$=Right$(newWord$, Len(newWord$)-1)
End Sub
        

I hope this makes multiple array handling easier for you

Download the Source Code


 

Previous Tips & Snips

Determining the Users O.S. API call to discover users O.S.

the NAME command complete with undocumented features.