The problem is likely that you’re starting your counter variables at 1
, then using that as an index into the array, but array’s are zero-based in c# so you’re essentially skipping the first item at 0
and then trying to access an item that’s out of the bounds of the array.
It’s also not accurate to count the \n
characters and expect that to be equal to the number of Environment.NewLine
instances.
It also seems you have a lot of unnecessary variables to track counting. Instead, your code could be reduced to:
string temp = Clipboard.GetText();
string[] lines = temp.Split(new[] {Environment.NewLine}, StringSplitOptions.None);
for (int i = 0; i < lines.Length; i++)
{
ListBox.Items.Add(lines[i]);
}
Or, you could just do it all in one line:
ListBox.Items.AddRange(
Clipboard.GetText().Split(new[] {Environment.NewLine}, StringSplitOptions.None));
solved Read string array with an int selector C# [closed]