VB.Net读取或保存listbox的记录到文件中的代码如下:
Public Class Form1 Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New() MyBase.New()
'This call is required by the Windows Form Designer. InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub
'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents ListBox1 As System.Windows.Forms.ListBox Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents ListBox2 As System.Windows.Forms.ListBox Friend WithEvents Button2 As System.Windows.Forms.Button <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.ListBox1 = New System.Windows.Forms.ListBox() Me.Button1 = New System.Windows.Forms.Button() Me.ListBox2 = New System.Windows.Forms.ListBox() Me.Button2 = New System.Windows.Forms.Button() Me.SuspendLayout() ' 'ListBox1 ' Me.ListBox1.Location = New System.Drawing.Point(72, 32) Me.ListBox1.Name = "ListBox1" Me.ListBox1.SelectionMode = System.Windows.Forms.SelectionMode.MultiSimple Me.ListBox1.Size = New System.Drawing.Size(184, 69) Me.ListBox1.TabIndex = 2 ' 'Button1 '更多vb.net源码和实例,请关注{乐博网 www.lob.cn} ' Me.Button1.Location = New System.Drawing.Point(128, 112) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(72, 24) Me.Button1.TabIndex = 3 Me.Button1.Text = "Write" ' 'ListBox2 ' Me.ListBox2.Location = New System.Drawing.Point(72, 160) Me.ListBox2.Name = "ListBox2" Me.ListBox2.Size = New System.Drawing.Size(184, 69) Me.ListBox2.TabIndex = 4 ' 'Button2 ' Me.Button2.Location = New System.Drawing.Point(136, 248) Me.Button2.Name = "Button2" Me.Button2.Size = New System.Drawing.Size(72, 24) Me.Button2.TabIndex = 5 Me.Button2.Text = "Load" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(320, 293) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button2, Me.ListBox2, Me.Button1, Me.ListBox1}) Me.Name = "Form1" Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen Me.Text = "Form1" Me.ResumeLayout(False)
End Sub
#End Region Dim w As IO.StreamWriter Dim r As IO.StreamReader
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim itms() As String = {"item1", "item2", "item3"} ListBox1.Items.AddRange(itms) End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim i As Integer w = New IO.StreamWriter("c:\test.txt") For i = 0 To ListBox1.Items.Count - 1 w.WriteLine(ListBox1.Items.Item(i)) Next w.Close() End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click r = New IO.StreamReader("c:\test.txt") While (r.Peek() > -1) ListBox2.Items.Add(r.ReadLine) End While r.Close() End Sub End Class
|