Guys I have this kind of problem where in I have a listbox and two buttons. Each of my button is labeled UP and DOWN. When I press UP, the current item in my listbox will be moved one level (or index) up and when I press DOWN button the current item goes one level down. I have been trying to manipulate the selectedindex property but it isn't working. Can anyone help?


Recommended Answers

All 11 Replies

try with
dim L1SI as string = Listbox1.selectedIndex.ToString
listbox1.item.SelectedIndex = L1SI - 1
dim L1SI as string = Listbox1.selectedIndex.ToString
listbox1.item.SelectedIndex = L1SI + 1

If this doesn't worked then we have to change "listbox1.item.SelectedIndex"... post if you need for help or if this doesn't worked

What exactly is the issue?
.moving the items up/down?
.or selecting the previously selected.Item with a new Index?

he wants to scroll up down the listbox i think....

Thanks for input NewUserVB.Net, though I'll wait for the o.p's(original.poster's) input on this; only makes sense Not to go way out of my way, when I possibly only have to walk a few steps Then be back on my original.path.:)

Oke... :-) but is correct the code that I posted?

Not really, though close.

With ListBox1
            If Not .SelectedIndex = -1 Then '// if .item selected.
                Dim L1SI As String = .SelectedItem
                '// run code here for up/down.
                .SelectedItem = L1SI
            End If
        End With

yeah this looks better, so wait the post of

guys, I was so surprised that many of you are helping. Thanks

what I want to accomplish is, like this;

I have three items in my listbox. say

now, say for instance, the currently selected item is Apple

now I have two buttons, labeled UP and Down. If I click on Down, The selected item (Apple) will be moved to the next index. So after clicking Down, the arrangement of the items will become


Got it?

>>Got it?
.will try to get it.Then reply.:)


Imports System.IO
Public Class Form1
#Region "-----===-----===-----=== DECLARATIONS ÆND STUFF ===-----===-----===-----"

    Private btnUp, btnDown As Button, lbMain As ListBox '// If you need control, you make your own.
    Private iT, iM As Integer, sT As String '// TEMP sh.t.
#End Region '===-----===-----===-----'

#Region "-----===-----===-----=== START.UP ÆND SH.T===-----===-----===-----"

    Private Sub loadMyCoolApp()
        Me.Font = New Font("verdana", 10, FontStyle.Bold) '// just.because.
        btnUp = Button1 : With btnUp : .Text = "^" : .Cursor = Cursors.Hand : End With '// take control of code.
        btnDown = Button2 : With btnDown : .Text = "v" : .Cursor = Cursors.Hand : End With '// loose control of code. xD
        lbMain = ListBox1 : With lbMain.Items : .Add("Guava") : .Add("Apple") : .Add("Pineapple") '// set.load ListBox.
            If Not .Count = 0 Then lbMain.SelectedIndex = 0 Else toogleUpDown()
        End With
    End Sub
    Private Sub toogleUpDown()
        iT = lbMain.SelectedIndex : btnUp.Enabled = False : btnDown.Enabled = False '//
        If Not iT = -1 Then '// if item selected Then lights.on.
            If iT > 0 Then btnUp.Enabled = True
            If iT < lbMain.Items.Count - 1 Then btnDown.Enabled = True
        End If
    End Sub
    Private Sub relocateItems(ByVal selBtn As Button)
        With lbMain
            sT = .SelectedItem '// get.Item Value.
            iM = .SelectedIndex  '// get.Item Index and some.
            .Items.Remove(.SelectedItem) '// .Remove.Item.
            If selBtn Is btnUp Then .Items.Insert(iM - 1, sT) '// .insert.Item. why -1?, cause it worx.xD
            If selBtn Is btnDown Then .Items.Insert(iM + 1, sT) '// .insert.Item. why +1?, cause it worx.xD
            .SelectedItem = sT '// highlight item again.
            Exit Sub '// get the out since done.
        End With
        '// u.still here? :-\; Then get the out. ;)
    End Sub
#End Region '===-----===-----===-----'

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    End Sub

    Private Sub _coolButton_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click, Button2.Click
        relocateItems(CType(sender, Button))
    End Sub

    Private Sub _ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    End Sub
End Class

I guess the first one helped me... Thanks everyone guys! This is solved already

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.