Link to home
Start Free TrialLog in
Avatar of spallski
spallski

asked on

List boxes

This is a college project I have a list box with data already loaded using .addItem I then have four control buttons up, down, select all, select none and a check box which when ticked is supposed to sort the data into alphabetical order.
How do I get the highlighted data to move up or down to select all or none using the control buttons?
How do I get the check box to sort in alphabetical order?
Avatar of Deja-vu
Deja-vu

'This is the code.
'When you put a listbox in a form you can set the "SORTED" property to True or False, unfortunately this property can't be changed in Runtime. For these reason I use two ListBox, List1 and List2, you can set List1.Sorted=true and List2.Sorted=false and change what control do you see.
'Take a look in the Check_Click method. If you don't take attention to the sorted Property, replace all the cListVis references to List1 and delete all the List2.


'Sorry by the grammatical error, I speak spanish. I can talk in English but like Tarzan. Corrections are welcome.
'Good Luck.
'Déjà-vu


Public cListVis As Object, cListInv As Object
Private Sub btDown_Click()
    If cListVis.TopIndex < cListVis.ListCount - 1 Then cListVis.TopIndex = cListVis.TopIndex + 1
End Sub

Private Sub btUp_Click()
    If cListVis.TopIndex > 0 Then cListVis.TopIndex = cListVis.TopIndex - 1
End Sub

Private Sub Check1_Click()
    If Check1 Then
        Set cListVis = List1
        Set cListInv = List2
    Else
        Set cListVis = List2
        Set cListInv = List1
    End If
    cListVis.Visible = True
    cListInv.Visible = False
End Sub

Private Sub Form_Load()
    List1.AddItem ("One")
    List1.AddItem ("Two")
    List1.AddItem ("Three")
    List1.AddItem ("Four")
    List1.AddItem ("Five")
    List1.AddItem ("Six")
    List1.AddItem ("Seven")
    List2.AddItem ("One")
    List2.AddItem ("Two")
    List2.AddItem ("Three")
    List2.AddItem ("Four")
    List2.AddItem ("Five")
    List2.AddItem ("Six")
    List2.AddItem ("Seven")
    Set cListVis = List1
    Set cListInv = List2
End Sub

Private Sub SelAll_Click()
    For nit = 0 To cListVis.ListCount - 1
        cListVis.Selected(nit) = True
    Next
End Sub

Private Sub SelNone_Click()
    For nit = 0 To cListVis.ListCount - 1
        cListVis.Selected(nit) = False
    Next
End Sub
Private Sub BubbleSort(List() As String, Selected() As Boolean)
    Dim lUBound As Long
    Dim I As Long
    Dim J As Long
    Dim sTmp As String
    Dim bTmp As Boolean
   
    lUBound = UBound(List)
    For I = lUBound To 1 Step -1
        For J = 0 To I - 1
            If List(J) > List(J + 1) Then
                sTmp = List(J + 1)
                List(J + 1) = List(J)
                List(J) = sTmp
               
                bTmp = Selected(J + 1)
                Selected(J + 1) = Selected(J)
                Selected(J) = bTmp
            End If
        Next
    Next
End Sub

Private Function ReadList(lst As ListBox, List() As String, Selected() As Boolean) As Boolean
    Dim I As Long
   
    With lst
        If .ListCount > 0 Then
            ReDim List(.ListCount - 1)
            ReDim Selected(.ListCount - 1)
           
            For I = 0 To .ListCount - 1
                List(I) = .List(I)
                Selected(I) = .Selected(I)
            Next
            ReadList = True
        End If
    End With
End Function

Private Sub WriteList(lst As ListBox, List() As String, Selected() As Boolean)
    Dim I As Long
   
    With lst
        .Clear
       
        For I = 0 To UBound(List)
            .AddItem List(I)
            .Selected(I) = Selected(I)
        Next
    End With
End Sub

Private Sub chkSort_Click()
    Dim arrList() As String
    Dim arrSelected() As Boolean
   
    If chkSort.Value = vbChecked Then
        If ReadList(List1, arrList, arrSelected) Then
            BubbleSort arrList, arrSelected
            WriteList List1, arrList, arrSelected
        End If
       
        cmdUp.Enabled = False
        cmdDown.Enabled = False
    Else
        cmdUp.Enabled = True
        cmdDown.Enabled = True
    End If
End Sub

Private Sub cmdDown_Click()
    Dim I As Long
    Dim sTmp As String
   
    With List1
        For I = .ListCount - 2 To 0 Step -1
            If .Selected(I) And Not .Selected(I + 1) Then
                sTmp = .List(I + 1)
                .List(I + 1) = .List(I)
                .List(I) = sTmp
                .Selected(I + 1) = True
                .Selected(I) = False
            End If
        Next
    End With
End Sub

Private Sub cmdUp_Click()
    Dim I As Long
    Dim sTmp As String
   
    With List1
        For I = 1 To .ListCount - 1
            If .Selected(I) And Not .Selected(I - 1) Then
                sTmp = .List(I - 1)
                .List(I - 1) = .List(I)
                .List(I) = sTmp
                .Selected(I - 1) = True
                .Selected(I) = False
            End If
        Next
    End With
End Sub
Avatar of spallski

ASKER

Thanks for the replies Déjà-vu and Tigerzhao its very welcome. The reply from Tigerzhao flew over my head a little as I started programming about 7 weeks ago at college and we have not covered alot of that stuff yet so I was unsure how to apply it. Déjà-vu the for loop works for select none but not for select all. I have 15 files loaded using the .addItem method do I need to use an array for this? Using the for loop you provided only highlighted the bottom file. Any help would be greatly appreciated thanks guys my apologies to Tigerzhao as I'm just a beginner at this but am keen to learn.
you need design
List1.MultiSelect = 1 'Simple
Or
List1.MultiSelect = 2 ' Extended
Thankyou TigerZhoa tried extended worked first time that button is now working, the problem I have is with the remove button I can get it to  remove individual files but when all the files are highlighted it still removes one not them all any help would be appreciated........
I am a beginner at this as you probably can guess!!!
ASKER CERTIFIED SOLUTION
Avatar of TigerZhao
TigerZhao

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I applied that peice of code to my program worked first time thankyou TigerZhao you've been very helpful onto the next program now write a calculator program lot of things I dont quite understand yet but I'm sure I will get there thanks again TigerZhao
TigerZhao has made some very helpful comments