I have 7 textbox entries and I would like to add them to a listbox and when I close my application it would save them, I have the code for the 7 texboxes and everything like that has been done, but I just dont know how to add multiple textboxs to a listbox then save the list box so when I close the application and reopen it, itll still be there, could someone help me out. I know I need a button to add the list to the listbox, but how do I save them, so theyll still be there?

Hi,

Here's an example how to save the listboxitems to a textfile:

Private Shared Sub WriteToFile()
	Dim SW As StreamWriter
	SW = File.Create("c:\MyTextFile.txt")
	For Each item As ListItem In ListBox.items
	SW.WriteLine(item.text)
	Next
	SW.Close()
End Sub

Adding 7 nos. textbox's text into a listbox

lstAlbums.Items.Add(Form1.textbox1.text.ToString)
lstAlbums.Items.Add(Form1.textbox2.text.ToString)
lstAlbums.Items.Add(Form1.textbox3.text.ToString)
lstAlbums.Items.Add(Form1.textbox4.text.ToString)
lstAlbums.Items.Add(Form1.textbox5.text.ToString)
lstAlbums.Items.Add(Form1.textbox6.text.ToString)
lstAlbums.Items.Add(Form1.textbox7.text.ToString)

For saving a listbox item into a text file

'for saveing listitem into text file
Dim Objfile As New System.IO.StreamWriter("C:\TestText.txt")
Dim intCounter As Long 
For intCounter = 0 To lstAlbums.Items.Count - 1
    objFile.WriteLine(lstAlbums.Items(intCounter).ToString)
Next intCounter

Objfile.Close()
Objfile.Dispose()

Retrieving textfile(i.e list items saved in the text file) into listbox on form load event

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Reading and adding into a listbox
Try
   Dim objFile As New System.IO.StreamReader("c:\TestText.txt")
   Dim strAlbumName As String
   strAlbumName = objFile.ReadLine()
   Do Until strAlbumName Is Nothing
        LstAlbums.Items.Add(strAlbumName)
        strAlbumName = objFile.ReadLine()
   Loop
   objFile.Close()
   objFile.Dispose()
   Catch ex As Exception
      MsgBox("The file could not be found.")
   End Try
End Sub

in my codes Listbox name is lstAlbums

Instead of saving the contents in a text file, when you close the application, save the contents in an xml file. And when the application starts again reload the xml data to the listbox again. The xml always allows modification of data for every time.
Refer to the following code

Private Sub AddtoXML()
	Try
		Dim dt As New DataTable("MyData")

		Dim dt1 As New DataTable("MyData")
		dt1 = GetDetails()
		If drplist_Servername2.Text <> "" AndAlso drplist_Database2.Text <> "" Then
			If dt1 Is Nothing Then
				Dim ColumnValues As [String]() = New [String]() {"ServerName", "DataBase", "Authentication_Type", "User_Id", "Password", "Backup_Enabled"}
				For Each col As String In ColumnValues
					dt.Columns.Add(col.ToString())
				Next
				Dim vals As [String]() = New [String]() {drplist_Servername2.Text.ToString(), drplist_Database2.Text.ToString(), If((radio_tab1_Sql.Checked), "SQL Server Authentication", "Windows Authentication"), txtusr.Text.ToString(), txtpwd.Text.ToString(), chkBack.Checked.ToString()}
				dt.Rows.Add(vals)
				dt.WriteXml(Application.StartupPath + "\DataBaseValues.xml")
			Else
				Dim vals As [String]() = New [String]() {drplist_Servername2.Text.ToString(), drplist_Database2.Text.ToString(), If((radio_tab1_Sql.Checked), "SQL Server Authentication", "Windows Authentication"), txtusr.Text.ToString(), txtpwd.Text.ToString(), chkBack.Checked.ToString()}
				dt1.Rows.Add(vals)
				dt1.WriteXml(Application.StartupPath + "\DataBaseValues.xml")
			End If
			'else
			'{
			'    dt1.WriteXml(@Application.StartupPath + "\\DataBaseValues.xml");
			'}
		End If
	Catch ex As Exception
		MessageBox.Show(ex.Message.ToString())
	End Try
End Sub

sorry about that, I figured it out, but forgot to write back to the form, thanks for the help and ill post the code that I used, so people can see different variations of it
this is to save all the listbox to a text file

Private Sub Button1_Click_2(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click
        Dim sfd As New SaveFileDialog
        sfd.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        sfd.Filter = "Text files (*.txt)|*.txt"

        Dim result As DialogResult = sfd.ShowDialog
        If result = Windows.Forms.DialogResult.OK AndAlso sfd.FileName <> String.Empty Then

            If ListBox1.Items.Count > 0 Then
                Using sw As New System.IO.StreamWriter(sfd.FileName)
                    For index As Integer = 0 To ListBox1.Items.Count - 1
                        sw.WriteLine(ListBox1.Items(index).ToString)
                    Next
                    sw.Close()
                End Using
            End If
        End If
    End Sub

and this is to load the textfile

Private Sub btnload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnload.Click
        ListBox1.Items.Clear()
        Dim ofd As New OpenFileDialog
        ofd.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        ofd.Filter = "Text files (*.txt)|*.txt"
        Dim result As DialogResult = ofd.ShowDialog
        Dim fileContent As String = Nothing
        Dim fileLines() As String

        If result = Windows.Forms.DialogResult.OK AndAlso ofd.FileName <> String.Empty Then
            Using sr As New System.IO.StreamReader(ofd.FileName)
                fileContent = sr.ReadToEnd
                sr.Close()
            End Using
        End If
        fileLines = fileContent.Split(Convert.ToChar(vbLf))

        For index As Integer = 0 To fileLines.GetUpperBound(0)
            ListBox1.Items.Add(fileLines(index))
        Next
    End Sub

It's good to share your codes too. Happy coding.....

Thank you so much. This helped me with my web browser. BTW this also helps with browser history too!!!!

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.