新行“\n”不起作用但没有给出错误

问题描述

我有一个程序可以加载一个选中的列表框,然后用户选择他们想要的项目并选择一个按钮来表示他们已经完成。选中的项目在一个连续的字符串中读取,每个字符串的末尾都有一个换行符“\n”。我的问题是除了换行符“\n”之外一切正常,我不知道为什么。

代码

private: System::Void bntSelected_Click(System::Object^ sender,System::EventArgs^ e) {

    int numSelected = this->checkedListBox1->CheckedItems->Count;

    if (numSelected != 0)
    {
        // Set input focus to the list Box.
        //SetFocus(lstReceiver);
        String^ listBoxStr = "";
        // If so,loop through all checked items and print results.  
        for (int x = 0; x < numSelected; x++)
        {
            listBoxStr = listBoxStr + (x + 1).ToString() + " = " + this->checkedListBox1->CheckedItems[x]->ToString() + "\n";
        }
        lstReceiver->Items->Add(listBoxStr);
    }
}

解决方法

字符串的格式正确,但 ListBox 控件没有在其列表项中显示换行符 - 每个项都被推到一行上。您可以通过调试或通过向表单添加 Label 并将 AutoSize 设置为 false 来查看这一点 - 标签将正确显示字符串中的换行符。

相关(C#):How to put a \n(new line) inside a list box?

,

尝试使用“\r\n”而不是“\n”。这可能是 Windows 的事情。