选择下一个重复的列表框item.net

时间:2018-08-20 13:17:00

标签: .net vb.net visual-studio-2012 listbox listboxitem

我尝试了几次,以选择下一个重复的行,并同时更改它,

例如

  

ListBox1项目:

1
1
1
2
1
3
4
5

我的代码(按钮1):

  

通过将第一行的第一个“ 1”更改为下一个,它将选择   另一个“ 1”,并将其也更改为“更改” ...等等。

  If TextBox1.Text = "1" Then

        ListBox1.SelectedItem = TextBox1.Text

        Dim indx As Integer = ListBox1.SelectedItem
        If indx <> -1 Then
            ListBox1.Select(indx, TextBox1.Text.Length  
        If ListBox1.SelectedItem <> "" Then
                ListBox1.SelectedIndices = "Changed"
            End If
        End If

    End If

我尝试过此方法,但是它选择了所有重复的项目,并且我希望每个按钮都单击一次。

Dim s As String = Me.TextBox1.Text
    Dim lb As ListBox = Me.ListBox1
    lb.SelectedIndex = -1
    If s.Length <> -1 Then


        For i As Integer = 0 To lb.Items.Count - 1

            Try
                If lb.Items(i).ToString.Contains(s) Then
                    lb.SelectedIndices.Add(i)


                Else

                End If
            Catch ex As Exception

            End Try

        Next
    End If

每按一次按钮将更改1个重复的项目行。

1 个答案:

答案 0 :(得分:1)

我不确定我是否了解您的需求,但是请尝试以下操作:

   Dim s As String = Me.TextBox1.Text
    Dim lb As ListBox = Me.ListBox1

    Dim selected = lb.SelectedIndex
    If s.Length <> -1 Then


        For i As Integer = selected + 1 To lb.Items.Count - 1
            If lb.Items(i).ToString = s Then
                lb.SelectedIndex = i
                Exit For
            End If
        Next
    End If