简体   繁体   中英

Adding line breaks in text box when reading from txt file

I'm trying to have contents from multiple textboxes, comboboxes, and even a dgv save to a txt file for reopening by the user later. Some of the textboxes are multiline so I'm using the current code to save the content:

        private void label31_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                using (StreamWriter write = new StreamWriter(File.Create(sfd.FileName)))
                {
                    write.WriteLine(date.Text);
                    write.WriteLine(compName.Text);
                    write.WriteLine(jobLocation.Text);
                    write.WriteLine(county.Text);
                    write.WriteLine(titleWork.Text);
                    write.WriteLine(lineVoltage.Text);
                    write.WriteLine(woNum.Text);
                    write.WriteLine(ordBy.Text);
                    write.WriteLine(super.Text);
                    write.WriteLine(area.Text);
                    write.WriteLine(bidOrTM.Text);
                    write.WriteLine(proMan.Text);
                    write.WriteLine(workSchDay.Text);
                    write.WriteLine(hoursNearest.Text);
                    write.WriteLine(perDiem.Text);
                    write.WriteLine(perDiemPerDay.Text);
                    write.WriteLine(rock.Text);
                    write.WriteLine(hotWork.Text);
                    string mltb1 = jobStory.Text.Replace(Environment.NewLine, @" \n ");
                    write.WriteLine(mltb1);
                    write.WriteLine(dtPurpose.Text);
                    write.WriteLine(gpsLat.Text);
                    write.WriteLine(gpsLong.Text);
                    write.WriteLine(city.Text);
                    write.WriteLine(majIntersect.Text);
                    write.WriteLine(pocPhone.Text);
                    string mltb2 = markInstr.Text.Replace(Environment.NewLine, @" \n ");
                    write.WriteLine(mltb2);
                    write.WriteLine(utilityComp.Text);
                    string mltb3 = utilityWork.Text.Replace(Environment.NewLine, @" \n ");
                    write.WriteLine(mltb3);
                    write.WriteLine(printedDate.Text);
                    write.WriteLine(reqNum.Text);

                    string sLine = "";

                    for (int r = 0; r <= stkSheetdgv.Rows.Count - 1; r++)
                    {
                        for (int c = 0; c <= stkSheetdgv.Columns.Count - 1; c++)
                        {
                            sLine = sLine + stkSheetdgv.Rows[r].Cells[c].Value;
                            if (c != stkSheetdgv.Columns.Count - 1)
                            {
                                sLine = sLine + ", ";
                            }
                        }
                        write.WriteLine(sLine);
                        sLine = "";
                    }
                    write.Close();
                }
            }
        }

Here is the code I'm using when opening the txt file:

        private void label30_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                StreamReader read = new StreamReader(File.OpenRead(ofd.FileName));
                date.Text = read.ReadLine();
                compName.Text = read.ReadLine();
                jobLocation.Text = read.ReadLine();
                county.Text = read.ReadLine();
                titleWork.Text = read.ReadLine();
                lineVoltage.Text = read.ReadLine();
                woNum.Text = read.ReadLine();
                ordBy.Text = read.ReadLine();
                super.Text = read.ReadLine();
                area.Text = read.ReadLine();
                bidOrTM.Text = read.ReadLine();
                proMan.Text = read.ReadLine();
                workSchDay.Text = read.ReadLine();
                hoursNearest.Text = read.ReadLine();
                perDiem.Text = read.ReadLine();
                perDiemPerDay.Text = read.ReadLine();
                rock.Text = read.ReadLine();
                hotWork.Text = read.ReadLine();
                jobStory.Text = read.ReadLine();
                dtPurpose.Text = read.ReadLine();
                gpsLat.Text = read.ReadLine();
                gpsLong.Text = read.ReadLine();
                city.Text = read.ReadLine();
                majIntersect.Text = read.ReadLine();
                pocPhone.Text = read.ReadLine();
                markInstr.Text = read.ReadLine();
                utilityComp.Text = read.ReadLine();
                utilityWork.Text = read.ReadLine();
                printedDate.Text = read.ReadLine();
                reqNum.Text = read.ReadLine();

                string[] lines = File.ReadAllLines(ofd.FileName);
                string[] values;

                for (int i = 30; i < lines.Length; i++)
                {
                    values = lines[i].ToString().Split(',');
                    string[] row = new string[values.Length];

                    for (int j = 0; j < values.Length; j++)
                    {
                        row[j] = values[j].Trim();
                    }
                    stkSheetdgv.Rows.Add(row);

                    read.Close();
                    read.Dispose();

                }
                
            }
        }

The problem I'm running into is when the text values for the textboxes with multiline populate they show something like this:

value \n value

When I need it to show:

value

value

I'm sure its a simple solution, I'm just ignorant.

Thanks for the help.

Just what @Magnetron was saying, when you write to file, you are replacing the system newline with '\n'

Take these lines for example:

tring mltb1 = jobStory.Text.Replace(Environment.NewLine, @" \n "); and write.WriteLine(mltb1);

When you do that, the text in your file is literally using the characters '\n'. When you read it back in, you'll need to reverse that action by performing Text.Replace(@"\n", Environment.NewLine) on anything that could be multi-line. This reversal needs to take place when reading in the file that was previously generated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM