【问题标题】:New line "\n" does not work yet no errors are given新行“\n”不起作用但没有给出错误
【发布时间】:2021-03-17 22:28:52
【问题描述】:

我有一个程序加载一个选中的列表框,然后用户选择他们想要的项目并选择一个按钮说他们完成了。选中的项目在一个连续的字符串中读取,在每个字符串的末尾添加一个换行符“\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);
    }
}

【问题讨论】:

  • String^ listBoxStr 这个怎么编译?你确定是 C++,而不是其他编程语言?
  • “\n 不起作用”是什么意思?
  • 是的,它是 C++ 并且通过 "\n" 不起作用,因为它不会在每次读取的末尾添加换行符。
  • 也许您应该使用 Add 将字符串一一添加,而不是在添加之前将它们全部附加到一个长字符串中。
  • 我一次加载一个,效果很好。

标签: c++-cli


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多