Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 537111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:53:32+00:00 2026-05-13T09:53:32+00:00

I am using the below code to find all the items in a listbox

  • 0

I am using the below code to find all the items in a listbox using vb.net 2005. But how can remove the non searched items from the listbox after searching?

EDIT : I included the entire code

 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:53:33+00:00Added an answer on May 13, 2026 at 9:53 am

    It sounds to me like you want something like this:

    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
    

    Notice I also put the call to ListBox1.EndUpdate() in a Finally block. In my experience this is a good practice, as it ensures your control will return to a normal state even in the event of an exception (which you can still handle however you like).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 402k
  • Answers 402k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer That's a negative lookahead. It matches anything that doesn't start… May 15, 2026 at 4:46 am
  • Editorial Team
    Editorial Team added an answer Include <sstream> header May 15, 2026 at 4:46 am
  • Editorial Team
    Editorial Team added an answer Make your bool_xxx functions actually functors of a specific kind… May 15, 2026 at 4:46 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.