Writing a Multiline Text box to Text File

Bigbadborris

Active member
Joined
Aug 21, 2013
Messages
35
Programming Experience
Beginner
Hi.

I havent written any sort of program for a few years now.
Ive been set a project at work which requires me to write the data in a Multiline text box to a text file
I have manage to write that data to a text file with the followin code (txtMemo being the multiline text box)

VB.NET:
[FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2][COLOR=#000000] cmdEnter_Click([/COLOR][/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2][COLOR=#000000] sender [/COLOR][/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2][COLOR=#000000] System.[/COLOR][/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af]Object[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2][COLOR=#000000], [/COLOR][/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2][COLOR=#000000] e [/COLOR][/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2][COLOR=#000000] System.[/COLOR][/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af]EventArgs[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2][COLOR=#000000]) [/COLOR][/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2][COLOR=#000000] cmdEnter.Click[/COLOR]
[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] MyFile [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] = [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515]"C:\Users\*********\Documents\Visual Studio 2010\Projects\Data Entry\TextData Entry\Data.txt"
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]Using[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] MySW [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af]StreamWriter[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2](MyFile, [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2])
[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]With[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] MySW
.WriteLine(txtName.Text + [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515]" "[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] + txtSurname.Text + [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515]" "[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] + txtDate.Text + [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515]" "[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] + txtPhone.Text)
.WriteLine(txtMemo.Text)
.Close()
[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]With
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]Using
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] 
[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]

However no matter how many lines are written in the textbox the data is written on one continuos line in the text file.
How can i make it write the data to the file in the same format as the way its typed in the textbox.

i.e

this is the data
that I want add
to a text file

Rather than

this is the data that I want to add to a text file.

Hope this makes sence

Many Thanks in advance
 
I think that you may be operating under a misconception. If the WordWrap property of the TextBox is True, which it is by default, then the text in the TextBox will, if long enough, be displayed on multiple lines, even if there are no line breaks in the that text. That is purely a result of the way the TextBox displays the text though. If, for instance, you were to increase the Width of the TextBox, then the text in the control will reflow automatically. So, is that your issue? Are the multiple lines in your TextBox produced simply by wrapping or does it contain actual line breaks?
 
I think that you may be operating under a misconception. If the WordWrap property of the TextBox is True, which it is by default, then the text in the TextBox will, if long enough, be displayed on multiple lines, even if there are no line breaks in the that text. That is purely a result of the way the TextBox displays the text though. If, for instance, you were to increase the Width of the TextBox, then the text in the control will reflow automatically. So, is that your issue? Are the multiple lines in your TextBox produced simply by wrapping or does it contain actual line breaks?

Thank you for your reply

The data in the textbox will be the descriptions of the work carried out on a customers property, and may be 500+ characters. I would like to split the data so that it ends up on several lines of the textfile to make it easier to read. as if it was being writen on a piece of A4 paper.
 
Hi,

As has been said, the text in your TextBox is on multiple lines due to the Word Wrapping feature of the TextBox. If you want to limit the number of words on each line in a Text file then you are going to have to manually add Line Breaks where you want them.

Assuming that the text file that is going to be viewed is using a Fixed Width font then you can split the text in the TextBox and add line breaks as you need. To do this you could follow this logic:-

1) Define what the maximum length of characters is going to be for each line of the file. Lets say 150 characters for example.

2) Create an Array of words by splitting all the words in the TextBox using the space character.

3) Create a temporary string to hold lines of words.

4) Use a For Loop to iterate through each word in the array and if the length of the temporary string, plus the length of the word, plus 1 (for a space character), is less than your maximum length then add the word to the temporary string preceded by a space. If this word breaches your maximum length then add the temporary string to a List(Of String) to save it and then reset the temporary string with the current word.

5) Once you have gone through all the words in the TextBox you will have a List of strings that can then just be saved to your text file with line breaks as you need.

Hope that helps.

Cheers,

