locked
move items up and down in a listbox RRS feed

  • Question

  • iam working in vb 2008 and i am trying to move the selected item in a listbox up or down,

     

    any help will be appreciated

    Monday, December 13, 2010 10:11 PM

Answers

  • it works for the up one how do i do the down one?


    Try this methods

    Sub MoveUpItem(ByVal listboxName As ListBox) 'move item up
            If listboxName.Items.Count > 0 Then
                Dim index As Integer = listboxName.SelectedIndex - 1
                If index >= 0 Then
                    listboxName.Items.Insert(index, listboxName.SelectedItem)
                    listboxName.Items.RemoveAt(listboxName.SelectedIndex)
                    listboxName.SelectedIndex = index

                End If
            End If
        End Sub
        Sub MoveDownItem(ByVal listboxName As ListBox) 'move item down
            If listboxName.Items.Count > 0 Then
                Dim index As Integer = listboxName.SelectedIndex + 1
                If index <= listboxName.Items.Count - 1 Then
                    listboxName.Items.Insert(index + 1, listboxName.SelectedItem)
                    listboxName.Items.RemoveAt(listboxName.SelectedIndex)
                    listboxName.SelectedIndex = index
                End If
            End If
        End Sub

    kaymaf 


    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    • Marked as answer by gajamaflake Monday, December 13, 2010 11:03 PM
    Monday, December 13, 2010 10:58 PM

All replies

  • it works for the up one how do i do the down one?
    Monday, December 13, 2010 10:41 PM
  • it works for the up one how do i do the down one?


    Try this methods

    Sub MoveUpItem(ByVal listboxName As ListBox) 'move item up
            If listboxName.Items.Count > 0 Then
                Dim index As Integer = listboxName.SelectedIndex - 1
                If index >= 0 Then
                    listboxName.Items.Insert(index, listboxName.SelectedItem)
                    listboxName.Items.RemoveAt(listboxName.SelectedIndex)
                    listboxName.SelectedIndex = index

                End If
            End If
        End Sub
        Sub MoveDownItem(ByVal listboxName As ListBox) 'move item down
            If listboxName.Items.Count > 0 Then
                Dim index As Integer = listboxName.SelectedIndex + 1
                If index <= listboxName.Items.Count - 1 Then
                    listboxName.Items.Insert(index + 1, listboxName.SelectedItem)
                    listboxName.Items.RemoveAt(listboxName.SelectedIndex)
                    listboxName.SelectedIndex = index
                End If
            End If
        End Sub

    kaymaf 


    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    • Marked as answer by gajamaflake Monday, December 13, 2010 11:03 PM
    Monday, December 13, 2010 10:58 PM