none
Search listbox using textbox RRS feed

  • Question

  • Hello im here again with one last question to complete my project. 

    I need to search in listbox using textbox. 

    Imagine my listbox contais:

    Andry

    Andry A

    Best

    Camaro

    Caliente

    When i type ca in textbox, listview shows camaro and caliente removing all other words. 

    If i type an listbox shows andry and andry a removing all other words. 

    Im using this leshay code to insert items from txt file to my listbox

    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim fn As String = "C:\Users\lesha\Desktop\testfile.txt"
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    		Dim lines() As String = IO.File.ReadAllLines(fn)
    		ListBox1.DataSource = lines
    
    	End Sub
    
    	Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
    		Dim s() As String = ListBox1.SelectedItem.ToString.Split(New String() {" ", "-"}, StringSplitOptions.RemoveEmptyEntries)
    		TextBox1.Text = s(0)
    		TextBox2.Text = s(1)
    	End Sub
    End Class

    Tuesday, October 1, 2019 7:13 PM

Answers

  • Hi

    No, the issue is not on your side - it is (was) here. Maybe fixed now. Give this version a try out and see.

    EDIT: needed to add a Split for Label2.Text

    EDIT: tidied up and got rid of unecessary code.

    ' Form1 with ListBox1, TextBox1
    ' Label1 and Label2
    Option Strict On
    Option Explicit On
    Public Class Form1
        Dim Words As New List(Of String)
        Dim LstWords As New List(Of String)
        Dim fn As String = "C:\Users\lesha\Desktop\testfile.txt"
        Dim Matches As New List(Of String)
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Words = IO.File.ReadAllLines(fn).ToList
            '    FakeData()
            FindMatches()
        End Sub
        Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
            FindMatches()
        End Sub
        Sub FindMatches()
            ListBox1.DataSource = Nothing
            LstWords.Clear()
            Matches.Clear()

            Matches.AddRange((From i In Words.Where(Function(word) word.ToString.ToLower.Contains(TextBox1.Text.ToLower)).ToList))

            LstWords.AddRange((From i In Matches Select i.Split("-"c)(0)).ToList)

            ListBox1.DataSource = LstWords
        End Sub
        Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
            If ListBox1.SelectedIndex < 0 Then Exit Sub
            Label1.Text = Split(Matches(ListBox1.SelectedIndex), "-")(0)
            Label2.Text = Split(Matches(ListBox1.SelectedIndex), "-")(1)
        End Sub

        Sub FakeData()
            ' delete this Sub if not needed
            Dim r As New Random
            Dim a As String = "abcdefghijklmnopqrstuvwxyz"
            Dim lst As New List(Of String)
            For i As Integer = 0 To 20000 - 1
                Dim init As String = StrConv(a.Substring(r.Next(0, a.Length), 1), vbProperCase) & "."

                Dim first As String = Nothing
                For j As Integer = 0 To r.Next(0, 9)
                    first &= a.Substring(r.Next(0, a.Length), 1)
                Next

                Dim num As String = r.Next(12345, 999999999).ToString

                Dim total As String = init & StrConv(first, vbProperCase) & "-" & num
                If Not lst.Contains(total) Then lst.Add(total)
            Next
            IO.File.WriteAllLines(fn, lst)
        End Sub
    End Class


    Regards Les, Livingston, Scotland




    • Edited by leshay Sunday, October 6, 2019 1:29 AM
    • Marked as answer by extream87 Sunday, October 6, 2019 11:31 AM
    Sunday, October 6, 2019 12:42 AM

