• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 916
  • Last Modified:

Moving Listbox Items up and down the list.

Hi all.

I am working on two buttons(up/down) that moves the selected.items to a new index(item to the new position in the list) - with the following code it works right up until the value is -1 or greater than number of indexes in the list - obviously, so how do set parameters so it can't go lower than 0 and greater than the number of indexes in the list? So I am trying the IF statement which helps, but it does not stop the movement beyond the limits - which throws error of course. Thanks for any help.
Dim int As Integer = lsbTasks.SelectedIndex
        If int = lsbTasks.Items.Count Then
            btnSTDown.Enabled = False
        ElseIf int <> lsbTasks.Items.Count Then
            btnSTDown.Enabled = True
            Try
                int = lsbTasks.SelectedIndex + 1
                Dim item As String = lsbTasks.SelectedItem
                lsbTasks.Items.Remove(item)
                lsbTasks.Items.Insert(int, item)
                lsbTasks.SetSelected(int, True)
            Catch
            End Try
        End If

Open in new window

0
Hawkvalley1
Asked:
Hawkvalley1
  • 2
  • 2
1 Solution
 
Christopher KileCommented:
When lsbTasks.SelectedIndex < lsbTasks.Items.Count - 1, btnSTDown should be enabled.
When lsbTasks.SelectedIndex > 0, btnSTUp should be enabled.
Correct your code to do this.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Right...I think these three routines should cover you nicely:
    Private Sub lsbTasks_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lsbTasks.SelectedIndexChanged
        Dim index As Integer = lsbTasks.SelectedIndex
        If index <> -1 Then
            btnSTUp.Enabled = (index > 0)
            btnSTDown.Enabled = (index < lsbTasks.Items.Count - 1)
        End If
    End Sub
 
    Private Sub btnSTUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSTUp.Click
        Dim index As Integer = lsbTasks.SelectedIndex
        If index > 0 Then
            Dim temp As String = lsbTasks.SelectedItem
            lsbTasks.Items.RemoveAt(index)
            lsbTasks.Items.Insert(index - 1, temp)
            lsbTasks.SelectedIndex = index - 1
        End If
    End Sub
 
    Private Sub btnSTDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSTDown.Click
        Dim index As Integer = lsbTasks.SelectedIndex
        If index <> -1 AndAlso index < lsbTasks.Items.Count - 1 Then
            Dim temp As String = lsbTasks.SelectedItem
            lsbTasks.Items.RemoveAt(index)
            lsbTasks.Items.Insert(index + 1, temp)
            lsbTasks.SelectedIndex = index + 1
        End If
    End Sub

Open in new window

0
 
Hawkvalley1Author Commented:
ok, this is what I have in the SelectedIndex_Changed event(1st part):
in the Button_Click event(2nd part):

It is working perfectly, thanks
'1st part
If lsbTasks.SelectedIndex < lsbTasks.Items.Count - 1 Then
            btnSTDown.Enabled = True
        Else
            btnSTDown.Enabled = False
        End If
        If lsbTasks.SelectedIndex > 0 Then
            btnSTUP.Enabled = True
        Else
            btnSTUP.Enabled = False
        End If
'2nd part
Dim int As Integer = lsbTasks.SelectedIndex + 1
        Dim item As String = lsbTasks.SelectedItem
        lsbTasks.Items.Remove(item)
        lsbTasks.Items.Insert(int, item)
        lsbTasks.SetSelected(int, True)

Open in new window

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Did you even look at my code?...   =)
0
 
Hawkvalley1Author Commented:
not til after I had already posted...

It is very well written, thanks...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now