none
move up item in listbox RRS feed

  • Question

  • move up item in listbox contain

    "Combined"

    to first index and select it

    i used this code put not work

       For i As Integer = 0 To ListBox1.Items.Count - 1
                If ListBox1.Items(i).ToString.Contains("Combined") Then
                    Dim index As Integer = i
                    ListBox1.SelectedItem = index
                    If index > 0 Then
                        ListBox1.Items.Insert(index, ListBox1.SelectedItem)
                        ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
                        ListBox1.SelectedIndex = index
                    End If
                End If
            Next


    • Edited by monemas Monday, February 26, 2018 5:08 PM not complete
    Monday, February 26, 2018 5:07 PM

Answers

  • Try this too:

    For i As Integer = 0 To ListBox1.Items.Count - 1
       If ListBox1.Items(i).ToString.Contains("Combined") Then
          If i <> 0 Then
             Dim z = ListBox1.Items(i)
             ListBox1.Items.RemoveAt(i)
             ListBox1.Items.Insert(0, z)
          End If
          ListBox1.SelectedIndex = 0
          Exit For
       End If
    Next
    

    • Marked as answer by monemas Monday, February 26, 2018 7:41 PM
    Monday, February 26, 2018 7:18 PM
  • I know the idea of your code
    But I want to based on the word in item to move the
    itemto be first

    Hi

    OK then, try this. 

    ' Form1 with ListBox1 containing some
    ' random values, and Button1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    	MoveIt("Three")
      End Sub
      Sub MoveIt(s As String)
    	ListBox1.Items.Remove(s)
    	ListBox1.Items.Insert(0, s)
    	ListBox1.SelectedIndex = 0
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by monemas Monday, February 26, 2018 7:41 PM
    Monday, February 26, 2018 7:18 PM

All replies

  • Hi

    Here is one way

    ' Form1 with ListBox1 containing some
    ' random values, and Button1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If ListBox1.SelectedIndex < 0 Then Exit Sub
        Dim s As Object = ListBox1.SelectedItem
        ListBox1.Items.Remove(s)
        ListBox1.Items.Insert(0, s)
        ListBox1.SelectedIndex = 0
      End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Monday, February 26, 2018 5:20 PM Added select of Item 0
    Monday, February 26, 2018 5:17 PM
  • Hi

    Here is one way

    ' Form1 with ListBox1 containing some
    ' random values, and Button1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If ListBox1.SelectedIndex < 0 Then Exit Sub
        Dim s As Object = ListBox1.SelectedItem
        ListBox1.Items.Remove(s)
        ListBox1.Items.Insert(0, s)
        ListBox1.SelectedIndex = 0
      End Sub
    End Class


    Regards Les, Livingston, Scotland


    I know the idea of your code
    But I want to based on the word in item to move the
    itemto be first
    Monday, February 26, 2018 5:24 PM
  • Are you trying to move multiple items that contain "Combined" or one instance of "Combined" ?

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, February 26, 2018 5:31 PM
    Moderator
  • Are you trying to move multiple items that contain "Combined" or one instance of "Combined" ?

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    one item

    put idont  no number index in listbox


    • Edited by monemas Monday, February 26, 2018 5:36 PM
    Monday, February 26, 2018 5:34 PM
  • You cannot do this with a for index loop. You are all the time changing the index and therefore direct as you do this you are in trouble. 

    You can do this using a do while. 

    https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/do-loop-statement


    Success
    Cor

    Monday, February 26, 2018 6:26 PM
  • I know the idea of your code
    But I want to based on the word in item to move the
    itemto be first

    Hi

    OK then, try this. 

    ' Form1 with ListBox1 containing some
    ' random values, and Button1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    	MoveIt("Three")
      End Sub
      Sub MoveIt(s As String)
    	ListBox1.Items.Remove(s)
    	ListBox1.Items.Insert(0, s)
    	ListBox1.SelectedIndex = 0
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by monemas Monday, February 26, 2018 7:41 PM
    Monday, February 26, 2018 7:18 PM
  • Try this too:

    For i As Integer = 0 To ListBox1.Items.Count - 1
       If ListBox1.Items(i).ToString.Contains("Combined") Then
          If i <> 0 Then
             Dim z = ListBox1.Items(i)
             ListBox1.Items.RemoveAt(i)
             ListBox1.Items.Insert(0, z)
          End If
          ListBox1.SelectedIndex = 0
          Exit For
       End If
    Next
    

    • Marked as answer by monemas Monday, February 26, 2018 7:41 PM
    Monday, February 26, 2018 7:18 PM