Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
So I have 20+ richtextboxes and they are all meant to have multiple lines typed into them. But when I try to load this file it puts each line into a separate richtextbox instead of the one they were all typed into. How do I get around this and only have what is typed into a specific richtextbox be opened in that specific box?

Below is my open code. I can post my save code if anyone needs to see it. Please not I've temporarily removed the checkboxes from opening/saving because I kept getting an error during testing for not being a valid boolean. I removed the textboxes and comboboxes from the code below because it said the post was too long.

What I have tried:

C#
        private void OpenToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            ofd.Filter = "WAP Files (.wap)|*.wap";
            ofd.Title = "Open File";
            
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                StreamReader read = new StreamReader(File.OpenRead(ofd.FileName));

                //replacefridge_ckbox.Checked = Convert.ToBoolean(read.ReadLine());
                //vhoodcab_ckbox.Checked = Convert.ToBoolean(read.ReadLine());
                //vhoodnocab_ckbox.Checked = Convert.ToBoolean(read.ReadLine());
                //furdown_ckbox.Checked = Convert.ToBoolean(read.ReadLine());
                //exhaust1_ckbox.Checked = Convert.ToBoolean(read.ReadLine());
                //exhaustkit1_ckbox.Checked = Convert.ToBoolean(read.ReadLine());
                //timer1_ckbox.Checked = Convert.ToBoolean(read.ReadLine());
                //exhaust2_ckbox.Checked = Convert.ToBoolean(read.ReadLine());
                //exhaustkit2_ckbox.Checked = Convert.ToBoolean(read.ReadLine());
                //timer2_ckbox.Checked = Convert.ToBoolean(read.ReadLine());
                //replacewh_ckbox.Checked = Convert.ToBoolean(read.ReadLine());
                //newcloset_ckbox.Checked = Convert.ToBoolean(read.ReadLine());
                //repaircloset_ckbox.Checked = Convert.ToBoolean(read.ReadLine());
                //replacestove_ckbox.Checked = Convert.ToBoolean(read.ReadLine());
                //smokealarm_ckbox.Checked = Convert.ToBoolean(read.ReadLine());
                //codetector_ckbox.Checked = Convert.ToBoolean(read.ReadLine());
                //dryervent_ckbox.Checked = Convert.ToBoolean(read.ReadLine());
                //replacefurnace_ckbox.Checked = Convert.ToBoolean(read.ReadLine());

......

                wallcomments.Text = read.ReadLine();
                floorcomments.Text = read.ReadLine();
                atticcomments.Text = read.ReadLine();
                fridgecomments.Text = read.ReadLine();
                door1comments.Text = read.ReadLine();
                door2comments.Text = read.ReadLine();
                door3comments.Text = read.ReadLine();
                door4comments.Text = read.ReadLine();
                livingroomcomments.Text = read.ReadLine();
                diningroomcomments.Text = read.ReadLine();
                kitchencomments.Text = read.ReadLine();
                hallwaycomments.Text = read.ReadLine();
                bathroom2comments.Text = read.ReadLine();
                bathroom1comments.Text = read.ReadLine();
                bedroom2comments.Text = read.ReadLine();
                bedroom1comments.Text = read.ReadLine();
                bedroom4comments.Text = read.ReadLine();
                bedroom3comments.Text = read.ReadLine();
                addroom2comments.Text = read.ReadLine();
                addroom1comments.Text = read.ReadLine();
                whcomments.Text = read.ReadLine();
                h_scomments.Text = read.ReadLine();
                hvaccomments.Text = read.ReadLine();
                
                fileName = ofd.FileName;
                filename_label.Text = System.IO.Path.GetFileName(fileName);

                read.Close();
                read.Dispose();
            }
        }
Posted
Updated 14-Jun-17 4:53am
Comments
ZurdoDev 14-Jun-17 10:40am    
Well, you clearly can't use ReadLine. You need to write code to check when you have actually reached the next line.
Richard MacCutchan 14-Jun-17 10:41am    
The code is doing what you tell it, you need to look at whether you are selecting the correct boxes for each line. It is never a good idea to assume that what you read from the file is the correct data, you need some way of adding some metadata to tell you which textbox the line belongs to.
[no name] 14-Jun-17 10:41am    
It will be writting to each textbox because you are assigning "read.ReadLine();" to each of them. Here is your code -