Ian
 
Hi,

As has been said, the text in your TextBox is on multiple lines due to the Word Wrapping feature of the TextBox. If you want to limit the number of words on each line in a Text file then you are going to have to manually add Line Breaks where you want them.

Assuming that the text file that is going to be viewed is using a Fixed Width font then you can split the text in the TextBox and add line breaks as you need. To do this you could follow this logic:-

1) Define what the maximum length of characters is going to be for each line of the file. Lets say 150 characters for example.

2) Create an Array of words by splitting all the words in the TextBox using the space character.

3) Create a temporary string to hold lines of words.

4) Use a For Loop to iterate through each word in the array and if the length of the temporary string, plus the length of the word, plus 1 (for a space character), is less than your maximum length then add the word to the temporary string preceded by a space. If this word breaches your maximum length then add the temporary string to a List(Of String) to save it and then reset the temporary string with the current word.

5) Once you have gone through all the words in the TextBox you will have a List of strings that can then just be saved to your text file with line breaks as you need.

Hope that helps.

Cheers,

Ian


This sounds like the best way Thank You. But I must admit that I have never programmed anything like this before. I would never expect someone to write the code for me but do you know if there are any examples of this process to point me in the right direction.

Its been a few years since my last program and im alot greyer on top now :-(

Many thanks
 
Hi,

I should not really do this but have a play with this. It's pretty simple stuff really, however, please read the comments to make sure you fully understand what's going on:-

VB.NET:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
  'Define maximum length of the text for each line
  Const maxNumCharsPerLine As Integer = 20
 
  'Create a list to hold the lines and a temp variable to build the lines
  Dim myListOfLines As New List(Of String)
  Dim strEachLine As String = String.Empty
 
  'Loop through each word with a for loop after splitting the text into words
  For Each strWord In TextBox1.Text.Split(" "c)
    'Check to see if the current string lenght + plus the next word length + 1 for a space if greater
    'then the maximum number of chacaters for each line
    If strEachLine.Length + strWord.Length + 1 > maxNumCharsPerLine Then
      'If the maximum length is breached then add the string to the list and reset the temporary variable
      myListOfLines.Add(strEachLine)
      strEachLine = strWord
    Else
      'Since the maximum length has not been breached continue to build the string with the next word
      'However, if this is the very first wotd then we do not want a space character at the start
      If strEachLine.Length > 0 Then
        strEachLine &= Space(1) & strWord
      Else
        strEachLine = strWord
      End If
    End If
  Next
  'When the For Loop is exited it will always be part way through building the last line of the text
  'so we must ensure that we add that last line to the list
  myListOfLines.Add(strEachLine)
 
  'Finally, you can save the text to a file with a CRLF character. Here I just display it in a MsgBox
  MsgBox(String.Join(Environment.NewLine, myListOfLines.ToArray))
End Sub

Hope that helps.

Cheers,

Ian
 
here is a very simple code you can use:
Private Sub Build_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Build.Click
        SaveFileDialog1.Filter = "Text File|*.txt"
        If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            If SaveFileDialog1.FileName <> " " Then
                FileOpen(1, SaveFileDialog1.FileName, OpenMode.Output)
                PrintLine(1, TextBox1.Text)
                FileClose(1)
            End If
        End If
    End Sub
 
Last edited by a moderator:
here is a very simple code you can use:
Private Sub Build_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Build.Click
        SaveFileDialog1.Filter = "Text File|*.txt"
        If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            If SaveFileDialog1.FileName <> " " Then
                FileOpen(1, SaveFileDialog1.FileName, OpenMode.Output)
                PrintLine(1, TextBox1.Text)
                FileClose(1)
            End If
        End If
    End Sub
I would suggest that any code that contains FileOpen be avoided. You might want to look at the File.WriteAllText method. Also, if it's configured correctly, a SaveFileDialog can't have an invalid FileName if ShowDialog returns OK.
 
Back
Top