Link to home
Start Free TrialLog in
Avatar of jedistar
jedistarFlag for Singapore

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!
Avatar of Ravi Singh
Ravi Singh
Flag of United Kingdom of Great Britain and Northern Ireland image

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!


Avatar of jedistar

ASKER

How do i make them in order from TextBox1 to TextBox 200 example..
how do i specify a location to save
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?
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...
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()
i think i realise the problem, it writes from right to left..
how to inverse this
ASKER CERTIFIED SOLUTION
Avatar of Ravi Singh
Ravi Singh
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i tried entering a b c d e
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.
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'?
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
do i need to trim?
ok trim doesnt work
Avatar of Bob Learned
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
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.
Zephyr__ , you are right, how do i solve this.
i only happens when i enter a b c d e..
others is fine.
i would everyone to be able to see it as english
and btw i'm using english
Is your current culture English-US?

Bob
yep
is there anyway i can make it english for everyone
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thank you.