Question

How do I Paste one line from the clipboard at a time in C#?

I am getting a argumentNullException on this line:

Clipboard.SetText( nextLine(clipboardText) );

I had nextLine() return an empty string if it cannot get text from the clipboard so it should never return null.

When I hit CTRL + C it will get all the clipboard text and store it in clipboardText.

When I hit CTRL + V it is supposed to paste the top line from clipboardText in the system clipboard and then paste it.

            if (Keys.C == (Keys)vkCode && Keys.Control == Control.ModifierKeys)
            {
                Console.WriteLine("CTRL+C");
                clipboardText = getTheClipboardText();
            }
            else if (Keys.V == (Keys)vkCode && Keys.Control == Control.ModifierKeys)
            {
                    Clipboard.SetText(nextLine(clipboardText)); //nextline returns a string of one line

                    clipboardText = removeFirstLine(clipboardText); //remove first line returns a string missing the first line

            }
     }
Était-ce utile?

La solution

It sounds like nextLine is returning null in some cases, so you need to think about how you want to handle it. For example:

string line = nextLine(clipboardText);
if (line == null)
{
    // Nothing more to do.
    return ...; // TODO: Work out what value to return
}
Clipboard.SetText(line);
clipboardText = removeFirstLine(clipboardText);

Alternatively, if your nextLine method should never return null, then that indicates that it's got a bug.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top