问题
i need the selected text in my textBox to be copied in my clipboard by my MenuItemCopy() methode and then be able to paste it by MenuItemPaste().need some help with it. here is my codes:
private void goToToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void menuItem_Click(object sender, EventArgs e)
{
ToolStripMenuItem menuItem = (ToolStripMenuItem)sender;
switch (menuItem.Name.Replace("ToolStripMenuItem", ""))
{
case "copy":
MenuItemCopy();
break;
case "paste":
MenuItemPaste();
break;
}
}
private void MenuItemPaste()
{
}
private void MenuItemCopy()
{
}
i tried string str= Clipboard.GetText() but it did not work.what is the problem?
回答1:
You shuld use Clipboard
class (msdn) and SelectedText
property of TextBox
(msdn):
To copy data to the clipboard use SetText
method (msdn).
To get data from the clipboard use GetText
method (msdn).
Following code copy selected text from TextBox
to clipboard:
Clipboard.SetText(tbText.SelectedText);
回答2:
Clipboard.SetText("Hello, clipboard");
To copy the contents of a textbox:
Clipboard.SetText(txtClipboard.Text);
To get the content in Clipboard
string str= Clipboard.GetText()
回答3:
To set some sort of text in the clipboard:
Clipboard.SetText("Whatever you want");
So in yourcase
Clipboard.SetText(txtMyTextBox.Text);
To get it back, use
Clipboard.GetText();
So
txtMyTextBox.Text = Clipboard.GetText();
Documentation
来源:https://stackoverflow.com/questions/20302195/copy-text-in-clipboard