Loading data from a text file between two points

MinusZero

Member
Joined
Oct 22, 2012
Messages
7
Programming Experience
1-3
Hi,

I am writing a program which will be able to save richtextbox contents and then load them back. I have been able to save and load without problem, until i thought that it wont handle multiple lines.
When i use multiple rows and save it, the txt file shows it all on one row, but when i load it back, it treats the text as separate rows.

Eg. in rtb1 i have:

Text
Text
Text

rtb2 has
Text2

This saves in a txt file as
TextTextText
Text2

When i load back, it is recognising the line breaks.

The problem is, when using StreamReader to read the lines back, I cant determine how many lines belong to that richtextbox (rtb). I figured i can concatenate a symbol to the start and end of rtb1 contents when it is saved and then load the contents for the particular rtb between the two points (saves as $TextTextText$). Thats where i am having trouble. How can i tell the program to load the text between the symbols into the richtextbox?

I have left out the unimportant code.

C#:
private void btnPDSave_Click(object sender, EventArgs e)
        {
            
                StreamWriter savePD = new StreamWriter("C:\savedfile.txt");
                savePD.WriteLine("$" + rtb1.Text + "$");
                savePD.WriteLine(rtb2.Text);
                savePD.WriteLine(rtb3.Text);
                savePD.WriteLine(rtb4.Text);
                savePD.Close();
        }

private void btnPDLoad_Click(object sender, EventArgs e)
        {
            
                string filename = ""; //create variable for the filename
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    filename = openFileDialog1.FileName; //filename = the full path name




                    using (StreamReader loadPD = File.OpenText(filename))
                    {
                        
                        rtb1.Text = loadPD.ReadLine(); //this reads the saved line
                        rtb2.Text = loadPD.ReadLine();
                        rtb3.Text = loadPD.ReadLine();
                        rtb2.Text = loadPD.ReadLine();


                    }
                }
                else
                { 
                }
            
        }

When loading the file and adding the lines, $Text goes in rtb1, Text in rtb2, Text$ in rtb3 and Text2 in rtb4. Text Text Text with line breaks (and without the symbols) should go in rtb1 and Text2 in rtb2 (rtb3 and 4 are empty in the example). I figure I need to use IndexOf or Contains, but i couldn't make it work.

Help would be appreciated.
 
Firstly, are you using any RTF formatting? If not, why are you using a RichTextBox at all when you could just use a TextBox?

Regardless, why don't you try just writing each control's contents to a single line in the file and replace its own line breaks with a character combination. That way you can just read a single line from the file and know it goes to a single control, replacing the delimiter with a line break. It's important to note that the RichTextBox uses just line feeds as line breaks, regardless of whether you input lone line feeds or carriage return/line feed pairs. E.g.
private const string DELIMITER = "$|$";

private void SaveToFile(string filePath, params Control[] controls)
{
    using (var writer = new StreamWriter(filePath))
    {
        foreach (var control in controls)
        {
            writer.WriteLine(control.Text.Replace("\r\n", DELIMITER).Replace("\n", DELIMITER));
        }
    }
}

private void ReadFromFile(string filePath, params Control[] controls)
{
    using (var reader = new StreamReader(filePath))
    {
        foreach (var control in controls)
        {
            string line;

            if ((line = reader.ReadLine()) == null)
            {
                // No more lines.
                break;
            }

            control.Text = line.Replace(DELIMITER, Environment.NewLine);
        }
    }
}
 
Last edited:
Back
Top Bottom