Question

I'm switching from VB.NET to C#.

I'm trying to copy the clipboard content to a ListBox.

I use the code below in VB.NET:

Dim getClipboard As String() = Split(Clipboard.GetText, vbNewLine)
lstTarget.Items.AddRange(getClipboard)

I couldn't find what to use to split "\n".

So far I was able to get clipboard text with

(Clipboard.GetText(TextDataFormat.Text)

I tried working with string[] and List<string> but I messed up with index or lengths and couldn't figure out what to do.

Was it helpful?

Solution

string text = Clipboard.GetText(TextDataFormat.Text);
lstTarget.Items.AddRange(text.Split("\n")));

OTHER TIPS

You can split the text and add it directly to the list:

lstTarget.AddRange(Clipboard.GetText(TextDataFormat.Text)
                            .Split(new[] { Environment.NewLine },
                                   StringSplitOptions.RemoveEmptyEntries));

You use .Split() to split strings

Clipboard.GetText().Split('\n').ToList().ForEach(line => lstTarget.Items.Add(line));

or

lstTarget.AddRange(Clipboard.GetText().Split('\n');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top