Results 1 to 3 of 3

Thread: listbox change index + ubound index

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2001
    Location
    UK
    Posts
    99

    listbox change index + ubound index

    ok guys, i got a listbox right

    listbox1

    i got some items in their, and i select one, how to i change the index on the selected item to move it up in the listbox, gotta do it via a command button lol

    also, i need to find out what the last index is in the listbox, basicly so i can create an array using it
    myarray(Last Item In Listbox1 Index Number, 2)

    please help me hit squad

  2. #2
    Junior Member
    Join Date
    Jul 2004
    Location
    Port Huron, Michigan
    Posts
    20
    This is a demo of moving items up and down in a listbox:
    ----------------------------------------------------------------
    'lstTemp = the list on the form
    'btnUp = the button on the form to move items up in the list
    'btnDown = the button on the form to move items down in list
    'Enum Directions = the two ways an item can move
    'Sub MoveItem = generic method to move item up or down

    Public Enum Directions
    Up = -1
    Down = 1
    End Enum

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim intX As Integer
    For intX = 1 To 10
    lstTemp.Items.Add("Item " & intX.ToString())
    Next
    End Sub


    Private Sub btnUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUp.Click
    MoveItem(lstTemp, Directions.Up)
    End Sub

    Private Sub btnDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDown.Click
    MoveItem(lstTemp, Directions.Down)
    End Sub

    Private Sub MoveItem(ByVal List As ListBox, ByVal Direction As Directions)
    'get current item
    Dim intIndex As Integer = List.SelectedIndex

    'Make sure its moveable
    If intIndex = 0 And Direction = Directions.Up Then Exit Sub
    If intIndex = (List.Items.Count - 1) And Direction = Directions.Down Then Exit Sub

    'use an object to store one of the items you are switching
    'because a listbox can store objects not just strings
    Dim oTemp As Object
    oTemp = List.SelectedItem

    List.Items(intIndex) = List.Items(intIndex + Direction)
    List.Items(intIndex + Direction) = oTemp
    List.SelectedIndex = (intIndex + Direction)
    End Sub
    ---------------------------------------------------------

    Also (ListBox.Items.Count - 1) gets the last index in a listbox.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2001
    Location
    UK
    Posts
    99
    Greate, cheers buddy, exactly what i needed

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width