1

当用户粘贴到 ListBox 中时,我希望剪贴板中的内容作为单独的项目排列。拆分文本将由换行符完成。

4

1 回答 1

0

如果您可以ListBox控制您的表单(我们称之为listBox),您应该处理它的KeyDown事件。

这是示例:

private void listBox_KeyDown(object sender, KeyEventArgs e)
{
    // Check that the button pressed is V, that Control is also pressed and that the clipboard contains text.
    if ((e.KeyCode == Keys.V) && e.Control && Clipboard.ContainsText())
    {
        // Get text from clipboard and separate it.
        string text = Clipboard.GetText();
        string[] textLines = text.Split(
            new string[] { Environment.NewLine }, 
            StringSplitOptions.RemoveEmptyEntries); // or don't

        // Add lines to listBox items.
        listBox.Items.AddRange(textLines);

        // Mark event as handled.
        e.Handled = true;
    }
}
于 2012-09-03T12:32:28.490 回答