Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I save a multi-line textbox as one line to a text file?

Ok guys, I am making a function to save a file. I have come across a problem in that when I save the data from multi-line text boxes it saves x amount of lines as x amount of lines in the text file.

So for example if the user entered:

line one
line two
line three

it would show as:

line one
line two
line three

as I want it to display as:

line one \n line two \n line three \n

The code I have is:

                savefile.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                savefile.Title = "Save your file";
                savefile.FileName = "";
                savefile.Filter = "ChemFile (*.cd)|*.cd|All Files|*.*";

                if (savefile.ShowDialog() != DialogResult.Cancel)
                {                    
                   // save the text file information
                    for (int i = 0; i < noofcrit; i++)
                    {
                        cdfile[i] = crittextbs[i].Text;
                    }                    
                }
                    // Compile the file
                    SaveFile = savefile.FileName;
                    System.IO.File.WriteAllLines(SaveFile, cdfile);

Any ideas how I can save multiline text files as one line? Thanks.

like image 372
RHodgett Avatar asked Dec 16 '10 15:12

RHodgett


People also ask

Can a TextBox have multiple lines?

In Windows forms, TextBox plays an important role. With the help of TextBox, the user can enter data in the application, it can be of a single line or of multiple lines. In TextBox, you are allowed to create a multiline TextBox which stores multiple lines of the content using Multiline property of the TextBox.

What is entering multiple lines of data in a text box?

To create a multi-line text input, use the HTML <textarea> tag. You can set the size of a text area using the cols and rows attributes. It is used within a form, to allow users to input text over multiple rows.

What is single line TextBox made of?

<input> elements of type text create basic, single-line inputs.

What is multi-line text?

The Multiline Text Field can be used to store larger amounts of text. The Multiline Text Field offers a lot of formatting options, such as: Adding bulleted and numbered lists. Use bold, italics and underline styling. Change the text format and size.


4 Answers

Replace Newline character with @" \n "or" \\n ", using @ to ignore any escape char

   string s= yourTextBox.Text.Replace(Environment.NewLine, @" \n "));
like image 103
Javed Akram Avatar answered Oct 05 '22 23:10

Javed Akram


I think you may need to do something like this. I'm not actually sure what the best way is to show escape characters. Also, I would use a StreamWriter.

string myData = txtMyTextBox.Text.Replace("\r"," \\r ").Replace("\n"," \\n ");
using(System.IO.StreamWriter sw = new System.IO.StreamWriter(filePath))
{
     sw.Write(myData);
}
like image 45
jocull Avatar answered Oct 06 '22 00:10

jocull


If you get the multi-line strings in a string array, you could just join them into a single line:

string[] multiline = new []{"multi","line","text"};
string singleLine = string.Join(@"\n",multiline);

if it's all a single line, a simple Replace would do the trick,

string singleLine = multiline.Replace("\r",string.Empty).Replace("\n",@"\n");
like image 34
SWeko Avatar answered Oct 06 '22 00:10

SWeko


It's all one line really ;)

Multi-line text boxes depending on platform (Win32 here) will save as:

Line\r\n Line\r\n Line\r\n

So you just need to replace \r\n with \n or whatever character replacement you want.

like image 37
Lloyd Avatar answered Oct 05 '22 22:10

Lloyd