清除列表框项目,但搜索项目vb.net除外

时间:2010-01-02 00:44:54

标签: .net vb.net

我使用以下代码使用vb.net 2005查找列表框中的所有项目。但是如何在搜索后从列表框中删除未搜索的项目?

编辑:我包含了整个代码

 Imports System.IO

Public Class Form1
    Public Sub New()

         InitializeComponent()
        ListBox1.SelectionMode = SelectionMode.MultiExtended


    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim reader As StreamReader = New StreamReader("input.txt")
        Try
            Me.ListBox1.Items.Clear()
            Do
                Me.ListBox1.Items.Add(reader.ReadLine)
            Loop Until reader.Peek = -1

        Catch
            Me.ListBox1.Items.Add("File is empty")

        Finally
            reader.Close()
        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ListBox1.BeginUpdate()
        ListBox1.SelectedIndices.Clear()
        If TextBox1.Text.Length > 0 Then
            For index As Integer = 0 To ListBox1.Items.Count - 1
                Dim item As String = ListBox1.Items(index).ToString()

                If item.IndexOf(TextBox1.Text, StringComparison.CurrentCultureIgnoreCase) >= 0 Then

                    ListBox1.SelectedIndices.Add(index)
                End If
            Next
        End If
        ListBox1.EndUpdate()
    End Sub

    Private Sub ListBox1_DragEnter(ByVal sender As Object, ByVal e As  _
 System.Windows.Forms.DragEventArgs) Handles ListBox1.DragEnter
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            e.Effect = DragDropEffects.All
        End If
    End Sub

    Private Sub ListBox1_DragDrop(ByVal sender As Object, ByVal e As  _
    System.Windows.Forms.DragEventArgs) Handles ListBox1.DragDrop
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            Dim MyFiles() As String

            Me.ListBox1.Items.Clear()

            MyFiles = e.Data.GetData(DataFormats.FileDrop)
            Using sr As StreamReader = New StreamReader(MyFiles(0))
                Dim line As String

                Do
                    line = sr.ReadLine()

                    ListBox1.Items.Add(line)
                Loop Until line Is Nothing

            End Using

        End If
    End Sub
End Class

2 个答案:

答案 0 :(得分:1)

听起来像你想要这样的东西:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ListBox1.BeginUpdate()

    Try
        ' keep track of the "non-searched items" '
        Dim indicesToRemove As New List(Of Integer)

        ListBox1.SelectedIndices.Clear()
        If TextBox1.Text.Length > 0 Then
            For index As Integer = 0 To ListBox1.Items.Count - 1
                Dim item As String = ListBox1.Items(index).ToString()

                If item.IndexOf(TextBox1.Text, StringComparison.CurrentCultureIgnoreCase) >= 0 Then
                    ListBox1.SelectedIndices.Add(index)
                Else
                    ' this item was not searched for; we will remove it '
                    indicesToRemove.Add(index)
                End If
            Next

            ' go backwards to avoid problems with indices being shifted '
            For i As Integer = indicesToRemove.Count - 1 To 0 Step -1
                Dim indexToRemove As Integer = indicesToRemove(i)
                ListBox1.Items.RemoveAt(indexToRemove)
            Next
        End If
    Finally
        ListBox1.EndUpdate()
    End Try
End Sub

注意我还将ListBox1.EndUpdate()的调用放在Finally块中。根据我的经验,这是一个很好的做法,因为它确保即使在发生异常时你的控制也会恢复到正常状态(你仍然可以按照自己喜欢的方式处理)。

答案 1 :(得分:0)

创建一个包含所有项目的List<string>并搜索该列表,然后只将搜索结果添加到列表框中。

当然,您的列表框中的所有项目都可以开始,但也可以在列表中。然后清除列表框并在列表中搜索在文本框中输入的项目。现在,您可以将搜索结果添加到列表框中。