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!
jedistarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ravi SinghSenior Software EngineerCommented:
Hi, assuming your textboxes are all on the same form, you could use the following procedure:

    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", False)

Call for saving to txt (with comma as delimiter):

Store("C:\Temp\MyFile.txt", True)
-------

Hope this helps!


jedistarAuthor Commented:
How do i make them in order from TextBox1 to TextBox 200 example..
jedistarAuthor Commented:
how do i specify a location to save
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

jedistarAuthor Commented:
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?
Ravi SinghSenior Software EngineerCommented:
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...
jedistarAuthor Commented:
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(sFileName, _
            IO.FileMode.Create, IO.FileAccess.ReadWrite, IO.FileShare.None)

        'Create the stream writer
        Dim myWriter As New System.IO.StreamWriter(myFileStream)

        'Write in what is in the text box
        For Each ctrl In Me.Controls
            If (TypeOf ctrl Is TextBox) AndAlso (ctrl.Name.StartsWith("TextBox")) 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()
jedistarAuthor Commented:
i think i realise the problem, it writes from right to left..
how to inverse this
Ravi SinghSenior Software EngineerCommented:
Hi, you could traverse the Control collection backwards, try:

        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(sFileName, _
            IO.FileMode.Create, IO.FileAccess.ReadWrite, IO.FileShare.None)

        'Create the stream writer
        Dim myWriter As New System.IO.StreamWriter(myFileStream)

        'Write in what is in the text box
        For j As Integer = Me.Controls.Count - 1 To 0 Step -1
            ctrl = Me.Controls(j)
            If (TypeOf ctrl Is TextBox) AndAlso (ctrl.Name.StartsWith("TextBox")) Then
                tb = CType(ctrl, TextBox)
                If Not (tb.Text = "") Then
                    myWriter.Write(tb.Text & " ")
                    i += 1
                End If
            End If
        Next j

        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()


----
This should save them in natural order

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jedistarAuthor Commented:
i tried entering a b c d e
and the data appears in .txt garbage, other than that, all other words work fine
Ravi SinghSenior Software EngineerCommented:
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.
jedistarAuthor Commented:
i entered a b c d e into 5 textboxes
Ravi SinghSenior Software EngineerCommented:
How did they appear in the text file? I'm not sure what you meant by 'garbage'?
jedistarAuthor Commented:
appeared as
&#8289;&#8290;&#8291;&#8292;&#8293;&#8289;&#8290;&#8291;&#8292;&#8293;&#8289;&#8290;&#8291;&#8292;&#8293;5 control chars?
i tried to copy it
but i pasted here..its blank..weird
jedistarAuthor Commented:
do i need to trim?
jedistarAuthor Commented:
ok trim doesnt work
Bob LearnedCommented:
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.UTF8)

Bob
Ravi SinghSenior Software EngineerCommented:
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.
jedistarAuthor Commented:
Zephyr__ , you are right, how do i solve this.
i only happens when i enter a b c d e..
others is fine.
jedistarAuthor Commented:
i would everyone to be able to see it as english
jedistarAuthor Commented:
and btw i'm using english
Bob LearnedCommented:
Is your current culture English-US?

Bob
jedistarAuthor Commented:
yep
jedistarAuthor Commented:
is there anyway i can make it english for everyone
Bob LearnedCommented:
Imports System.Globalization

...


   Threading.Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US")

Bob
jedistarAuthor Commented:
thank you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.