Hello all and thanks for reading.
I'm trying to make a program that will simply remember what I type in multiple text boxes. I started approching the problem with a database but decided I should probably use file I/O with a text doc to save my data. I've found a few thing on loading my txt file, but I don't know where to go from there. I'm thinking I'll go comma delimated. I basically have about 150 text boxes. The number of text boxes will not change so I don't want a database. If I load the file and save it to string, how do I make, say the fourth item, show up in the fouth txt box. Also, how do I make it save the data that I type in the fourth textbox save to the 4th spot in my comma delimated txt file Below is a sample of my code that I have in the event.textchanged for the first box. Thank you for taking the time on this.
Using MyReader As New _Microsoft.VisualBasic.FileIO.TextFieldParser(
"bay.txt")MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters(
",") Dim currentRow As String() While Not MyReader.EndOfData TrycurrentRow = MyReader.ReadFields()
Dim currentField As String For Each currentField In currentRow Next Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineExceptionMsgBox(
"Line " & ex.Message & _ "is not valid and will be skipped.") End Try End While End Using
Need help saving what's typed in a textbox
MondeoST24
I got it. Below is the code for anybody who needs it to the whole form.
Public
Class Form1 Dim _numberOfTextboxes As Integer = 2 Dim _text(_numberOfTextboxes - 1) As String Dim _CommaText As String Dim _control As Control
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click For Each _control In Me.Controls If TypeOf _control Is TextBox Then Dim _index As Integer = CInt(_control.Tag)_text(_index - 1) = _control.Text
End If Next ' Join the array into one comma delimited string_CommaText = Join(_text, ","c)
' Save the file, I am using .net 2005. If you are using an ' earlier version you will have to use your method. My.Computer.FileSystem.WriteAllText("bay.txt", _CommaText, False) End Sub Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim filename As String = ("bay.txt") Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8filename =
My.Computer.FileSystem.ReadAllText(filename, encoding) Dim TestArray() As String = Split(filename, ",") For Each _control In Me.Controls If TypeOf _control Is TextBox Then Dim _index As Integer = CInt(_control.Tag)_control.Text = TestArray(_index - 1)
End If Next End SubEnd
ClassAbdul Rehmannn
Bryan;
Addressing your question about getting the 4th textbox to the 4th place, You can utilize your "tag" property of each textbox. For example, the first textbox should have a tag of 1, the second of 2, etc. Once you have done this, the following code should work for you (I tested it).
To get the information back from the file - do the process in reverse. You can load it to a string, use the split function to put it into an array, then use a for/each loop again to reload the text. If you can't work through it, give me a hollar and I can help with that part.
There is probably a cleaner way to do it (please post if you have one) but this method should work OK.
' I used a button to initiate the process for convenience Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim _numberOfTextboxes As Integer = 3 Dim _text(_numberOfTextboxes - 1) As String Dim _CommaText As String Dim _control As Control ' Iterrate through each control on your form For Each _control In Me.Controls ' If the control is a textbox, then add its text ' to an array. Note! You will have to massage the ' code if you have some textboxes that aren't part ' of the information you want to save If TypeOf _control Is TextBox Then Dim _index As Integer = CInt(_control.Tag)_text(_index - 1) = _control.Text
End If Next ' Join the array into one comma delimited string_CommaText = Join(_text, ","c)
' Save the file, I am using .net 2005. If you are using an ' earlier version you will have to use your method. My.Computer.FileSystem.WriteAllText("C:/Test.txt", _CommaText, False) End SubMCVapor
Awsome, that's the hard part as far as I'm concerned. Thank you so much. I took me a little while to get the stuff in the corrent spots on the form but it's working great now, for the most part. I thought I'd have to change the code for each box.
Now I need to get the data into the textbox on load. I know the data is there because I opened the txt file and checked.
Here is my form code:
Public
Class Form1 Dim _numberOfTextboxes As Integer = 2 Dim _text(_numberOfTextboxes - 1) As String Dim _CommaText As String Dim _control As ControlPrivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each _control In Me.Controls
If TypeOf _control Is TextBox Then
Dim _index As Integer = CInt(_control.Tag)_text(_index - 1) = _control.Text
End If Next ' Join the array into one comma delimited string_CommaText = Join(_text, ","c)
' Save the file, I am using .net 2005. If you are using an ' earlier version you will have to use your method. My.Computer.FileSystem.WriteAllText("C:\bay.txt", _CommaText, False) End SubEnd
Class