Avatar of jorgemgonzalez
jorgemgonzalezFlag for United States of America asked on

Using a text file as autocomplete source

Hello experts!

I have a long list of foreign words (mostly names) stored in a text file generated via spell checking, which means some are added from time to time, those words are added one per line, many of those words are a royal pain to type, therefore it would be sweet to be able to get some auto-completion here. (I have to translate a lot of books from English to Spanish, so you can imagine)

How can I use that long word list as the source for autocompletion in a textbox?

Please help.
Visual Basic.NET

Avatar of undefined
Last Comment
kaufmed

8/22/2022 - Mon
kaufmed

Here's one method:
Dim autocompleteList As New List(Of String)

Using reader As New System.IO.StreamReader("C:\path\to\file.txt")
    While Not reader.EndOfStream
        autocompleteList.Add(reader.ReadLine())
    End While
End Using


Me.textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource
Me.textBox1.AutoCompleteCustomSource = autocompleteList

Open in new window

SOLUTION
TheLastFantasy

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
jorgemgonzalez

Ok. I tried both ways, but I might be missing something, my text box doesn't show any suggestions. Should I do anything else for this to work? What triggers the suggestions?
kaufmed

Did you set the AutoCompleteMode?
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER
jorgemgonzalez

Yes i did, your corrected code I added to the Form1_Load, whether on load or on text changed I see no results at all.
TheLastFantasy

You have to add something to the form load event:


Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
     
  MyBase.KeyPreview = True
       
 End Sub

That should do it!


TLF
kaufmed

@TheLastFantasy

I didn't have to add that for my example.

Here is my complete sample:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim autocompleteList As New System.Windows.Forms.AutoCompleteStringCollection

        Using reader As New System.IO.StreamReader("file.txt")
            While Not reader.EndOfStream
                autocompleteList.Add(reader.ReadLine())
            End While
        End Using


        Me.TextBox1.AutoCompleteSource = AutoCompleteSource.CustomSource
        Me.TextBox1.AutoCompleteMode = AutoCompleteMode.Suggest
        Me.textBox1.AutoCompleteCustomSource = autocompleteList
    End Sub
End Class

Open in new window

untitled.PNG
file.txt
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
jorgemgonzalez

Still doesnt work.
The text box in my case is a multtiline text box, would that somehow be the problem with your approaches?
kaufmed

I don't believe AutoComplete works with Multiline TextBoxes.
ASKER
jorgemgonzalez

I guess it wouldn't for I added a small custom source in design time and id would not work either, therefore, I'll have to add a little dialog with a single-line textbox and with the click of a button pass the resulting text to the selected text on my main trext box, heck! that,s faster than typing something like "Zatsvilikhovskina" (or worse!) without making mistakes :)

Your help is very appreciated, splitting the points 50/50.

Thanks a million!
Your help has saved me hundreds of hours of internet surfing.
fblack61
kaufmed

NP. Glad to help  :)