I've got a program that is essentially a big form with text boxes, combo boxes and radio buttons that exports a .txt file in a hard coded format, when I manually fill out the form and export it all of my written formatting stays in place. Here's a super simple example of what I'm doing: (The exported file is broken up into sub sections, and each subsection may have a different number of values under it based on form input)
PredefinedVariable1 = "Section Title:\n" + "\tDescription of TextBox1: " + TextBox1.Text;
PredefinedVariable2 = "\tDescription of TextBox2: " + TextBox2.Text;
string[] Values_Array = { PredefinedVariable1, PredefinedVariable2 };
using (SystemIO.StreamWriter file = new System.IO.StreamWriter(//Location and naming convention for my file))
{
file.WriteLine("Form Title:");
for each (string line in Values_Array)
{
if (line != null)
{
file.WriteLine(line);
}
}
}
Like I said, this works, but what I'm working on now is importing that exported file, Here's another simple version of what I'm doing once I navigate to the file in an OpenFileDialog:
Form where I can browse for a file and open it:
using (StreamReader FileReader = new StreamReader(NewFile.FileName))
{
//Code to save the file name to a variable defined in a separate class.
//Code to parse the file directory portion of the file name. returning the NewFile.Formatted
//Code that checked the formatted file name for a known value.
//More code that takes the file and adds each line to a list of strings.
for each (line in ListOfLines)
{
switch (line)
{
case String a when a.Contains("TextBox1:"): VariableToHoldTextBox1Value = a; continue;
case String b when b.Contains("TextBox2:"): VariableToHoldTextBox2Value = b; continue;
}
}
}
Back to the Form where I input the values to export the file:
//On File Load:
TextBox1.Text = VariableToHoldTextBox1Value; //I'm parsing out JUST the value and NOT the formatted text
TextBox2.Text = VariableToHoldTextBox2Value;
The exported .txt file will follow this general format: (Descriptions will be indented with \t)
Form Title/Section 1 Title:
Field 1 Description:
Field 2 Description:
Section 2 Title:
Field 3 Description:
Field 4 Description:
Section 3 Title:
Field 5 Description:
Field 6 Description:
As I said when I manually fill in the form and export it, it will export the file formatted exactly how I want, and when I import the file it will properly parse out and trim just the value from each line and fill in the correct values on the form based on a unique bit of text in each line, but when I export the form based on an Imported file I lose SOME of the formatting and I can't determine why, I've tweaked several of the export format text and the contains text on the import and still have the same issue, here's what it looks like when exporting an imported file:
Form Title/Section 1 Title:
Field 1 Description:
Field 2 Description:
Field 3 Description: (Missing "Section 2 Title:" and bumping these lines up and together)
Field 4 Description:
Section 3 Title:
Field 5 Description:
Field 6 Description: \
I've been trying everything I can think of to isolate the issue for a few hours yesterday and a few more today and can't seem to make it work, even when I DRASTICALLY change what my export text and Contains text lines to ensure there isn't some additional likeness to a separate line. Is there something else I can try?
Textbox1:blah\nblah\n
. When you try to read this you'll only get the first text line. Instead of creating your own format, why not use JSON, CSV, YAML etc which already handle newlines and escaping and are supported by libraries? - Panagiotis Kanavos