jedistar
asked on
Storing values of TextBoxes to .txt and .doc
Hi,
I have 200 textboxes TextBox1, TextBox2.. TextBox200.
A user can enter a word in each textbox, in any of 200 textboxes, from 1 to 200 words in textboxes.
and click "Store" my application will store only those TextBoxes that has values into a .txt and .doc
The user can key into any of the 1-200 textboxes. If the user keys into
TextBox2 - man
TextBox6 - fish
TextBox10 - dog
TextBox15 - cat
*clicks Store*
How do i make it store into file.txt as CSV:
man,fish,dog,cat
How do i make it store into a Word .doc with spacing as:
man fish dog cat
Thanks, help appreciated!
I have 200 textboxes TextBox1, TextBox2.. TextBox200.
A user can enter a word in each textbox, in any of 200 textboxes, from 1 to 200 words in textboxes.
and click "Store" my application will store only those TextBoxes that has values into a .txt and .doc
The user can key into any of the 1-200 textboxes. If the user keys into
TextBox2 - man
TextBox6 - fish
TextBox10 - dog
TextBox15 - cat
*clicks Store*
How do i make it store into file.txt as CSV:
man,fish,dog,cat
How do i make it store into a Word .doc with spacing as:
man fish dog cat
Thanks, help appreciated!
ASKER
How do i make them in order from TextBox1 to TextBox 200 example..
ASKER
how do i specify a location to save
ASKER
I have changed the points to 500 (max) for an addon part to this question.
I wanna also make 2 buttons, "Save" and "Restore"
Save - will save all the textboxes values in TextBox1 to TextBox200 into a the CSV
Restore - will restore all that is saved earlier from the CSV regardless of any changes made
So perhaps when i click "Save" it will do the above. any idea?
I wanna also make 2 buttons, "Save" and "Restore"
Save - will save all the textboxes values in TextBox1 to TextBox200 into a the CSV
Restore - will restore all that is saved earlier from the CSV regardless of any changes made
So perhaps when i click "Save" it will do the above. any idea?
Couple of things:
Will the textboxes be named TextBox1, TextBox2 etc indeffinately? If so, when saving is it OK to save the TextBox's index with the textbox content. i.e.
TextBox2 - man
TextBox6 - fish
TextBox10 - dog
TextBox15 - cat
in a csv file would be:
2, man
6, fish
10, dog
15, cat
i.e. Is it possible to change the file formatting... both for the txt file and doc files? If not it will be slightly tricky (if not impossible) to determine which text goes with which textbox (when restoring) because your initial requirement states that any blank textbox content should not be saved...
Will the textboxes be named TextBox1, TextBox2 etc indeffinately? If so, when saving is it OK to save the TextBox's index with the textbox content. i.e.
TextBox2 - man
TextBox6 - fish
TextBox10 - dog
TextBox15 - cat
in a csv file would be:
2, man
6, fish
10, dog
15, cat
i.e. Is it possible to change the file formatting... both for the txt file and doc files? If not it will be slightly tricky (if not impossible) to determine which text goes with which textbox (when restoring) because your initial requirement states that any blank textbox content should not be saved...
ASKER
ok i got it to write using the below codes..
but the words order are wrong in the a.txt, any one knows why?
Dim i As Integer
Dim ctrl As Control
Dim tb As TextBox
Dim sFileName As String = Application.StartupPath & "\a.txt"
Dim myFileStream As New System.IO.FileStream(sFile Name, _
IO.FileMode.Create, IO.FileAccess.ReadWrite, IO.FileShare.None)
'Create the stream writer
Dim myWriter As New System.IO.StreamWriter(myF ileStream)
'Write in what is in the text box
For Each ctrl In Me.Controls
If (TypeOf ctrl Is TextBox) AndAlso (ctrl.Name.StartsWith("Tex tBox")) Then
tb = CType(ctrl, TextBox)
If Not (tb.Text = "") Then
myWriter.Write(tb.Text & " ")
i += 1
End If
End If
Next
If i = 0 Then
MessageBox.Show("No words written to " & Application.StartupPath & "\a.txt")
Else
MessageBox.Show(i.ToString & " words written to " & Application.StartupPath & "\a.txt")
End If
'Flush before we close
myWriter.Flush()
'Close everything
myWriter.Close()
myFileStream.Close()
but the words order are wrong in the a.txt, any one knows why?
Dim i As Integer
Dim ctrl As Control
Dim tb As TextBox
Dim sFileName As String = Application.StartupPath & "\a.txt"
Dim myFileStream As New System.IO.FileStream(sFile
IO.FileMode.Create, IO.FileAccess.ReadWrite, IO.FileShare.None)
'Create the stream writer
Dim myWriter As New System.IO.StreamWriter(myF
'Write in what is in the text box
For Each ctrl In Me.Controls
If (TypeOf ctrl Is TextBox) AndAlso (ctrl.Name.StartsWith("Tex
tb = CType(ctrl, TextBox)
If Not (tb.Text = "") Then
myWriter.Write(tb.Text & " ")
i += 1
End If
End If
Next
If i = 0 Then
MessageBox.Show("No words written to " & Application.StartupPath & "\a.txt")
Else
MessageBox.Show(i.ToString
End If
'Flush before we close
myWriter.Flush()
'Close everything
myWriter.Close()
myFileStream.Close()
ASKER
i think i realise the problem, it writes from right to left..
how to inverse this
how to inverse this
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
i tried entering a b c d e
and the data appears in .txt garbage, other than that, all other words work fine
and the data appears in .txt garbage, other than that, all other words work fine
what do you mean by 'garbage', can you give an example?
If you mean you entered "a b c d e" in one textbox, then yes, you will have problems when reading the words in from the textfile... i.e. "a", "b", "c", "d", "e" will be considered as seperate words and will be placed in seperate textboxes... you don't currently have any way of distinguishing each words' corresponding textbox...
So will the words themselves (in each textbox) contain a space?... if so, it may conflict with your space delimiter.
If you mean you entered "a b c d e" in one textbox, then yes, you will have problems when reading the words in from the textfile... i.e. "a", "b", "c", "d", "e" will be considered as seperate words and will be placed in seperate textboxes... you don't currently have any way of distinguishing each words' corresponding textbox...
So will the words themselves (in each textbox) contain a space?... if so, it may conflict with your space delimiter.
ASKER
i entered a b c d e into 5 textboxes
How did they appear in the text file? I'm not sure what you meant by 'garbage'?
ASKER
appeared as
⁡⁢⁣̽ 2;⁥& #8289;R 90;⁣ ⁤ 293;⁡ ;⁢&# 8291;̽ 2;⁥5 control chars?
i tried to copy it
but i pasted here..its blank..weird
⁡⁢⁣̽
i tried to copy it
but i pasted here..its blank..weird
ASKER
do i need to trim?
ASKER
ok trim doesnt work
What is your code page? Are you using English, US? My guess is that you aren't and you are going to need to use an encoder.
Example:
Dim writer As New IO.StreamWriter(path, System.Text.UTF8Encoding.U TF8)
Bob
Example:
Dim writer As New IO.StreamWriter(path, System.Text.UTF8Encoding.U
Bob
Hi,
Are you using notepad to view the text file? If so, open the file with ANSI encoding... from notepad... File -> Open --> Select the file --> Set the Encoding drop down list to ANSI.
Hope this helps.
Are you using notepad to view the text file? If so, open the file with ANSI encoding... from notepad... File -> Open --> Select the file --> Set the Encoding drop down list to ANSI.
Hope this helps.
ASKER
Zephyr__ , you are right, how do i solve this.
i only happens when i enter a b c d e..
others is fine.
i only happens when i enter a b c d e..
others is fine.
ASKER
i would everyone to be able to see it as english
ASKER
and btw i'm using english
Is your current culture English-US?
Bob
Bob
ASKER
yep
ASKER
is there anyway i can make it english for everyone
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thank you.
Private Sub Store(ByVal FilePath As String, ByVal CSV As Boolean)
Dim SR As New StreamWriter(FilePath)
For Each C As Control In Me.Controls
If (TypeOf (C) Is TextBox) Then
If (C.Text.Trim() <> String.Empty) Then
If (CSV = True) Then
SR.Write(C.Text & ",")
Else
SR.Write(C.Text & " ")
End If
End If
End If
Next
SR.Close()
End Sub
----------
Call for saving to doc (with spaces as delimiter):
Store("C:\Temp\MyFile.doc"
Call for saving to txt (with comma as delimiter):
Store("C:\Temp\MyFile.txt"
-------
Hope this helps!