Queue
Queue

Reputation: 456

Copy to clipboard escape characters not remaining

I have a winform file that contains a button, copyBtn, that when clicked should copy the contents of a listbox, stringsListBox, to the clipboard.

Using the implementation below, the escape character \n is not being recognized when I paste to a text document.

Can I keep the escape character while copying to the clipboard?

private void copyBtn_Click(object sender, EventArgs e)
{
    string copyString = "";

    if(stringsListBox.Items.Count > 0)
    {
        foreach(string item in changesListbox.Items)
        {
            copyString += item + "\n";
        }
    }

    Clipboard.SetText(copyString);
}

If the listbox contains the following values: (1, 2, 3, 4) then when I paste to a text file the output is on one line:

1234 

When I desire it to be one number per line:

1
2
3
4

Upvotes: 0

Views: 1041

Answers (1)

Zein Makki
Zein Makki

Reputation: 30042

When dealing with text files, you need to use \r\n:

copyString += item + "\r\n";

Or better:

copyString += item + Environment.NewLine;

result in file:

1
2
3
4

From Docs : Environment.NewLine Property

**Action: A property which returns the appropriate newline string for the given platform.

**Returns: \r\n on Win32.

Upvotes: 3

Related Questions