INTERACT FORUM

Windows => Plug-in Development => Topic started by: KingSparta on October 21, 2006, 07:21:44 am

Title: VB6 Code Order A Listbox
Post by: KingSparta on October 21, 2006, 07:21:44 am
Some Code I Wrote To Order The Listbox

1. One Checked Listbox = List1
2. Two Cmd Buttons cmdMove_Click (Index 0 and 1)

Private Sub cmdMove_Click(Index As Integer)
    Dim strTemp As String
    Dim intPos As Integer
    Dim MySelectionChecked As Boolean
    '
    If List1.ListCount > 0 Then
        strTemp = List1.List(List1.ListIndex)
        intPos = List1.ListIndex
        MySelectionChecked = False
        If List1.Selected(intPos) = True Then
            MySelectionChecked = True
        End If
        If Index = 0 Then
            If intPos - 1 >= 0 Then
                List1.RemoveItem List1.ListIndex
                List1.AddItem strTemp, intPos - 1
                List1.Selected(intPos - 1) = MySelectionChecked
            End If
        Else
            If intPos + 1 < List1.ListCount Then
                List1.RemoveItem List1.ListIndex
                List1.AddItem strTemp, intPos + 1
                List1.Selected(intPos + 1) = MySelectionChecked
            End If
        End If
    End If
End Sub
Title: Re: VB6 Code Order A Listbox
Post by: Mr ChriZ on October 21, 2006, 09:37:09 am
I've added this to the list of useful code snippets  :)
Title: Re: VB6 Code Order A Listbox
Post by: KingSparta on October 22, 2006, 01:51:23 pm
A Slight Mod

This Will Allow You To Move A Selected Item Up Or Down, But It Will Roll Over From The Bottom To the Top And From the Top To the Bottom.

Note This Is A Checked Listbox

Private Sub cmdMove_Click(Index As Integer)

On Error Resume next

    If List1.ListCount < 1 Then
         Exit Sub
    End If

    Dim strTemp As String
    Dim intPos As Integer
    Dim MySelectionChecked As Boolean
    '
        strTemp = List1.List(List1.ListIndex)
        intPos = List1.ListIndex

        If List1.Selected(intPos) = True Then
            MySelectionChecked = True
        Else
            MySelectionChecked = False
        End If

        If Index = 0 Then
            If intPos - 1 >= 0 Then
                List1.RemoveItem List1.ListIndex
                List1.AddItem strTemp, intPos - 1
                List1.ListIndex = intPos - 1
                List1.Selected(intPos - 1) = MySelectionChecked
            Else
                List1.RemoveItem List1.ListIndex
                List1.AddItem strTemp
                List1.ListIndex = List1.ListCount - 1
                List1.Selected(List1.ListCount - 1) = MySelectionChecked
            End If
        Else
            If intPos + 1 < List1.ListCount Then
                List1.RemoveItem List1.ListIndex
                List1.AddItem strTemp, intPos + 1
                List1.ListIndex = intPos + 1
                List1.Selected(intPos + 1) = MySelectionChecked
            Else
                List1.RemoveItem List1.ListIndex
                List1.AddItem strTemp, 0
                List1.ListIndex = 0
                List1.Selected(0) = MySelectionChecked
            End If
        End If

End Sub