locked
Save and Load listbox items to file with dialog

    Question

  • How do I save and load listbox items to a text file using the save and open dialogs??
     Thanks
    Tuesday, November 27, 2007 1:48 AM

Answers

  • Save

    Code Block

    Dim SFD As New SaveFileDialog

    'show only text files

    SFD.Filter = "Text Files | *.txt"

    If SFD.ShowDialog = Windows.Forms.DialogResult.OK Then

    Dim StrmWrtr As IO.StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(SFD.FileName, False)

    For i As Integer = 0 To ListBox1.Items.Count - 1

    StrmWrtr.WriteLine(ListBox1.Items(i))

    Next

    StrmWrtr.Close()

    End If

     

     

    Read

    Code Block

    Dim OFD As New OpenFileDialog

    'show only text files

    OFD.Filter = "Text Files | *.txt"

    If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then

    Dim StrmRdr As IO.StreamReader = My.Computer.FileSystem.OpenTextFileReader(OFD.FileName)

    Do Until StrmRdr.EndOfStream = True

    ListBox1.Items.Add(StrmRdr.ReadLine)

    Loop

    End If

    End Sub

     

     

    Tuesday, November 27, 2007 4:56 AM