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.
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.
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.
<input> elements of type text create basic, single-line inputs.
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.
Replace Newline
character with @" \n "
or" \\n "
, using @
to ignore any escape char
string s= yourTextBox.Text.Replace(Environment.NewLine, @" \n "));
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);
}
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");
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With