1
當用戶粘貼到列表框中時,我希望剪貼板中的內容以單獨項目的形式排列。分割文本將由換行符完成。覆蓋粘貼操作列表框
當用戶粘貼到列表框中時,我希望剪貼板中的內容以單獨項目的形式排列。分割文本將由換行符完成。覆蓋粘貼操作列表框
如果您的表單上有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;
}
}