1

我正在制作一个程序,您可以在其中输入项目的名称和描述。然后将其添加到列表框中,完成后您可以将所有项目保存到“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
{
}

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

4

1 回答 1

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()
}
于 2013-02-14T23:05:24.187 回答