尝试使用多行文本框读取文件时出错。 [英] Error while trying to read a file with a multi-line textbox.

查看:87
本文介绍了尝试使用多行文本框读取文件时出错。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有20多个富文本框,他们都打算输入多行。但是当我尝试加载这个文件时,它会将每行放入一个单独的richtextbox而不是它们都输入的那个。如何绕过这个并且只在特定的文本框中打开要在特定框中打开的内容?



下面是我的开放代码。如果有人需要看,我可以发布我的保存代码。请不要暂时删除打开/保存的复选框,因为我在测试期间一直收到错误,因为它不是有效的布尔值。我从下面的代码中删除了文本框和组合框,因为它说帖子太长了。



我尝试了什么:



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:

        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();
            }
        }

推荐答案

您保存的数据可能包含换行符,这意味着您也不能使用newline作为数据分隔符,因为你的代码读取它时不知道哪个换行符是你数据的一部分,而且应该将数据分成不同的文本框。



如果您的数据是自由格式的,那么将其保存为明文是一个坏主意:我建议您考虑将其存储在XML文件中:输出到XML - Google搜索 [ ^ ]或在一个支持者f中ormat - 这可能有所帮助: ByteArrayBuilder - 字节的StringBuilder [ ^ ]。这是我用于二进制数据的内容,但它很乐意存储和恢复多个字符串。
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.


这篇关于尝试使用多行文本框读取文件时出错。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