【问题标题】:Forwarding to the next item in Listbox?转发到列表框中的下一项?
【发布时间】:2014-08-24 14:29:51
【问题描述】:

我正在使用 VB WinForms 应用程序。在名为 list 的列表框中有以下项目:

Ant 
Cat
Ball
Dog
..

01
03
04
02
..

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

【问题讨论】:

  • 如果您对列表框进行排序,您所需要做的就是转到下一项
  • @Plutonix 再次阅读问题

标签: vb.net winforms listbox


【解决方案1】:

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

  1. 你有无序的列表框
  2. 您的 ListBox 可能包含 string 值或 numeric 值 (String/Integer),您需要能够在这两种情况下找到下一项。

例如。

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

或者类似的东西

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

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

我的解决方案同时处理字符串和数值,并且可以正确排序这种数值列表。

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

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

【讨论】:

  • @Shell 了解什么?我只是处理这两种情况。 1,2,11,2201,11,02,221,11,002,022,222 之类的列表都得到了正确排序。 op 将决定它是否有用
  • @Shell 我编辑了我的答案,现在应该更清楚了。它处理这两种格式,它只是不关心填充零,因为值在排序之前被转换为数字
  • 这是一个正确答案但我没看懂:'根据类型对列表项进行排序 Dim sortedList = (From x In ListBox1.Items Let y = Convert.ChangeType(x, T) Order按 y 选择 x).ToArray
  • 你甚至可以写一篇关于 CodeProject 的文章来展示它的使用。不适合我。
  • @user2642840 通常如果您尝试对此列表进行排序"1", "2", "11", "3",因为它们是字符串,它们的排序方式如下:"1","11","2","3"。函数Convert.ChangeType 告诉将它们考虑为特定类型(在本例中为整数),以便可以将它们排序为"1","2","3",11
【解决方案2】:

你可以这样做

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

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

FindStringExact() 方法的第二个参数中,我们已经分配了listBox1.SelectedIndex,因为即使该项目与不同的索引重复,那么它应该在该项目之后检查。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多