Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Move Items Up & Down In a Listbox (VB.Net)

Posted on 2003-10-23
5
Medium Priority
?
1,692 Views
Last Modified: 2008-07-08
I have a listbox with values and 2 buttons(UP & DOWN) to the side of it. Where a user can change the order of the values. I have been fooling around with it and can't get it to work. If they have an item selected and they keep pressing the UP button the item should move closer to the top.

Any help would be appreciated

0
Comment
Question by:idalton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 1

Accepted Solution

by:
johan_asplund earned 200 total points
ID: 9609217
Hello

I just made a small example that you can try

    Private Sub btnUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUp.Click

        Dim item As Object
        Dim index As Integer
        item = ListBox1.SelectedItem()
        index = ListBox1.Items.IndexOf(item)

        ListBox1.Items.Insert(index - 1, item)
        ListBox1.SelectedIndex = index - 1
        ListBox1.Items.RemoveAt(index + 1)


    End Sub

    Private Sub btnDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDown.Click
        Dim item As Object
        Dim index As Integer
        item = ListBox1.SelectedItem()
        index = ListBox1.Items.IndexOf(item)

        ListBox1.Items.Insert(index + 2, item)
        ListBox1.SelectedIndex = index + 2
        ListBox1.Items.RemoveAt(index)


    End Sub



Br
Johan
1
 
LVL 7

Expert Comment

by:Joe_Griffith
ID: 9610323
Here's what I do...

Note that I've filled the list box with objects of type clsSpListItem and the listbox in question is named lst.  

  Private Sub rptUp_Fire() Handles rptUp.Click
    Dim i As Integer
    Dim pi_Index As Integer
    Dim po_ListItem As clsSpListItem

    i = lst.SelectedIndex
    If i < 0 Then Exit Sub
    po_ListItem = CType(lst.Items(lst.SelectedIndex), clsSpListItem)

    i = i - 1
    If i >= 0 Then
      lst.Items.RemoveAt(lst.SelectedIndex)
      lst.Items.Insert(i, po_ListItem)
      lst.SelectedIndex = i
    Else
      Beep()
    End If
  End Sub

  Private Sub rptDown_Fire() Handles rptDown.Click
    Dim i As Integer
    Dim po_listitem As clsSpListItem

    i = lst.SelectedIndex
    If i < 0 Then Exit Sub
    po_listitem = CType(lst.Items(lst.SelectedIndex), clsSpListItem)

    i = i + 1
    If i <= lst.Items.Count - 1 Then
      lst.Items.RemoveAt(lst.SelectedIndex)
      lst.Items.Insert(i, po_listitem)
      lst.SelectedIndex = i
    Else
      Beep()
    End If
  End Sub
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 10224304
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: johan_asplund {http:#9609217}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

TheLearnedOne
EE Cleanup Volunteer
0
 

Expert Comment

by:ScottG_Work
ID: 21956937
I wanted to stop and take a few minutes to modify the above code  by Joe Griffith.  I searched for a while until I found this solution.  I have modified his code to allow both drag and drop features and well as changing the position of the item with the Up/Down Buttons.  Make sure that you set the .AllowDrop property of listbox1 to True and you should be in good shape.  This code also checks to make sure that an item is selected and that the index is within bounds of the total item count.  Thanks again to the original poster for this solution... :)

    Private Sub ListBox1_DragDrop(ByVal sender As Object, ByVal e As _ System.Windows.Forms.DragEventArgs) Handles ListBox1.DragDrop
        ListBox1.Items.Insert(ListBox1.IndexFromPoint (ListBox1.PointToClient(New Point(e.X, e.Y))), e.Data.GetData _(DataFormats.Text))
        ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
    End Sub
 
    Private Sub ListBox1_DragOver(ByVal sender As Object, ByVal e As _ System.Windows.Forms.DragEventArgs) Handles ListBox1.DragOver
        e.Effect = DragDropEffects.Move
    End Sub
 
    Private Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As _ System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown
        ListBox1.DoDragDrop(ListBox1.Text, DragDropEffects.All)
    End Sub
    Private Sub btnUp_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles btnUp.Click
 
        Dim item As Object
        Dim index As Integer
        If ListBox1.SelectedItem <> Nothing Then
            item = ListBox1.SelectedItem()
            index = ListBox1.Items.IndexOf(item)
 
            If index <> 0 Then
                ListBox1.Items.Insert(index - 1, item)
                ListBox1.SelectedIndex = index - 1
                ListBox1.Items.RemoveAt(index + 1)
            End If
        End If
 
    End Sub
 
    Private Sub btnDown_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles btnDown.Click
        Dim item As Object
        Dim index As Integer
        If ListBox1.SelectedItem <> Nothing Then
            item = ListBox1.SelectedItem()
            index = ListBox1.Items.IndexOf(item)
 
            If index <> ListBox1.Items.Count - 1 Then
                ListBox1.Items.Insert(index + 2, item)
                ListBox1.SelectedIndex = index + 2
                ListBox1.Items.RemoveAt(index)
            End If
        End If
    End Sub

Open in new window

0

Featured Post

Python: Series & Data Frames With Pandas

Learn the basics of Python’s pandas library of series & data frames and how we can use these tools for data manipulation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

722 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