All replies

  • Hi

    Use a ComboBox instead ofr a TextBox and you can do this

    Form1 with ListBox1, ComboBox1

    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim fn As String = "C:\Users\lesha\Desktop\testfile.txt"
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    		Dim lines() As String = IO.File.ReadAllLines(fn)
    		ListBox1.DataSource = lines
    
    		With ComboBox1
    			.DataSource = lines
    			.AutoCompleteMode = AutoCompleteMode.SuggestAppend
    			.AutoCompleteSource = AutoCompleteSource.ListItems
    		End With
    	End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Tuesday, October 1, 2019 7:51 PM
    Tuesday, October 1, 2019 7:50 PM
  • Since you do not have a question, you should make this post a discussion instead of question.


    Sam Hobbs
    SimpleSamples.Info

    Tuesday, October 1, 2019 8:01 PM
  • leshay thanks for all your help. Is working but if you have on listbox the name M. Leshay if you type Les you dont get any result because of M. behind. 

    Wednesday, October 2, 2019 8:13 AM
  • Hi,

    If you want this effect?

    Also, when you input "en", it will search for strings which contain "en", and then display them on the ListBox.


    If so, you can try my code as follows.

    Imports System.IO
    
    Public Class Form1
        Dim fn As String = "D:\testfile.txt"
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim lines() As String = IO.File.ReadAllLines(fn)
            ListBox1.DataSource = lines
        End Sub
    
        Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
            ListBox1.DataSource = Nothing
            ListBox1.Items.Clear()
            Using fs As FileStream = New FileStream(fn, FileMode.Open)
                Using m_streamReader As StreamReader = New StreamReader(fs)
                    m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin)
    
                    Dim strLine As String = m_streamReader.ReadLine()
                    Dim aa As String() = New String(100) {}
                    Dim shu As Integer = 0
    
                    Do
                        Dim split As String() = strLine.Split(vbCrLf)
                        strLine = m_streamReader.ReadLine()
    
                        Dim temp = split.Where(Function(s) s.Contains(TextBox1.Text))
                        For Each t As String In temp
                            ListBox1.Items.Add(t)
                        Next
                    Loop While strLine IsNot Nothing
                End Using
            End Using
        End Sub
    End Class

    Hope it will be helpful.

    Best Regards,

    Julie 


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, October 2, 2019 8:40 AM
    Moderator
  • Hello im here again with one last question to complete my project. 

    I need to search in listbox using textbox. 

    Imagine my listbox contais:

    Andry

    Andry A

    Best

    Camaro

    Caliente

    When i type ca in textbox, listview shows camaro and caliente removing all other words. 

    If i type an listbox shows andry and andry a removing all other words. 

    Im using this leshay code to insert items from txt file to my listbox

    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim fn As String = "C:\Users\lesha\Desktop\testfile.txt"
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    		Dim lines() As String = IO.File.ReadAllLines(fn)
    		ListBox1.DataSource = lines
    
    	End Sub
    
    	Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
    		Dim s() As String = ListBox1.SelectedItem.ToString.Split(New String() {" ", "-"}, StringSplitOptions.RemoveEmptyEntries)
    		TextBox1.Text = s(0)
    		TextBox2.Text = s(1)
    	End Sub
    End Class

    Hi

    Here is a better version of the code I posted. This version will keep the ListBox and Combobox in sync and will add an item if not already in the list. User can use the CB in the same way as a TextBox for input. There is also a DELETE button.

    ' Form1 with ListBox1, ComboBox1
    ' and Button1
    Option Strict On
    Option Explicit On
    Imports System.ComponentModel
    Public Class Form1
        Dim fn As String = "C:\Users\lesha\Desktop\testfile.txt"
        Dim bs As New BindingSource
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            bs.DataSource = IO.File.ReadAllLines(fn).ToList
            ListBox1.DataSource = bs

            With ComboBox1
                .DataSource = bs
                .AutoCompleteMode = AutoCompleteMode.SuggestAppend
                .AutoCompleteSource = AutoCompleteSource.ListItems
            End With
        End Sub
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
            ' User changed CB selection so update
            ' the ListBox to match
            Dim s As String = ComboBox1.SelectedItem.ToString
            If ListBox1.Items.Contains(s) Then bs.Position = bs.IndexOf(s)
        End Sub
        Private Sub ComboBox1_Validating(sender As Object, e As CancelEventArgs) Handles ComboBox1.Validating
            ' update datasource if not already
            ' in the list
            ' (with ProperCase comversion)
            Dim s As String = ComboBox1.Text
            If Not bs.Contains(s) Then
                Dim UserInput As String = StrConv(s, VbStrConv.ProperCase)
                bs.Add(UserInput)
                bs.Position = bs.IndexOf(UserInput)
            End If
        End Sub
        Private Sub ComboBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyDown
            ' User used ENTER key to complete
            ' entry of new item
            If e.KeyCode = Keys.Enter Then ValidateChildren()
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            ' delete item
            Dim s As String = ComboBox1.SelectedItem.ToString
            If bs.Contains(s) Then bs.Remove(s)
        End Sub
    End Class


    Regards Les, Livingston, Scotland



    • Edited by leshay Wednesday, October 2, 2019 10:03 AM Updated code
    Wednesday, October 2, 2019 9:09 AM
  • leshay i've setted listbox1.visible = false and im using just combobox to select names.

    1) with your code combobox displays all names-numbers like  listbox how can i list names only behind the separator "-" from listbox? 

    Listbox contains:

    A-1

    B-2

    C-3

    Combobox only:

    A

    B

    C

    Dim s() As String = listbox1.Items.ToString.Split(New String() {"-"}, StringSplitOptions.RemoveEmptyEntries)
    combobox1.Text = s(0)


    2) My txt file have +-14000 lines and the load is a little slow is possible to speed up the readalllines proccess?

    • Edited by extream87 Wednesday, October 2, 2019 10:23 PM
    Wednesday, October 2, 2019 10:06 PM
  • Hi

    I suspect you are not asking the correct question.

    Your data, I believe is of the form

    <string><space><hyphen><space><string>

    Is that correct? ANSWER EXPECTED

    Here is a question for you: A ComboBox can be made to display one aspect of your data (first string) and when User selects an item in the ComboBox, it can be made to return the  (second string). Is that the kind of thing you want? ANSWER EXPECTED

    *

    If so and you don't actually need the Listbox then everything is a lot more strasightforward.

    *

    As for the speed aspect. It is much more likely that the filling of the ListBox is taking longer that the reading of the data.

    *

    I seem to remember asking you if the data is under your control such that you could (quite easily) re arrange it to be a more manageable format (such as <string><comma><string>) BUT YOU DIDN'T ANSWER!

    *

    Here is a short example where both 'parts' of the data are shown in Labels when ComboBox Index is changed. This example uses a file as follows:

    Andry - Smith
    Andry A - Brown
    Best - White
    Camaro - Black
    Caliente - Purple
    ' Form1 with ComboBox1,
    ' Label1 and Label2
    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim fn As String = "C:\Users\lesha\Desktop\testfile.txt"
    	Dim dt As New DataTable("Freddy")
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		Dim lst() As String = IO.File.ReadAllLines(fn)
    		With dt
    			.Columns.Add("Display")
    			.Columns.Add("Value")
    		End With
    		For Each s As String In lst
    			Dim a() As String = s.Split(New String() {" - "}, StringSplitOptions.RemoveEmptyEntries)
    			dt.Rows.Add(a(0), a(1))
    		Next
    		With ComboBox1
    			.DataSource = dt
    			.AutoCompleteMode = AutoCompleteMode.SuggestAppend
    			.AutoCompleteSource = AutoCompleteSource.ListItems
    			.DisplayMember = "Display"
    			.ValueMember = "Value"
    			.SelectedIndex = (dt.Rows.Count - 1) \ 2
    		End With
    	End Sub
    	Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
    		Label1.Text = ComboBox1.Text
    		Label2.Text = ComboBox1.SelectedValue.ToString
    	End Sub
    	Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    		Label1.Text = ComboBox1.Text
    		Label2.Text = ComboBox1.SelectedValue.ToString
    	End Sub
    End Class


    Regards Les, Livingston, Scotland



    • Edited by leshay Thursday, October 3, 2019 12:06 AM
    Wednesday, October 2, 2019 10:39 PM
  • Hello im here again with one last question to complete my project. 

    I need to search in listbox using textbox. 


    Use Listbox findstring or findstringcomplete

    https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.listbox.findstring?view=netframework-4.8


    Success
    Cor

    Thursday, October 3, 2019 5:35 AM
  • leshay is perfect now. But or im doing something wrong or if you have an data value in combobox for example m. leshay if you type les will not return any value because the prefix m.

    Thank you so much 

    Thursday, October 3, 2019 9:34 AM
  • leshay is perfect now. But or im doing something wrong or if you have an data value in combobox for example m. leshay if you type les will not return any value because the prefix m.

    Thank you so much 

    HI

    Please read the BOLD parts again in my last post!


    Regards Les, Livingston, Scotland

    Thursday, October 3, 2019 9:36 AM
  • Your data, I believe is of the form

    <string><space><hyphen><space><string>

    False i've managed data to a simple way:

    <string><hyphen><string>

    Here is a question for you: A ComboBox can be made to display one aspect of your data (first string) and when User selects an item in the ComboBox, it can be made to return the  (second string). Is that the kind of thing you want? ANSWER EXPECTED
    Correct

    I seem to remember asking you if the data is under your control such that you could (quite easily) re arrange it to be a more manageable format (such as <string><comma><string>) BUT YOU DIDN'T ANSWER!
    Yes data is under my control ive already changed format <string><space><hyphen><space><string> to <string><hyphen><string> i can change to <string><comma><string> if this helps.



    • Edited by extream87 Thursday, October 3, 2019 10:08 AM
    Thursday, October 3, 2019 10:07 AM
  • Your data, I believe is of the form

    <string><space><hyphen><space><string>

    False i've managed data to a simple way:

    <string><hyphen><string>

    Here is a question for you: A ComboBox can be made to display one aspect of your data (first string) and when User selects an item in the ComboBox, it can be made to return the  (second string). Is that the kind of thing you want? ANSWER EXPECTED
    Correct

    I seem to remember asking you if the data is under your control such that you could (quite easily) re arrange it to be a more manageable format (such as <string><comma><string>) BUT YOU DIDN'T ANSWER!
    Yes data is under my control ive already changed format <string><space><hyphen><space><string> to <string><hyphen><string> i can change to <string><comma><string> if this helps.



    Hi

    Fine, thanks for the answers.

    The<string><hyphen><string> format is fine. I did see you using a Split on "-" only so I womdered if you had changed things.

    The ComboBox AutoComplete works on the start of the string and so, typing m will not find LesHay. This is where you need to plan things around what you want to do. Usual proctice would be Surname then Forename and/or Initials.

    If you need to find items from a section within the string then that would involve a lot extra coding.


    Regards Les, Livingston, Scotland


    • Edited by leshay Thursday, October 3, 2019 11:12 AM
    Thursday, October 3, 2019 11:12 AM
  • leshay thanks one more time for all your help.

    Data format:

    M. Leshay-1012

    E. Leshay-1689

    A. Extream87-3091

    R. Extream87-501

    ....

    We are showing Name on label1and numbers on label 2.

    Like you said we cant search les or ext directly because the name prefix.

    Its possible do this:

    Separate data in 3 columns

    1) Name prefix = (ex: M.) of M. Leshay

    2) Name = Leshay

    3) Value = next the - separator

    Now we can point the combobox search into column 2.

    In this case i will type les and will appear M. Leshay result. 

    Dont forget using this scenario combobox presentation still showing column 1 & 2 = M. Leshay

    Hope ive explained me good. 

    Obs: This will not result ive also have names like Leshay m. or extream87 .G so we can split like i said

    • Edited by extream87 Thursday, October 3, 2019 12:46 PM
    Thursday, October 3, 2019 12:40 PM
  • Hi

    Try this version and see it it is any better.

    NOTE: there are limitations on how you can use Name-Initial for unique finding.

    This is the data I am using:

    M.Leshay-1012
    E.Leshay-1689
    A.Extream87-3091
    R.Extream87-501

    ' Form1 with ComboBox1,
    ' Label1 and Label2
    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim fn As String = "C:\Users\lesha\Desktop\testfile.txt"
    	Dim dt As New DataTable("Freddy")
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		Dim lst() As String = IO.File.ReadAllLines(fn)
    		With dt
    			.Columns.Add("Display")
    			.Columns.Add("Value")
    		End With
    		For Each s As String In lst
    			Dim a() As String = s.Split(New String() {"-"}, StringSplitOptions.RemoveEmptyEntries)
    			Dim b() As String = a(0).Split(New String() {"."}, StringSplitOptions.RemoveEmptyEntries)
    			dt.Rows.Add(b(1) & " " & b(0), a(1))
    		Next
    		With ComboBox1
    			.DataSource = dt
    			.AutoCompleteMode = AutoCompleteMode.SuggestAppend
    			.AutoCompleteSource = AutoCompleteSource.ListItems
    			.DisplayMember = "Display"
    			.ValueMember = "Value"
    		End With
    	End Sub
    	Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
    		Label1.Text = ComboBox1.Text
    		Label2.Text = ComboBox1.SelectedValue.ToString()
    	End Sub
    	Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    		Label1.Text = ComboBox1.Text
    		Label2.Text = ComboBox1.SelectedValue.ToString()
    	End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Thursday, October 3, 2019 1:43 PM
    Thursday, October 3, 2019 1:41 PM
  • leshay 'System.IndexOutOfRangeException' on

    dt.Rows.Add(b(1) & " " & b(0), a(1))

    I think is because we are trying to use "." as separator an not every lines contains name prefix for example M. or E.

    M.Leshay-1012 E.Leshay-1689 A.Extream87-3091 R.Extream87-501

    Extream87-502

     

    Thursday, October 3, 2019 10:50 PM
  • Hi

    It is about time you think through your posts before posting. About 2 posts up, you laid out the data format being used and are now moving the goalposts by changing it. You should have mentioned that the first part (initial) was optional.

    You are creating unecessary  work for me - trying to firstly understand your question(s) and secondly having to re-write code.

    Make rgis change and try it to see if it helps. Add the 3 line arounf the original (bold line)

    Try
    	dt.Rows.Add(b(1) & " " & b(0), a(1))
    Catch ex As IndexOutOfRangeException
    	dt.Rows.Add(a(0), a(1))
    End Try
    


    Regards Les, Livingston, Scotland

    Thursday, October 3, 2019 11:05 PM
  • Hello, leshay thanks for all your help unfortunately the code works but takes me minutos to get combobox complete. Also this is not my target having the first character reversed to end. Im really thankfull for all your help but i dont want give you more work so im gonna use your previuous code. 
    Friday, October 4, 2019 10:09 AM
  • Hi

    OK, you say that 'but takes me minutos to get combobox complete' then you must be doing something yourself to cause that to happem Maybe you are using an old steam powered computer :)

    Try this code. It has a sub to create a test data fileof 20000 records, and a time toread/fill combobox with 20000 records,

    1. Set the fn Path to a new unused file path name which the code will create the 20000 record test file from.

    2. Run the code.Rhe test file should be created. The code should now be stopped at the Stop line.

    3. Delete the lines before the Stop line and the Stop line too.

    4. Delere the Sub FakeData

    )NOTE: 3 and 4 can be commented out instead of deleted)

    5. Run the code again. This time it should run and open Form1 where the record count and time to fill combobox are displayed.

    *

    On my system, I get consistent times of 7.5 to 8.7 seconds

    ' Form1 with ComboBox1,
    ' Label1 and Label2
    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim fn As String = "C:\Users\lesha\Desktop\testfile.txt"
    	Dim dt As New DataTable("Freddy")
    
    
    	' lines below only test data
    	' remove when not needed
    	' remove sub also
    	Dim sw As New Stopwatch
    	Sub FakeData()
    		Dim r As New Random
    		Dim a As String = "abcdefghijklmnopqrstuvwxyz"
    		Dim lst As New List(Of String)
    		For i As Integer = 0 To 20000
    			Dim init As String = StrConv(a.Substring(r.Next(0, a.Length), 1), vbProperCase) & "."
    			Dim len As Integer = r.Next(4, 9)
    			Dim first As String = StrConv(a.Substring(r.Next(0, a.Length - len), len), vbProperCase) & "-"
    			Dim num As String = r.Next(123, 999999).ToString
    
    			Dim total As String = init & first & num
    			If Not lst.Contains(total) Then lst.Add(total)
    		Next
    		IO.File.WriteAllLines(fn, lst)
    	End Sub
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		' line below only test data
    		' remove when not needed
    		' create 20000 fake test data items
    
    		FakeData()
    		Stop
    
    
    		' start stopwatch
    		' line below only test time
    		' remove when not needed
    
    		sw.Start()
    
    
    		Dim lst() As String = IO.File.ReadAllLines(fn)
    		With dt
    			.Columns.Add("Display")
    			.Columns.Add("Value")
    		End With
    		For Each s As String In lst
    			Dim a() As String = s.Split(New String() {"-"}, StringSplitOptions.RemoveEmptyEntries)
    			Dim b() As String = a(0).Split(New String() {"."}, StringSplitOptions.RemoveEmptyEntries)
    			Try
    				dt.Rows.Add(b(1), b(0) & " " & a(1))
    			Catch ex As IndexOutOfRangeException
    				dt.Rows.Add(a(0), a(1))
    			End Try
    		Next
    		With ComboBox1
    			.DataSource = dt
    			.AutoCompleteMode = AutoCompleteMode.SuggestAppend
    			.AutoCompleteSource = AutoCompleteSource.ListItems
    			.DisplayMember = "Display"
    			.ValueMember = "Value"
    		End With
    
    
    		' lines below only test time
    		' remove when not needed
    		sw.Stop()
    		Label2.Text = (sw.ElapsedMilliseconds / 1000).ToString("0.00 sec")
    		Label1.Text = dt.Rows.Count.ToString & " rec"
    	End Sub
    	Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    		Label1.Text = ComboBox1.Text
    		Label2.Text = ComboBox1.SelectedValue.ToString()
    	End Sub
    End Class
    


    Regards Les, Livingston, Scotland

    Friday, October 4, 2019 12:01 PM
  • leshay i've got 20001rec 4,86sec so seems all is ok i dont know why in my code im getting slow speed i dont have nothing slowly the proccess...
    Anyway im not an pro like you but...

    Why we dont create 2 listbox:
    listbox1 reads all lines with the correct separator ("-") (0) and (1) as you already know

    listbox2 will be = listbox1 (copy)

    We will use listbox1 visible = false as data owner

    listbox2 will be used to search with one textbox and since we always have data stored in listbox1 will be faster to load.
    I've checked this code but im not an expert and i dont know how to setup it correctly or if what im saying its correct.
    Code:

    Public Class Glossary
        Private Sub Glossary_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            List()
            Refreshlist()
        End Sub
    
        Private Sub List()
            word.AddRange(New String() {"apple", "banana", "berry", "cashew", "lemon", "mango", "peanut"})
        End Sub
    
        Private word As New List(Of Object)()
    
        Private Sub Refreshlist()
            lstword.Items.Clear()
            lstword.Items.AddRange(word.Where(Function(word) word.ToString().Contains(txtSearch.Text)).ToArray())
        End Sub
    
        Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
            Refreshlist()
        End Sub
    End Class





    • Edited by extream87 Friday, October 4, 2019 10:31 PM
    • Proposed as answer by leshay Saturday, October 5, 2019 11:40 PM
    Friday, October 4, 2019 10:19 PM
  • Hi

    That seems to be a good approach. It is fast and efficient. It lets the User filter in real time with any portion  of the string.

    I have tested with the following code

    NOTE: a new FakeData Sub needs to be used one time to create the test data - a better set of data for testing with.

    ' Form1 with ListBox1, Label1
    ' and TextBox1
    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim Words As New List(Of String)
    	Dim LstWords As New List(Of String)
    	Dim fn As String = "C:\Users\lesha\Desktop\testfile.txt"
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		Words = IO.File.ReadAllLines(fn).ToList
    		'	FakeData()
    		FindMatches()
    	End Sub
    	Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    		FindMatches()
    	End Sub
    	Sub FindMatches()
    		ListBox1.DataSource = Nothing
    		LstWords.Clear()
    		LstWords.AddRange(Words.Where(Function(word) word.ToString.ToLower.Contains(TextBox1.Text.ToLower)).ToList)
    
    		LstWords = LstWords.OrderBy(Function(x) x.Length).ThenBy(Function(x) x).ToList()
    
    		ListBox1.DataSource = LstWords
    		Label1.Text = LstWords.Count.ToString
    	End Sub
    	Sub FakeData()
    		' delete this Sub if notneeded
    		Dim r As New Random
    		Dim a As String = "abcdefghijklmnopqrstuvwxyz"
    		Dim lst As New List(Of String)
    		For i As Integer = 0 To 20000 - 1
    			Dim init As String = StrConv(a.Substring(r.Next(0, a.Length), 1), vbProperCase) & "."
    
    			Dim first As String = Nothing
    			For j As Integer = 0 To r.Next(0, 9)
    				first &= a.Substring(r.Next(0, a.Length), 1)
    			Next
    
    			Dim num As String = r.Next(12345, 999999999).ToString
    
    			Dim total As String = init & StrConv(first, vbProperCase) & "-" & num
    			If Not lst.Contains(total) Then lst.Add(total)
    		Next
    		IO.File.WriteAllLines(fn, lst)
    	End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Friday, October 4, 2019 11:51 PM
    Friday, October 4, 2019 11:50 PM
  • leshay fantastic job now works great.

    I only need to add separator thing to show text behind "-" on listbox (instead of B. Zsjx-7388383 only show B. Zsjx) and when listbox item selected show text next to "-" into textbox2 (7388383)

    Saturday, October 5, 2019 11:01 AM
  • Hi

    Try this code. NOTE: code and UI are different so new project for this .

    ' Form1 with ListBox1, TextBox1
    ' Label1 and Label2
    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim Words As New List(Of String)
    	Dim LstWords As New List(Of String)
    	Dim WordNum As New List(Of String)
    	Dim fn As String = "C:\Users\lesha\Desktop\testfile.txt"
    	Dim dic As New Dictionary(Of String, String)
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		Words = IO.File.ReadAllLines(fn).ToList
    
    		'	FakeData()
    		FindMatches()
    	End Sub
    	Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    		FindMatches()
    	End Sub
    	Sub FindMatches()
    		ListBox1.DataSource = Nothing
    		LstWords.Clear()
    		LstWords.AddRange((From i In Words Select i.Split("-"c)(0)).ToList.Where(Function(word) word.ToString.ToLower.Contains(TextBox1.Text.ToLower)).ToList)
    
    		WordNum.AddRange((From i In Words Select i.Split("-"c)(1)).ToList.Where(Function(word) word.ToString.ToLower.Contains(TextBox1.Text.ToLower)).ToList)
    
    		'	LstWords = LstWords.OrderBy(Function(x) x.Length).ThenBy(Function(x) x).ToList()
    
    		ListBox1.DataSource = LstWords
    	End Sub
    	Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
    		If ListBox1.SelectedIndex < 0 Then Exit Sub
    		Label1.Text = ListBox1.SelectedItem.ToString
    		Label2.Text = WordNum(ListBox1.SelectedIndex)
    	End Sub
    
    	Sub FakeData()
    		' delete this Sub if not needed
    		Dim r As New Random
    		Dim a As String = "abcdefghijklmnopqrstuvwxyz"
    		Dim lst As New List(Of String)
    		For i As Integer = 0 To 20000 - 1
    			Dim init As String = StrConv(a.Substring(r.Next(0, a.Length), 1), vbProperCase) & "."
    
    			Dim first As String = Nothing
    			For j As Integer = 0 To r.Next(0, 9)
    				first &= a.Substring(r.Next(0, a.Length), 1)
    			Next
    
    			Dim num As String = r.Next(12345, 999999999).ToString
    
    			Dim total As String = init & StrConv(first, vbProperCase) & "-" & num
    			If Not lst.Contains(total) Then lst.Add(total)
    		Next
    		IO.File.WriteAllLines(fn, lst)
    	End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Saturday, October 5, 2019 5:53 PM
    • Marked as answer by extream87 Saturday, October 5, 2019 11:28 PM
    • Unmarked as answer by extream87 Saturday, October 5, 2019 11:42 PM
    Saturday, October 5, 2019 2:03 PM
  • leshay everything is working except the value next to "-" separator.
    Imagine 10000 words on listbox1 if you type "shay" will appear leshay in 1st position of listbox when you select it the result the name is ok but the value will be wrong cause is getting the value of 1st index of all words of listbox do you understand what i mean?
    • Edited by extream87 Saturday, October 5, 2019 11:45 PM
    Saturday, October 5, 2019 11:30 PM
  • Hi

    What are you using as the data file - is it one generated with the FakeData Sub in the code I posted - or something else?


    Regards Les, Livingston, Scotland

    Saturday, October 5, 2019 11:55 PM
  • My txt data. With names-values

    Im gonna create an new txt with some lines to check if the problem is from my txt. 

    If i use your other code works great but in listbox i will have name-value.

    Sunday, October 6, 2019 12:19 AM
  • Hi

    No, the issue is not on your side - it is (was) here. Maybe fixed now. Give this version a try out and see.

    EDIT: needed to add a Split for Label2.Text

    EDIT: tidied up and got rid of unecessary code.

    ' Form1 with ListBox1, TextBox1
    ' Label1 and Label2
    Option Strict On
    Option Explicit On
    Public Class Form1
        Dim Words As New List(Of String)
        Dim LstWords As New List(Of String)
        Dim fn As String = "C:\Users\lesha\Desktop\testfile.txt"
        Dim Matches As New List(Of String)
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Words = IO.File.ReadAllLines(fn).ToList
            '    FakeData()
            FindMatches()
        End Sub
        Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
            FindMatches()
        End Sub
        Sub FindMatches()
            ListBox1.DataSource = Nothing
            LstWords.Clear()
            Matches.Clear()

            Matches.AddRange((From i In Words.Where(Function(word) word.ToString.ToLower.Contains(TextBox1.Text.ToLower)).ToList))

            LstWords.AddRange((From i In Matches Select i.Split("-"c)(0)).ToList)

            ListBox1.DataSource = LstWords
        End Sub
        Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
            If ListBox1.SelectedIndex < 0 Then Exit Sub
            Label1.Text = Split(Matches(ListBox1.SelectedIndex), "-")(0)
            Label2.Text = Split(Matches(ListBox1.SelectedIndex), "-")(1)
        End Sub

        Sub FakeData()
            ' delete this Sub if not needed
            Dim r As New Random
            Dim a As String = "abcdefghijklmnopqrstuvwxyz"
            Dim lst As New List(Of String)
            For i As Integer = 0 To 20000 - 1
                Dim init As String = StrConv(a.Substring(r.Next(0, a.Length), 1), vbProperCase) & "."

                Dim first As String = Nothing
                For j As Integer = 0 To r.Next(0, 9)
                    first &= a.Substring(r.Next(0, a.Length), 1)
                Next

                Dim num As String = r.Next(12345, 999999999).ToString

                Dim total As String = init & StrConv(first, vbProperCase) & "-" & num
                If Not lst.Contains(total) Then lst.Add(total)
            Next
            IO.File.WriteAllLines(fn, lst)
        End Sub
    End Class


    Regards Les, Livingston, Scotland




    • Edited by leshay Sunday, October 6, 2019 1:29 AM
    • Marked as answer by extream87 Sunday, October 6, 2019 11:31 AM
    Sunday, October 6, 2019 12:42 AM
  • Hi

    I have editted the code in last post to add a Split for Label2.Text, tidied up and got rid of unecessary code


    Regards Les, Livingston, Scotland


    • Edited by leshay Sunday, October 6, 2019 1:29 AM
    Sunday, October 6, 2019 12:49 AM
  • Thanks master leshay after all this work this works amazing.

    Thanks for all your help and comprehension.

    Sunday, October 6, 2019 11:32 AM