Napadlo mne, že tohle budete chtít udělat a proto jsem tak ten kód navrhl. Viz ta poznámka o _lineEditors kolekci.
Public Class Form1
Private _fileName As String = "C:/test.lsv"
Private _lineEditors As New List(Of TextBox)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim add As Integer = 0
Using fs As New IO.FileStream(_fileName, IO.FileMode.OpenOrCreate)
Using sr As New IO.StreamReader(fs, System.Text.Encoding.UTF8)
While Not sr.EndOfStream
Dim tb As New TextBox
tb.Text = sr.ReadLine()
AddHandler tb.TextChanged, AddressOf TextBox_TextChanged
tb.Location = New Point(12, 45 + add)
add += 33
Me.Controls.Add(tb)
_lineEditors.Add(tb)
End While
End Using
End Using
End Sub
Private Sub TextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
DirectCast(sender, TextBox).BackColor = SystemColors.Info
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Using fs As New IO.FileStream(_fileName, IO.FileMode.Create)
Using sw As New IO.StreamWriter(fs)
For Each tb As TextBox In _lineEditors
sw.WriteLine(tb.Text)
Next
End Using
End Using
MsgBox("All values has been saved.", MsgBoxStyle.OkOnly, "Save")
End Sub
' append a new line editor
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim tb As New TextBox
tb.Text = String.Empty
AddHandler tb.TextChanged, AddressOf TextBox_TextChanged
tb.Location = New Point(12, _lineEditors.Last.Location.Y + 33)
Me.Controls.Add(tb)
_lineEditors.Add(tb)
End Sub
End Class
Button dva nyní obsluhuje ukládání, prohlédněte si ten kód, uvidíte, že to není žádná věda =) Taky jsem přidal Button3, který připojí další TextBox na konec, což Vám umožní vytvořit další řádek, hodnotu.
|