2012-09-03 58 views

回答

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; 
    } 
}