954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Listbox change selectedindex value

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?

Thanks

markdean.expres
Junior Poster
152 posts since Aug 2010
Reputation Points: 10
Solved Threads: 1
 

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

button2.click:
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

NewUserVB.Net
Light Poster
31 posts since Dec 2011
Reputation Points: 10
Solved Threads: 1
 

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

codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 

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

NewUserVB.Net
Light Poster
31 posts since Dec 2011
Reputation Points: 10
Solved Threads: 1
 

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.:)

codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 

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

NewUserVB.Net
Light Poster
31 posts since Dec 2011
Reputation Points: 10
Solved Threads: 1
 

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
codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 

yeah this looks better, so wait the post of markdean.express

NewUserVB.Net
Light Poster
31 posts since Dec 2011
Reputation Points: 10
Solved Threads: 1
 

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
Apple
Guava
Pineapple

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


Guava
Apple
Pineapple

Got it?

markdean.expres
Junior Poster
152 posts since Aug 2010
Reputation Points: 10
Solved Threads: 1
 

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

codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 

2.Buttons,1.ListBox,new.Project

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 '// lights.off.
        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 f.ck out since done.
        End With
        '// u.still here? :-\; Then get the f.ck out. ;)
    End Sub
#End Region '===-----===-----===-----'

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        loadMyCoolApp()
    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
        toogleUpDown()
    End Sub
End Class
codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 

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

markdean.expres
Junior Poster
152 posts since Aug 2010
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: