-3

I've created a program to make bug templates and am having a problem with text not saving correctly.

I have a text file called TemplateTexts that holds all the text for each template, a template looks like this -

REQUIREMENTS
- Example

ADDITIONAL REQUIREMENTS
- Example 
- Example
-----COPY BELOW THIS LINE-----

When the program closes it copies all of that into 1 line of a text file. (looks like this)

REQUIREMENTS- Example ADDITIONAL REQUIREMENTS- Example - Example-----COPY BELOW THIS LINE-----

The text file contains 20 lines of templates. The template is saved as 1 line of text into the text file, but when I go to open the program again, it turns that 1 line of text into multiple lines of text like how it is displayed in the first example.

Any idea why this might be happening? or is there a better way to save each template into a text file, possibly by separating it with flags or something?

Here's the code to my program:

public partial class Form1 : Form
{
    static String buttonNamesPath = AppDomain.CurrentDomain.BaseDirectory + "/ButtonNames.txt";
    String[] ButtonNames    = System.IO.File.ReadAllLines(buttonNamesPath);
    static String buttonTextPath = AppDomain.CurrentDomain.BaseDirectory + "/ButtonText.txt";
    String[] ButtonText     = System.IO.File.ReadAllLines(buttonTextPath);

    private void SetupTextField()
    {
        comboBox1.Items.Clear();
        comboBox2.Items.Clear();
        for (int i = 0; i < ButtonNames.Length; i++)
        {
            comboBox1.Items.Insert(i, ButtonNames[i]);
            comboBox2.Items.Insert(i, ButtonNames[i]);
        }
    }

    public Form1()
    {
        InitializeComponent();
        this.FormClosing += this.Form1_FormClosing;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        SetupTextField();
    }

    private void richTextBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        string comboBoxText;
        comboBoxText = comboBox1.SelectedItem.ToString();

        int strNumber;
        int strIndex = 0;
        for (strNumber = 0; strNumber < ButtonNames.Length; strNumber++)
        {
            strIndex = Array.FindIndex(ButtonNames, x => x.Contains(comboBoxText)); 
            if (strIndex >= 0)
                break;
        }

        ButtonNames[strIndex] = textBox1.Text;
        ButtonText[strIndex] = richTextBox2.Text;
        SetupTextField();
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        System.IO.File.WriteAllLines(buttonNamesPath, ButtonNames);
        System.IO.File.WriteAllLines(buttonTextPath, ButtonText);
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void button3_Click(object sender, EventArgs e)
    {
        richTextBox1.Text = "";
    }

    private void button2_Click(object sender, EventArgs e)
    {

        string comboBoxText;
        comboBoxText = comboBox2.SelectedItem.ToString();

        int strNumber;
        int strIndex = 0;
        for (strNumber = 0; strNumber < ButtonNames.Length; strNumber++)
        {
            strIndex = Array.FindIndex(ButtonNames, x => x.Contains(comboBoxText));
            if (strIndex >= 0)
                break;
        }


        richTextBox1.Text = ButtonText[strIndex];
    }

    private void label3_Click(object sender, EventArgs e)
    {

    }
}

I also have 2 text files called ButtonNames.txt and ButtonText.txt

