Vinayak D.Gaikwad
Vinayak D.Gaikwad

Reputation: 89

Forwarding to the next item in Listbox?

I am working in a VB WinForms Application. In a listbox named list has the following items:

Ant 
Cat
Ball
Dog
..

or

01
03
04
02
..

I want to go to the next item alphabetically or in a numeric sequence. For Example, the user selects 'Ball' and the then press a button then the selected item must change to 'Cat'. The list box isn't data bond.

Upvotes: 0

Views: 732

Answers (2)

Vland
Vland

Reputation: 4272

There are two problems to address in your question:

  1. you have unordered ListBoxes
  2. your ListBox could contain string values or numeric values (String/Integer) and you need to be able to find the next item in both cases.

eg.

"11"
"01"
"02"
"22"

or maybe something like

"11"
"1"
"111"
"2"
"22"

if you sort this list as String you're going to get 1, 11, 111, 2, 22 which is wrong, you need to sort it as Integer to get 1, 2, 11, 22, 111

My solution handles both string and numeric values, and it correctly sorts this kind of numeric list.

You can use this code in a button click event

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    'default type, string
    Dim T = GetType(String)

    'if every listbox item is a number, consider it as an integer (for sorting purposes)
    Dim numericList = (From x In ListBox1.Items Select x).All(Function(x) IsNumeric(x))
    'if true, use Integer as type
    If numericList Then
        T = GetType(Integer)
    End If

    'sort the list items based on type
    Dim sortedList = (From x In ListBox1.Items
                      Let y = Convert.ChangeType(x, T)
                      Order By y
                      Select x).ToArray

    'find the index of the current item
    Dim currentIndex = Array.IndexOf(sortedList, ListBox1.SelectedItem)

    'find the index of the next item (from the sorted list)
    Dim nextSortedIndex = currentIndex + 1

    'check that the next index exists (otherwise we get an exception) 
    If nextSortedIndex >= ListBox1.Items.Count Then
        Exit Sub
    End If

    'find the next listbox index to select
    Dim nextItem = sortedList.ElementAt(nextSortedIndex)
    Dim nextListIndex = ListBox1.Items.IndexOf(Convert.ToString(nextItem))

    'select the new item
    ListBox1.SelectedIndex = nextListIndex
End Sub

Upvotes: 1

Shell
Shell

Reputation: 6849

You can do like this

  1. Get the sorted array of listbox Items
  2. Find the Index from sorted Array of Selected Item : found Index
  3. If the last index of array is more than found Index then add the +1 into that Index
  4. Now, get the next item from the Sorted Array by found Index : found Item
  5. Find the Index of that found Item from the ListBox using FindStringExact() method
  6. if the returned index is not -1 then set that index to SelectedIndex property.

Dim items() As String = listBox1.Items.Cast(Of String)().OrderBy(Function(x) x).ToArray()
Dim iIndex As Integer = -1
If ListBox1.SelectedIndex <> -1 Then
    iIndex = Array.IndexOf(items, ListBox1.SelectedItem.ToString())
End If
If iIndex >= (items.Length - 1) Then iIndex = -1
Dim strItem As String = items(iIndex + 1)

iIndex = ListBox1.FindStringExact(strItem, ListBox1.SelectedIndex)
If iIndex > -1 Then
    ListBox1.SelectedIndex = iIndex
End If

In second parameter of FindStringExact() method We have assigned listBox1.SelectedIndex coz even the item is duplicate with different index then it should check after that item.

Upvotes: 0

Related Questions