ComboBox Saving input in dropdown

  • Thread starter Thread starter trevor.niemack
  • Start date Start date
T

trevor.niemack

Hello I want to have a combobox that saves all the input, so if I put
the word test in the combobox and submit a query I want to have test
saved so that the next time I go to that combobox I can click on the
letter T and all the inputs with the letter T comes through.
 
Writing the search word(s) to the text file.

Sub WriteWords()
Dim sw As StreamWriter = New StreamWriter("list.txt", True,
System.Text.Encoding.UTF8)
sw.WriteLine(Me.cmbSearch.Text)
sw.Close()
End Sub

Loading the combobox entries from the text file.
Sub LoadWords()
Dim sr As StreamReader = New StreamReader("list.txt")
Dim line As String
Do Until (line Is Nothing)
line = sr.ReadLine()
Me.cmbSearch.Items.Add(line)
Loop
sr.Close()
End Sub

Usage:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
LoadWords()
End Sub

Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSearch.Click
'==>> Searching code here
WriteWords()
Me.cmbSearch.Items.Clear()
LoadWords()
End Sub
 
Thank you very much for the help but I have done something with a bit
more complexity and that might be my problem.
I have actually tried to on submitting the form taken any items in the
listbox.items colloection and tried to write that to an xml and then at
form load I load the xml back into the listbox.items collection however
I have not gotten it to work as yet. I would like to try and get it
working like this but I will try your previous suggestion as well
Below is the code I am currently using but it obviously does not work,
I hope you can understand what I am trying to do here.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim xmlDoc As New XmlDocument
Dim frompathelem As XmlElement =
xmlDoc.CreateElement("FromPath")
Dim topathelem As XmlElement = xmlDoc.CreateElement("ToPath")
xmlDoc.Load(Application.StartupPath & "\PathEntries.xml")

Try
Dim node As XmlNode
node = xmlDoc.DocumentElement.AppendChild(frompathelem)
Dim txtfrompathxml As XmlText
node = xmlDoc.SelectSingleNode("//FromPath")
txtfrompathxml = xmlDoc.DocumentElement.AppendChild(node)
For Each myobj As Object In TxtFromPath.Items
Dim frompathcollection As XmlText =
xmlDoc.CreateTextNode(myobj)
frompathcollection =
xmlDoc.DocumentElement.AppendChild(node)
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try

Try
Dim node As XmlNode
node = xmlDoc.DocumentElement.AppendChild(topathelem)
Dim txttopathxml As XmlText
node = xmlDoc.SelectSingleNode("//ToPath")
txttopathxml = xmlDoc.DocumentElement.AppendChild(node)
For Each myobj As Object In TxtToPath.Items
Dim topathcollection As XmlText =
xmlDoc.CreateTextNode(myobj)
topathcollection =
xmlDoc.DocumentElement.AppendChild(node)
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try

xmlDoc.Save(Application.StartupPath & "\PathEntries.xml")
End Sub


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

If IO.File.Exists(Application.StartupPath & "\PathEntries.xml")
= True Then
Dim xmlsaveddoc As New XmlDocument
Dim node As XmlNode
xmlsaveddoc.Load(Application.StartupPath &
"\PathEntries.xml")
Try
For Each node In xmlsaveddoc.SelectNodes("//FromPath")
txtFromPath.Items.Add(node.InnerText)
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
Try
For Each node In xmlsaveddoc.SelectNodes("//ToPath")
txtToPath.Items.Add(node.InnerText)
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
ElseIf IO.File.Exists(Application.StartupPath &
"\PathEntries.xml") = False Then
Dim xmldoc As New XmlDocument
xmldoc.LoadXml("<Root></Root>")
xmldoc.Save(Application.StartupPath & "\PathEntries.xml")
End If
End Sub
 
Back
Top