wallcomments.Text = read.ReadLine();
floorcomments.Text = read.ReadLine();
atticcomments.Text = read.ReadLine();
fridgecomments.Text = read.ReadLine();
door1comments.Text = read.ReadLine();
and so on ....

1 solution

The chances are that the data you are saving contains newline characters, which means you can't use newline as a "data separator" as well because your code when it reads it has no idea which newline is part of your data, and which is supposed to separate data into different textboxes.

If you data is freeform, then saving it in "clear text" is a bad idea: I'd suggest you look at storing it perhaps in an XML file: Output to XML - Google search[^] or in a propriatory format - this may help: ByteArrayBuilder - a StringBuilder for Bytes[^]. It's what I use for my binary data, but it happily stores and restore multiple strings.
 
Share this answer
 
Comments
Robmeister8911 15-Jun-17 17:46pm    
So I have found that XmlWriter will work for me... however.. I'm having trouble figuring out how to save the state of a checkbox in XmlWriter. Any help on that topic?
private void save_xml_Click(object sender, EventArgs e)
        {
            sfd.Filter = "XML Files (.xml)|*.xml";
            sfd.Title = "Save As";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                XmlWriter writer = XmlWriter.Create(sfd.FileName);

                writer.WriteStartDocument();
                writer.WriteStartElement("Assessment_Info");

                writer.WriteStartElement("Date");
                writer.WriteString(date_tbox.Text);
                writer.WriteEndElement();

                writer.WriteStartElement("Name");
                writer.WriteString(name_tbox.Text);
                writer.WriteEndElement();

                writer.WriteStartElement("Address");
                writer.WriteString(address_tbox.Text);
                writer.WriteEndElement();
                
                

                writer.WriteStartElement("Exterior_Type");
                writer.WriteString(exterior_cbox.Text);
                writer.WriteEndElement();

                writer.WriteStartElement("Wall_Comments");
                writer.WriteString(wallcomments.Text);
                writer.WriteEndElement();


                writer.WriteEndDocument();

                writer.Close();

            }
        }
OriginalGriff 16-Jun-17 1:33am    
What's the problem? What have you tried to do to save the state?
Can you not use
WriteString(myCheckBox.Checked.ToString());
Robmeister8911 16-Jun-17 9:20am    
That worked, thanks. How about loading it back as boolean, though?
        private XmlDocument doc;
        private XmlElement root;

        private void open_xml_Click(object sender, EventArgs e)
        {
            ofd.Filter = "XML Files (.xml)|*.xml";
            ofd.Title = "Open File";

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(ofd.FileName);
                root = doc.DocumentElement;
                date_tbox.Text = root.GetElementsByTagName("Date")[0].InnerText;
                name_tbox.Text = root.GetElementsByTagName("Name")[0].InnerText;
                address_tbox.Text = root.GetElementsByTagName("Address")[0].InnerText;

                //replacefridge_ckbox.Checked = root.GetElementsByTagName("Replace_Fridge")[0].InnerText;

                exterior_cbox.Text = root.GetElementsByTagName("Exterior_Type")[0].InnerText;
                wallcomments.Text = root.GetElementsByTagName("Wall_Comments")[0].InnerText;

                fileName = ofd.FileName;
                filename_label.Text = System.IO.Path.GetFileName(fileName);

            }
        }
OriginalGriff 16-Jun-17 9:35am    
It's a bool value: so the string will be either "True" or "False" - and bool has both a Parse and a TryParse method:
https://msdn.microsoft.com/en-us/library/system.boolean.parse(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.boolean.tryparse(v=vs.110).aspx
Robmeister8911 16-Jun-17 14:33pm    
I understand its a boolean, I'm asking how do I convert it? I read the links you sent me but its not helping.. I'm a noob at coding so there's a lot I cannot do LOL

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900