Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

List Box contents save to text file HELP!!!!!!!!!!!!!!!! 1

Status
Not open for further replies.

ridifvx

Programmer
Apr 10, 2003
34
ID
I tried to save a content of a list box to a text file in VB 6 i write the code like this

Open "c:\temp.txt" For Output As 1
For i=0 To Listbox1.ListCount-1
Print #1, Listbox1.List(i)
Next
Close #1

Now i want to use savefiledialog in VBNET so when i click save button, the app show save file dialog and save the content of a list book in a desired name and path
and how to open that text file so i can load it later using openfile dialog..

any sugestion please :(

(i know this is silly Question but i don't find any good clue in MSDN online help )
 
Here is the code for saving & opening the text file using SaveFileDialogBox and OpenFileDialog box

Regards
Nouman

Private Sub Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save.Click
SaveFileDialog1.Filter = "Text files (*.txt)|*.txt"
SaveFileDialog1.AddExtension = True
SaveFileDialog1.ShowDialog()
If SaveFileDialog1.FileName <> &quot;&quot; Then
Dim sw As StreamWriter
sw = File.CreateText(SaveFileDialog1.FileName)
Dim i As Integer
For i = 0 To ListBox1.Items.Count - 1
sw.WriteLine(ListBox1.Items(i))
Next
sw.Close()
End If


End Sub
Private Sub open_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles open.Click
OpenFileDialog1.Filter = &quot;Text files (*.txt)|*.txt&quot;
OpenFileDialog1.AddExtension = True
OpenFileDialog1.ShowDialog()
If OpenFileDialog1.FileName <> &quot;&quot; Then
Dim sr As StreamReader
sr = File.OpenText(OpenFileDialog1.FileName)
Dim x As String
While sr.Peek <> -1
x = sr.ReadLine
ListBox2.Items.Add(x)
End While
sr.Close()
End If
End Sub

Nouman Zaheer
Software Engineer
MSR
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top