0

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

3
Contributors
11
Replies
13
Views
5 Years
Discussion Span
Last Post by markdean.expres
Featured Replies
  • [B]2.Buttons,1.ListBox,new.Project[/B] [CODE]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 … Read More

0

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

0

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

0

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

Edited by codeorder: n/a

0

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
0

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?

1

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

Edited by codeorder: .TopIndex should be a ListBox.thing as well, i.pressume.:)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.