Khakis7
  • 413
  • 4
  • 19
  • 2
    Describe how you are emitting newlines into the file please. – Eric Lippert Jul 20 '16 at 17:56
  • "When the program closes": What program? Yours or Notepad? What does your code look like to write the file? Actually, come to think of it, can you describe in simple, short words what you mean by "a notepad"? Is it a window with a blue icon in the upper left corner? – 15ee8f99-57ff-4f92-890c-b56153 Jul 20 '16 at 17:56
  • I can't get it... Is notepad wrong or your application is not doing right? – jcvegan Jul 20 '16 at 17:57
  • I'm using the windows 7 notepad program that comes with your game. I'll update my question to have the code I use to write to the program and read from it. By closing I mean closing the C# form that is my program via clicking the X – Khakis7 Jul 20 '16 at 17:59
  • 3
    Presumably you're using "\n" for your line endings, which Notepad doesn't recognize as a Windows line ending. Instead, use `Environment.NewLine`. – adv12 Jul 20 '16 at 18:00
  • Ah okay, so after writing to the file add in Enviornment.NewLine? – Khakis7 Jul 20 '16 at 18:01
  • 1
    Now that I see your code, it doesn't look like that's the issue. – adv12 Jul 20 '16 at 18:02
  • It seems to me that when I copy from a richtextbox in C# to a notepad, it messes with all the formating that the richtextbox has. Is there a way to writeline which keeps the format? – Khakis7 Jul 20 '16 at 18:04
  • So when you say "a notepad", you mean a text file, which you're opening in Notepad; is that correct? – 15ee8f99-57ff-4f92-890c-b56153 Jul 20 '16 at 18:07
  • Yeah that's correct. – Khakis7 Jul 20 '16 at 18:07
  • Would it be better/possible to write to a spreadsheet and have each block be a string? – Khakis7 Jul 20 '16 at 18:08
  • For me, `System.IO.File.WriteAllLines()` appends a newline to each line: `System.IO.File.WriteAllLines(@"c:\alllines.txt", new[] { "foo", "bar" });` creates "foo\x0d\x0abar\x0d\x0a" a.k.a. "foo\r\nbar\r\n" on the disk. I'm on Win7 and Notepad displays the newlines just as you'd expect. – 15ee8f99-57ff-4f92-890c-b56153 Jul 20 '16 at 18:10
  • 1
    This question may be relevant: http://stackoverflow.com/questions/7067899/richtextbox-newline-conversion Maybe the carriage returns are getting stripped when you ask for the `RichTextBox`'s `Text` property. – adv12 Jul 20 '16 at 18:14
  • 1
    Oh, wait a minute: Are you TRYING to save without newlines? Is that what you're trying to say? Do you want NO newlines in the file you create? (PLEASE DO NOT USE THE WORD "NOTEPAD" IN YOUR RESPONSE). Try this: `System.IO.File.WriteAllText(String.Join(" ", ButtonNames));` – 15ee8f99-57ff-4f92-890c-b56153 Jul 20 '16 at 18:16
  • I'm trying to save each index of the array as a separate line in the text file, while maintaining the formatting from the richtextbox in my form, does that make sense? – Khakis7 Jul 20 '16 at 18:20
  • What formatting? Bold and italic? Or newlines? How are you getting the text from the RichTextBox? Are you putting rtb.Text in the first entry of an array? Do those arrays have only one item each at the time you write them to the text files? – 15ee8f99-57ff-4f92-890c-b56153 Jul 20 '16 at 18:22
  • 1
    Can you explain what this paragraph was meant to convey: "The template is saved as 1 line of text into the notepad [meaning "the text file"], but when I go to open the program again, it turns that 1 line of text into multiple lines of text like how it is displayed in the first example." – 15ee8f99-57ff-4f92-890c-b56153 Jul 20 '16 at 18:23
  • 1
    FYI, I wrote a quick app to verify my hunch that `RichTextBox.Text` returns `\n` for line endings, so my answer (below) is not just a guess. – adv12 Jul 20 '16 at 18:23
  • 2
    @TimMcGee You have to show us everywhere you are modifying `ButtonNames` and/or `ButtonText`. adv12 likely figured it out with his link to the other SO question, but unless you show us all the relevant code we can't tell you where things went wrong. There is very likely a problem with using the `RichTextBox`'s `Text` property, but we only figured that out after you happened to mention it in a comment. Please add that code to your post. – Quantic Jul 20 '16 at 18:24
  • I've updated my question with my code – Khakis7 Jul 20 '16 at 18:27
  • This is basically what I'm trying to accomplish - Write a template into a richtextbox and click a button to save that text into a string. When the program closes write that string (from an array of strings) into a text file that holds the entire array. Open the program again and have all the templates saved into the text file. – Khakis7 Jul 20 '16 at 18:27
  • Tim, can you add to your question an example, a full example, of your template file, as it is, with the newline problem, but indented by four spaces on the left margin. And then also include an example of what you *want* it to look like, also indented by four spaces at the left margin. Just fix it up by hand in Notepad to look the way you want it to look, add four spaces at the left margin, and paste that in too. – 15ee8f99-57ff-4f92-890c-b56153 Jul 20 '16 at 19:12

1 Answers1

2

When you ask the RichTextBox for its Text property, it converts the rich text it contains internally to a plain-text string, and apparently by default it uses \n to translate line endings. Notepad doesn't recognize \n as a line ending (because it's looking for the official Windows line ending of \r\n), so it displays everything on one line. If you want to save with \r\n for line endings, use string.Replace on the result of RichTextBox.Text to replace \n with \r\n.

See this question and its answers for more details:

RichTextBox Newline Conversion?

Community
  • 1
  • 1
adv12
  • 8,443
  • 2
  • 24
  • 48