粘贴板(Clipboard)可以用来暂存、传递数据,如从网页上拷贝数据到粘贴板上,通过粘贴板保存到另外的地方(Word、文本等)。
C#中如何使用粘贴板,可以参考如下代码:
private void button1_Click(object sender, System.EventArgs e)
{
if (textBox1.SelectedText != "")
Clipboard.SetDataObject(textBox1.SelectedText);
else
textBox2.Text = "在textBox1中没有选定的内容";
}
private void button2_Click(object sender, System.EventArgs e)
{
IDataObject iData = Clipboard.GetDataObject();
//检测数据是否是可以使用的格式,即文本格式
if (iData.GetDataPresent(DataFormats.Text))
{
//是可以使用的格式,就在textbox2中显示剪切板中的内容
textBox2.Text = (String)iData.GetData(DataFormats.Text);
}
else
{
//如果不是
textBox2.Text = "没有从剪切板中接收到数据!";
}
}