[英]Multi-line textbox shows mult-line string as One-Line
我正在制作一个程序,您可以在其中输入项目的名称和描述。 然后将其添加到列表框中,完成后可以将所有项目保存到“txt”文件中。 (使用StreamWriter)。 该程序还有一个编辑按钮,允许您通过首先从列表框中删除它然后将其显示回文本框中来编辑列表框中的描述(以便您可以编辑它)
如果描述是多行的,当我在列表框中选择它并单击将在文本框中显示回来的编辑按钮时,它将成功显示多行。 但是如果我先将列表框中的所有项目保存到文件中 。 然后再次打开文件,以便将项目加载回列表框。 然后单击编辑按钮......
多行描述将在文本框中显示为单行描述。
我不知道为什么 - 但我还制作了一个标签,它将显示文本框所显示的确切字符串,标签显示为多行,而文本框则不是!
该字符串肯定是多行的,但多行文本框在使用StreamReader将项目加载回列表框后显示为单行。
多行字符串的示例:(名为“theString1”)
这是第1行
这是第2行
使用以下代码: TextBox1.Text = theString1;
这会出现在文本框中:
这是line1This是line2
但是如果我使用带有标签的相同代码。 它会正确显示它。
如果有人能向我解释为什么会这样,我会非常高兴。 我只需要一个解释。 提前致谢。
- -[更多信息] - -
你知道吗 我自己想出了这个代码所以它可能设置错了。 如果你告诉我一个更好的方法,我会很高兴。
我使用列表来存储描述文本+项目名称。 我用'''分隔了这两个。并拆分了字符串(见代码)。
这是单击编辑按钮时发生的代码。 它会从列表框中删除该项目并将其显示在文本框中,以便您可以对其进行编辑并再次添加到列表框中:
int index = listBox.SelectedIndex;
itemName.Text = listBox.SelectedItem.ToString();
var descVar = descList.ElementAt(index).Split('`');
string theString1 = descVar[1];
TextBox1.Text = theString1;
这是将它保存到文件的方式:
FileDialog save = new SaveFileDialog();
save.Title = "Save information...";
save.DefaultExt = "Text File|*.txt";
save.Filter = "Text File|*.txt";
if (save.ShowDialog() == DialogResult.OK)
{
StreamWriter sw = new StreamWriter(save.FileName);
foreach (string s in listBox.Items) //This writes the names of item names.
{
sw.WriteLine(s);
}
sw.WriteLine("`1`"); //I use this to seperate the item names from description.
foreach (string s in descList) //This writes the descriptions that are stored in a list named "descList".
{
sw.WriteLine(s);
sw.WriteLine("``"); //I use this to seperate descriptions from each other because they are multi-line.
}
sw.WriteLine("`2`"); //Just something so I know where it ends. :D
sw.Close();
}
else
{
}
这就是它的载入方式:(这绝对可以更好!)
FileDialog load = new OpenFileDialog();
load.Title = "Load information...";
load.DefaultExt = "Text File|*.txt";
load.Filter = "Text File|*.txt";
if (load.ShowDialog() == DialogResult.OK)
{
List<string> loadDesc = new List<string>(); //Don't ask you will see why
descList.Clear();
while (listBox.Items.Count > 0) //This removes all items in the listbox to load new ones.
{
int index = 0;
listBox.Items.RemoveAt(index);
descList.Clear();
itemName.Text = "";
}
StreamReader rw = new StreamReader(load.FileName);
for (; true; )
{
string read = rw.ReadLine();
if (read == "`1`") //When it reaches the separator I made it stops reading.
{
break;
}
else
{
listBox.Items.Add(read);
}
}
for (; true; )
{
string read = rw.ReadLine();
if (read == "`2`")
{
break;
}
else
{
loadDesc.Clear();
loadDesc.Add(read);
for (; true; ) //Please tell me if this can be done differently.
{
string read2 = rw.ReadLine();
if (read2 != "``") //This will keep reading the whole description until it reaches the separator.
{
loadDesc.Add(read2); //Adds each line into the list I created.
}
else
{
break;
}
}
string oneBigString = string.Join("\n", loadDesc); //This basically converts all strings in a list into one string.
descList.Add(oneBigString); //And this finally add the string to the main list from where it then loads.
}
}
}
else
{
}
我相信就是这样。 如果您还有其他需要 - 告诉我。
string oneBigString = string.Join("\\n", loadDesc);
是问题所在。
使用Environment.NewLine
而不是\\n
我还要介绍一些可以通过代码改进的东西(有很多东西,但我只想覆盖一对)。
while (listBox.Items.Count > 0) //This removes all items in the listbox to load new ones.
您不需要遍历列表框中的每个元素来删除它。 你可以做listBox.clear()
另外,使用break来摆脱循环通常是不好的做法。 这应该写成......
for (; true; )
{
string read = rw.ReadLine();
if (read == "`1`") //When it reaches the separator I made it stops reading.
{
break;
}
else
{
listBox.Items.Add(read);
}
}
这个
string read = rw.ReadLine()
while(read != "`1`")
{
listBox.Items.Add(read);
read = rw.ReadLine()
}
但更重要的是,如果文件中找不到1
,该怎么办? 它会导致程序崩溃,因此您还需要检查是否有更多数据需要读取...
string read = rw.ReadLine()
while(read != "`1`" && !sw.EndOfStream) // Make sure you're not at the end of the file
{
listBox.Items.Add(read);
read = rw.ReadLine()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.