0

I have a program that loads a checked list box, then the user selects the items they want and selects a button to say they are done. The checked items are read in a contiguous string with a newline "\n" at the end of each string added. My problem is everything works ok except the newline "\n", and I don't know why.

Code

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);
    }
}
drescherjm
  • 10,365
  • 5
  • 44
  • 64
PaulaJoann
  • 15
  • 1
  • 7

2 Answers2

0

The string is being formed correctly, but the ListBox control doesn't show newlines in its list items - each item is pushed onto a single line. You can see this by debugging, or by adding a Label to the form with AutoSize set to false - the label will show the newline(s) in the string properly.

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

libertyernie
  • 2,590
  • 1
  • 17
  • 13
-1

Instead of ‘\n’, try ‘\r\n’. This may be a windows thing.