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

清除列表框项目,搜索项目除外vb.net,.net,vb.net,.net,Vb.net,我使用下面的代码使用vb.NET2005查找列表框中的所有项目。但是,如何在搜索后从列表框中删除未搜索的项目 编辑:我包括了整个代码 Imports System.IO Public Class Form1 Public Sub New() InitializeComponent() ListBox1.SelectionMode = SelectionMode.MultiExtended End Sub Private Sub

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

编辑:我包括了整个代码

 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
创建一个包含所有项目的
列表
,并搜索该列表,然后只将搜索结果添加到列表框中

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

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


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

在我看来,您似乎想要这样的结果:

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

注意,我还在
Finally
块中调用了
ListBox1.EndUpdate()
。根据我的经验,这是一种很好的做法,因为它可以确保即使发生异常(您仍然可以根据自己的喜好处理异常),您的控件也会恢复到正常状态。

我觉得您需要这样的功能:

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


注意,我还在
Finally
块中调用了
ListBox1.EndUpdate()
。根据我的经验,这是一种很好的做法,因为它可以确保即使发生异常(您仍然可以随心所欲地处理),您的控件也会恢复到正常状态。

请澄清:什么是“未搜索项”?你能举一个例子说明列表的开头是什么,你有什么数据,以及它应该以什么形式结束吗?列表框显示了拖入其中的文件内容,每行一行。我正在使用文本框搜索列表框中的项目,当我单击按钮时,与搜索关键字匹配的所有行都会高亮显示,其他行不会高亮显示,但它们仍保留在其他行中。我想做的是,只要我键入searchbox并单击按钮,清除所有内容,列表框就应该只包含与关键字匹配的行。希望我清楚你是使用WinForms还是WPF?我使用的是VB.NET 2008 Windows应用程序不是2005对不起(在问题中)你能澄清一下:什么是“非搜索项”?你能举一个例子说明列表的开头是什么,你有什么数据,以及它应该以什么形式结束吗?列表框显示了拖入其中的文件内容,每行一行。我正在使用文本框搜索列表框中的项目,当我单击按钮时,与搜索关键字匹配的所有行都会高亮显示,其他行不会高亮显示,但它们仍保留在其他行中。我想做的是,只要我键入searchbox并单击按钮,清除所有内容,列表框就应该只包含与关键字匹配的行。希望我很清楚你是使用WinForms还是WPF?我使用的是VB.NET 2008 Windows应用程序不是2005对不起(在问题中),但问题是我正在读取大约3-7 mb的文件,并创建一个包含所有这些项目的列表?。。。。它是否挂起了我的应用程序?我正在通过dragdrop将文件加载到listbox中。你是否使用拖放功能将文件的所有项目添加到列表框中?@Tony:但我会在用户每次搜索时打开文件,这会延迟我的应用程序响应。正确的?他必须盯着应用程序看,不知道什么时候会完成。因此,您通过拖放添加到列表框中的每一项,也添加到列表中,然后搜索该列表中与您的textbox.text匹配的项,并且对于找到的每一项匹配项,都添加到列表框中,但在执行此操作之前,不要忘记先清除listbox。这不会减慢应用程序的运行速度,如果确实如此,请将其放在另一个线程中。但问题是我正在读取大约3-7 mb的文件,并创建一个包含所有这些项目的列表?。。。。它是否挂起了我的应用程序?我正在通过dragdrop将文件加载到listbox中。你是否使用拖放功能将文件的所有项目添加到列表框中?@Tony:但我会在用户每次搜索时打开文件,这会延迟我的应用程序响应。正确的?他必须盯着应用程序看,不知道什么时候会完成。因此,您通过拖放添加到列表框中的每一项,也添加到列表中,然后搜索该列表中与您的textbox.text匹配的项,并且对于找到的每一项匹配项,都添加到列表框中,但在执行此操作之前,不要忘记先清除listbox。这不会减慢应用程序的运行速度,如果确实如此,请将其放在另一个线程中。谢谢@dan,这是我的想法,尽管我的计划要长一点,因为我要删除所有项目,搜索包含项目的列表,并添加任何匹配项。所以你的做法更好!:)谢谢@dan,这是我的想法,虽然我的计划要长一点,因为我要删除所有项目,搜索包含项目的列表,并添加任何匹配项。所以你的做法更好!:)