PDA

View Full Version : ذخیره لیست باکس



elham99
دوشنبه 17 مهر 1391, 17:20 عصر
سلام دوستان خوبید ؟من میخواستم داخل listbox رو به صورت text در یک مسیر مشخص ذخیره کنم میتونید کمکم کنید؟

xxnagin
دوشنبه 17 مهر 1391, 20:02 عصر
سلام
آموزش کامل کار با فایل ها و فولدرها در VB.NET (http://barnamenevis.org/showthread.php?342327-آموزش-کامل-کار-با-فایل-ها-و-فولدرها-در-VB.NET&highlight=file)
اینم یک نمونه


Public Class Form1
Dim w As IO.StreamWriter
Dim r As IO.StreamReader
Private Sub loadfile()
Try
r = New IO.StreamReader("D:\MyTextFile.txt")
While (r.Peek() > -1)
ListBox1.Items.Add(r.ReadLine)
End While
r.Close()
Catch ex As Exception
End Try
End Sub
Private Sub savefile()
Try
Dim i As Integer
w = New IO.StreamWriter("D:\MyTextFile.txt")
For i = 0 To ListBox1.Items.Count - 1
w.WriteLine(ListBox1.Items.Item(i))
Next
w.Close()
Catch ex As Exception
End Try
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If IO.File.Exists("D:\MyTextFile.txt") = True Then
loadfile()
Else
IO.File.CreateText("D:\MyTextFile.txt")
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
loadfile()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
savefile()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If TextBox1.Text <> "" Then
ListBox1.Items.Add(TextBox1.Text)
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
ListBox1.Items.Clear()
savefile()
End Sub
End Class