Vb.net 是否转发到列表框中的下一项?

Vb.net 是否转发到列表框中的下一项?,vb.net,winforms,listbox,Vb.net,Winforms,Listbox,我在一个VB WinForms应用程序中工作。在名为list的列表框中,list包含以下项: Ant Cat Ball Dog .. 或 我想按字母顺序或数字顺序转到下一项。例如,用户选择“Ball”,然后按下按钮,则所选项目必须更改为“Cat”。列表框不是数据绑定 您的问题中有两个问题需要解决: 您有无序的列表框 您的列表框可以包含字符串值或数值值(字符串/整数),您需要能够在这两种情况下找到下一项 例如 或者类似的 "11" "1" "111" "2" "22" 如果您将此列表排序为S

我在一个VB WinForms应用程序中工作。在名为list的列表框中,list包含以下项:

Ant 
Cat
Ball
Dog
..


我想按字母顺序或数字顺序转到下一项。例如,用户选择“Ball”,然后按下按钮,则所选项目必须更改为“Cat”。列表框不是数据绑定

您的问题中有两个问题需要解决:

  • 您有无序的列表框
  • 您的列表框可以包含字符串值或数值值(
    字符串
    /
    整数
    ),您需要能够在这两种情况下找到下一项 例如

    或者类似的

    "11"
    "1"
    "111"
    "2"
    "22"
    
    如果您将此列表排序为
    String
    您将得到1,11,111,2,22这是错误的,您需要将其排序为
    Integer
    以获得1,2,11,22,111

    我的解决方案同时处理字符串和数字值,并对此类数字列表进行正确排序

    您可以在按钮
    单击
    事件中使用此代码

    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
    
    你可以这样做

  • 获取列表框项目的排序数组
  • 从所选项目的排序数组中查找索引:已找到索引
  • 如果数组的最后一个索引大于找到的索引,则将+1添加到该索引中
  • 现在,通过找到的索引从排序数组中获取下一项:找到的项
  • 使用
    FindStringExact()
    方法从列表框中查找该已找到项的索引
  • 如果返回的索引不是-1,则将该索引设置为
    SelectedIndex
    属性

  • Dim items()As String=listBox1.items.Cast(字符串的形式)().OrderBy(函数(x)x.ToArray()
    Dim iIndex作为整数=-1
    如果ListBox1.SelectedIndex-1,则
    iIndex=Array.IndexOf(items,ListBox1.SelectedItem.ToString())
    如果结束
    如果iIndex>=(items.Length-1),则iIndex=-1
    尺寸字符串=项目(iIndex+1)
    iIndex=ListBox1.FindStringExact(strItem,ListBox1.SelectedIndex)
    如果iIndex>-1,则
    ListBox1.SelectedIndex=iIndex
    如果结束
    

    FindStringExact()
    方法的第二个参数中,我们分配了
    listBox1。选择了dex
    z,因为即使项目是重复的,但索引不同,它也应该在该项目之后进行检查。

    如果对列表框进行排序,则只需转到下一个item@Plutonix阅读问题again@Shell要理解什么?我只是处理这两种情况。像
    1,2,11,22
    01,11,02,22
    1,11002022222
    这样的列表都能正确排序。op将决定它是否有用not@Shell我编辑了我的答案,现在应该更清楚了。它处理这两种格式,它只是不关心填充零,因为值在排序之前会转换为数字。这是一个正确的答案,但我不明白:“根据类型Dim sortedList=(从ListBox1中的x开始。项让y=Convert.ChangeType(x,t)你甚至可以写一篇关于CodeProject的文章来展示它的用途。不适合我。@user2642840通常情况下,如果您尝试对列表进行排序
    “1”、“2”、“11”、“3”
    ,因为它们是字符串,所以它们的排序如下:
    “1”、“11”、“2”、“3”
    。函数<代码>转换。ChangeType 告诉它们考虑特定类型(在这种情况下为整数),以便它们可以被分类为<代码>“1”、“2”、“3”、11 < /代码>。
    "11"
    "1"
    "111"
    "2"
    "22"
    
    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
    
    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