多行文本框将多行字符串显示为一行

时间:2013-02-14 19:54:26

标签: c# string textbox multiline

我正在制作一个程序,您可以在其中输入项目的名称及其描述。然后将其添加到列表框中,完成后可以将所有项目保存到“txt”文件中。 (使用StreamWriter)。此程序还有一个编辑按钮,允许您通过首先从列表框中删除它然后在文本框中显示它来编辑列表框中的描述(以便您可以编辑它)

如果描述是多行的,当我在列表框中选择它并单击将在文本框中显示回来的编辑按钮时,它将成功显示多行。 但是,如果我将列表框中的所有项目首先保存到文件。然后再次打开文件,以便将项目加载回列表框。然后单击编辑按钮......

多行描述将在文本框中显示为单行描述。

我不确定为什么 - 但我还制作了一个标签,它会显示文本框所显示的确切字符串,标签显示为多行,而文本框则不是!

该字符串肯定是多行的,但多行文本框在使用StreamReader将项目重新加载到列表框后显示为单行。

多行字符串示例:(名为“theString1”)

  

这是第1行

     

这是第2行

使用以下代码:TextBox1.Text = theString1;这将显示在文本框中:

  

这是第1行,这是第2行

但是如果我使用带标签的相同代码。它会正确显示。

如果有人能向我解释为什么会这样,我会非常高兴。我只需要一个解释。 提前谢谢。

--- [更多信息] ---

就是这样,你知道。我自己想出了这个代码,所以它可能设置错误了。 如果你告诉我一个更好的方法,我会很高兴。

我使用列表来存储描述文本+项目名称。我用'''分隔了这两个。并拆分了字符串(见代码)。

这是单击编辑按钮时发生的代码。它从中删除项目 列表框并在文本框中显示它,以便您可以编辑它并再次添加到列表框:

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
{
}

我相信就是这样。 如果您还有其他需要 - 请告诉我。

1 个答案:

答案 0 :(得分:3)

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()
}
相关问题