简体   繁体   English

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

[英]Multi-line textbox shows mult-line string as One-Line

I am making a program where you enter an item's name and it's description. 我正在制作一个程序,您可以在其中输入项目的名称和描述。 Then you add it to a listbox and after you are done you can save all the items to a 'txt' file. 然后将其添加到列表框中,完成后可以将所有项目保存到“txt”文件中。 (Using StreamWriter). (使用StreamWriter)。 This program also has an edit button that allows you to edit the description in the listbox by removing it first from the listbox and then showing it back in the textbox (, so you can edit it) 该程序还有一个编辑按钮,允许您通过首先从列表框中删除它然后将其显示回文本框中来编辑列表框中的描述(以便您可以编辑它)

If the description is multi-line, it will successfuly show it multi-line when I select it in the listbox and click the edit button that will show it back in the textbox. 如果描述是多行的,当我在列表框中选择它并单击将在文本框中显示回来的编辑按钮时,它将成功显示多行。 BUT if I save all the items in the listbox to a file first . 但是如果我先将列表框中的所有项目保存到文件中 And then open up the file again so it load the items back into the listbox. 然后再次打开文件,以便将项目加载回列表框。 And then clicking the edit button... 然后单击编辑按钮......

The multi-line description will show as a one-line description in the textbox. 多行描述将在文本框中显示为单行描述。

I am not sure why - but I've also made a label that will show the exact string that the textbox is suppose to show and the label is showing it multi-lined while textbox isn't! 我不知道为什么 - 但我还制作了一个标签,它将显示文本框所显示的确切字符串,标签显示为多行,而文本框则不是!

The string is definitely multi-line but the multi-line textbox is showing it one-line after loading the items back into the listbox using StreamReader. 该字符串肯定是多行的,但多行文本框在使用StreamReader将项目加载回列表框后显示为单行。

Example of the multi-line string: (named "theString1") 多行字符串的示例:(名为“theString1”)

This is line 1 这是第1行

This is line 2 这是第2行

Using the following code: TextBox1.Text = theString1; 使用以下代码: TextBox1.Text = theString1; this appears in the text box: 这会出现在文本框中:

This is line1This is line2 这是line1This是line2

But if I use the same code with a label. 但是如果我使用带有标签的相同代码。 It will show it correctly. 它会正确显示它。

If someone can explain to me why this is happening I will be more than happy. 如果有人能向我解释为什么会这样,我会非常高兴。 I just need an explanation. 我只需要一个解释。 Thanks in advance. 提前致谢。

---[More info]--- - -[更多信息] - -

Just so you know. 你知道吗 I came up with this code myself so it is probably set-up all wrong. 我自己想出了这个代码所以它可能设置错了。 I will be happy if you tell me a better way to do this. 如果你告诉我一个更好的方法,我会很高兴。

I am using a list to store the description text + the item name. 我使用列表来存储描述文本+项目名称。 I seperated these two using '`' .And splited the string (see code). 我用'''分隔了这两个。并拆分了字符串(见代码)。

This is the code that happens when you click the edit button. 这是单击编辑按钮时发生的代码。 It removes the item from the listbox and shows it in the textbox so you can edit it and add to listbox again: 它会从列表框中删除该项目并将其显示在文本框中,以便您可以对其进行编辑并再次添加到列表框中:

int index = listBox.SelectedIndex;

itemName.Text = listBox.SelectedItem.ToString();

var descVar = descList.ElementAt(index).Split('`');   
string theString1 = descVar[1];  

TextBox1.Text = theString1;

This is how it saves it to a file: 这是将它保存到文件的方式:

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

And this is how it loads: (This can definitely be better!) 这就是它的载入方式:(这绝对可以更好!)

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

I believe that is it. 我相信就是这样。 If there is anything else you need - tell me. 如果您还有其他需要 - 告诉我。

string oneBigString = string.Join("\\n", loadDesc); is where the issue is. 是问题所在。

Use Environment.NewLine instead of \\n 使用Environment.NewLine而不是\\n

I'm also just going to go over a couple of things that could be improved with your code (there are a lot, but I just want to cover a couple). 我还要介绍一些可以通过代码改进的东西(有很多东西,但我只想覆盖一对)。

while (listBox.Items.Count > 0) //This removes all items in the listbox to load new ones.

You don't need to iterate over every element in the listbox to remove it. 您不需要遍历列表框中的每个元素来删除它。 You can just do listBox.clear() 你可以做listBox.clear()

Also, using break to get out of loops is generally bad practice. 另外,使用break来摆脱循环通常是不好的做法。 This should be written as... 这应该写成......

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

this 这个

string read = rw.ReadLine()
while(read != "`1`")  
{
   listBox.Items.Add(read);
   read = rw.ReadLine()
}

but theres more, what if 1 is never found in the file? 但更重要的是,如果文件中找不到1 ,该怎么办? It would crash your program, so you also need to check if there is more data to be read... 它会导致程序崩溃,因此您还需要检查是否有更多数据需要读取...

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.

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