'窗体上放5个textBox控件,控件名依次为 TextBox1, TextBox2, TextBox3, TextBox4, TextBox5
'Button1按钮为读文件,Button2按钮为写文件
'程序第一运行时先在5个文本框中输入相应内容,点击“写文件”,以创建文件
Public Class Form1
Private textBoxArray() As TextBox
Private fileName As String
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.textBoxArray = New TextBox() {Me.TextBox1, Me.TextBox2, Me.TextBox3, Me.TextBox4, Me.TextBox5}
fileName = "csdx.txt"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If System.IO.File.Exists(fileName) <> True Then
MsgBox("文件不存在!,请先在文本框中输入数据,点击写文件。")
Exit Sub
End If
Dim line As String
Dim sr As IO.StreamReader = New IO.StreamReader(fileName, System.Text.Encoding.Default)
Dim I As Integer
For I = 1 To 5 Step 1
If sr.EndOfStream Then Exit For
line = sr.ReadLine
Me.textBoxArray(I - 1).Text = line
Next I
sr.Close()
sr = Nothing
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim sw As IO.StreamWriter = New IO.StreamWriter(fileName, False)
Dim I As Integer
For I = 1 To 5
sw.WriteLine(Me.textBoxArray(I - 1).Text)
sw.Flush()
Next
sw.Close()
sw = Nothing
End Sub
End Class
温馨提示:答案为网友推荐,仅供参考