I have a problem that I am really having a hard time with. I have a textbox in my form that I am trying to add into autocomplete so when I get back into that form I can just type the first letter and have it populate.

I have a textbox that I type a manufactures name into (ex. Konica Minolta) and at the button I have this code to save the textbox1.text into the autocomplete properties

Dim MySource As New AutoCompleteStringCollection()
        Dim add As String
        add = TextBox1.Text
        MySource.Add(add)
        TextBox1.AutoCompleteCustomSource = MySource
        TextBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend
        TextBox1.AutoCompleteSource = AutoCompleteSource.CustomSource

when i close the form and go back in, the textbox does not autocomplete. I am not sure that the coding is right and also I would like to add in if that name is already in the list to not add it over and over. I am trying to stay away from manualy adding in the names, I would really like to have it autopopulate.

Recommended Answers

All 9 Replies

Let's see the form close code where you save your collection. You'll need another passage (code) to load from there when your form shows.

Here is what I have under my button, which everything happens under this button

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim txt As String
        txt = TextBox1.Text
        My.Computer.FileSystem.CreateDirectory("C:\Copiers\" & txt & "\")

        Dim sFilename As String
        sFilename = ("C:\Copiers\" & txt & "\") & TextBox2.Text & ".txt"

        Dim i As Integer
        Dim aryText(7) As String

        aryText(0) = "--"
        aryText(1) = TextBox3.Text
        aryText(2) = TextBox4.Text
        aryText(3) = TextBox6.Text
        aryText(4) = TextBox7.Text
        aryText(5) = TextBox5.Text
        aryText(6) = TextBox8.Text
        Dim objWriter As New System.IO.StreamWriter(sFilename, True)

        For i = 0 To 6
            objWriter.WriteLine(aryText(i))
        Next

        Dim item As New ListViewItem
        item.Text = TextBox3.Text
        item.SubItems.Add(TextBox4.Text)
        item.SubItems.Add(TextBox6.Text)
        item.SubItems.Add(TextBox7.Text)
        item.SubItems.Add(TextBox5.Text)
        item.SubItems.Add(TextBox8.Text)
        ListView1.Items.Add(item)

        objWriter.Close()


        TextBox3.Clear()
        TextBox4.Clear()
        TextBox5.Clear()
        TextBox6.Clear()
        TextBox7.Clear()
        TextBox8.Clear()



        If CheckBox1.Checked Then
            Form1.ComboBox1.Items.Clear()
            Dim q = From dir In IO.Directory.GetDirectories("C:\Copiers\")
                    Select IO.Path.GetFileName(dir)
            Form1.ComboBox1.Items.AddRange(q.ToArray)

            Dim MySource As New AutoCompleteStringCollection()
            Dim add As String
            add = TextBox1.Text
            MySource.Add(add)
            TextBox1.AutoCompleteCustomSource = MySource
            TextBox1.AutoCompleteMode = AutoCompleteMode.Suggest
            TextBox1.AutoCompleteSource = AutoCompleteSource.CustomSource


            Me.Close()
            Form1.Show()
        End If
    End Sub

This helped me when the program is open and running but when I close out of the program and get back into it nothing populates.
In this part of the code I do not want to manually add in like they do here

If My.Settings.MyCities.Count < 3 Then
            Cities.AddRange((New String() {"Aberdeen", "Bangor", "Birmingham", "Bolton", "Charlton", "Doncaster", "Dorking", "Dundee", "Edinburgh", "Glasgow", "London", "Manchester", "Reading"}))
            My.Settings.MyCities = Cities
        Else

Here is what I have now

Public Class Form2
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim txt As String
        txt = TextBox1.Text
        My.Computer.FileSystem.CreateDirectory("C:\Copiers\" & txt & "\")

        Dim sFilename As String
        sFilename = ("C:\Copiers\" & txt & "\") & TextBox2.Text & ".txt"

        Dim i As Integer
        Dim aryText(7) As String

        aryText(0) = "--"
        aryText(1) = TextBox3.Text
        aryText(2) = TextBox4.Text
        aryText(3) = TextBox6.Text
        aryText(4) = TextBox7.Text
        aryText(5) = TextBox5.Text
        aryText(6) = TextBox8.Text
        Dim objWriter As New System.IO.StreamWriter(sFilename, True)

        For i = 0 To 6
            objWriter.WriteLine(aryText(i))
        Next

        Dim item As New ListViewItem
        item.Text = TextBox3.Text
        item.SubItems.Add(TextBox4.Text)
        item.SubItems.Add(TextBox6.Text)
        item.SubItems.Add(TextBox7.Text)
        item.SubItems.Add(TextBox5.Text)
        item.SubItems.Add(TextBox8.Text)
        ListView1.Items.Add(item)

        objWriter.Close()


        TextBox3.Clear()
        TextBox4.Clear()
        TextBox5.Clear()
        TextBox6.Clear()
        TextBox7.Clear()
        TextBox8.Clear()



        If CheckBox1.Checked Then
            Form1.ComboBox1.Items.Clear()
            Dim q = From dir In IO.Directory.GetDirectories("C:\Copiers\")
                    Select IO.Path.GetFileName(dir)
            Form1.ComboBox1.Items.AddRange(q.ToArray)

            Dim MySource As New AutoCompleteStringCollection()

            With TextBox1
                .AutoCompleteCustomSource = MySource
                .AutoCompleteMode = AutoCompleteMode.Append
                .AutoCompleteSource = AutoCompleteSource.CustomSource
            End With

            MySource.Add(TextBox1.Text)
            My.Settings.MySource = MySource

            Me.Close()
            Form1.Show()
        End If
    End Sub

    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim MySource As New AutoCompleteStringCollection()


        MySource = My.Settings.MySource


            With TextBox1
            .AutoCompleteCustomSource = MySource
            .AutoCompleteSource = AutoCompleteSource.CustomSource
            .AutoCompleteMode = AutoCompleteMode.Append
        End With
    End Sub
End Class

This does save what I type in the textbox when I am in the program but when I get back into the program there is nothing populating. I even added the code to form1's startup to see if it would pull it in but nothing. When I look in the collection for the textbox nothing is in there either.

Still can't find code to save your customizations when the form closes. To quote Nintendo.

Everything not saved will be lost

Does any one know what that code would be that I need for when it closes?

I guess I will close out this thread and try to find help else where. Thanks

Sorry to read your last reply. I don't mind reading your code, and commenting on it but rarely do folk write your code for you.

Do not take offense at that or this website to follow. There appears to be a lot of new and old programmers that think it's OK to ask folk to write their code for them. And some even get irate when you don't. Longer read at http://mattgemmell.com/what-have-you-tried/

There is good code here how this works. I think you need to rethink your saving of the file and how you load it.
https://msdn.microsoft.com/en-us/library/system.windows.forms.textbox.autocompletemode(v=vs.110).aspx

Here is a complete snippets how it works:

Imports System.IO

Public Class Form1
    Dim path As String = "C:\Test\Copiers"
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' Create the list to use as the custom source and syve to file
        Dim mytext As String = File.ReadAllText(path & "\ test.txt")
        'create the source mytxt is the saved file we split to generate an array of string
        'str() is the array we useas the source
        Dim str() As String = mytext.Split(","c)
        Dim MySource As New AutoCompleteStringCollection()
        MySource.AddRange(str)
        ' Create and initialize the text box.
        With TextBox1
            .AutoCompleteCustomSource = MySource
            .AutoCompleteMode = AutoCompleteMode.SuggestAppend
            .AutoCompleteSource = AutoCompleteSource.CustomSource
        End With
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Application.Exit()
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        Dim mytxt As String = ""
        Dim myfile As System.IO.StreamWriter
        If Directory.Exists(Path) Then
            Debug.Print("That path exists already.")
        Else
            Dim di As DirectoryInfo = Directory.CreateDirectory(Path)
            Debug.Print("The directory was created successfully")
        End If
        mytxt = TextBox2.Text & "," & TextBox3.Text & "," & TextBox4.Text
        Debug.Print(mytxt)
        myfile = My.Computer.FileSystem.OpenTextFileWriter(Path & "\ test.txt", True)
        myfile.WriteLine(mytxt)
        myfile.Close()
    End Sub
End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.