Solved

Moving Listbox Items up and down the list.

Posted on 2008-09-29
5
905 Views
Last Modified: 2012-05-05
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
Comment
Question by:Hawkvalley1
  • 2
  • 2
5 Comments
 
LVL 23

Expert Comment

by:Christopher Kile
ID: 22601099
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
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 22601530
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
 
LVL 9

Author Comment

by:Hawkvalley1
ID: 22601599
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22601650
Did you even look at my code?...   =)
0
 
LVL 9

Author Comment

by:Hawkvalley1
ID: 22601758
not til after I had already posted...

It is very well written, thanks...
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Zoom web page in asp.net 2 36
Starting to learn JAVA, 7 47
Paging GridView 7 34
VB.Net - Oracle BulkCopy from CSV Date Format 7 24
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now