我以winform为例,设计两个textbox控件,分别命名为:textbox1和textbox2,设计两个button控件,分别命名:为btn_send和btn_get。
代码如下:
<span style="font-family:Microsoft YaHei;font-size:14px;"> private void btn_send_Click(object sender, EventArgs e)
{
try
{
Clipboard.SetText(textBox1.Text);
MessageBox.Show("已成功将文本框内容复制到剪贴板!");
}
catch (Exception)
{
MessageBox.Show("Error!");
}
}
private void btn_get_Click(object sender, EventArgs e)
{
string txt2 = textBox2.Text;
try
{
IDataObject iData = Clipboard.GetDataObject();
if (iData.GetDataPresent(DataFormats.Text))
{
//MessageBox.Show((string)iData.GetData(DataFormats.Text));
textBox2.Text = (string)iData.GetData(DataFormats.UnicodeText);
}
else
{
MessageBox.Show("目前剪贴板中数据不可转换为文本", "错误");
}
}
catch (Exception)
{
MessageBox.Show("Error");
}
}</span>