复制:
private void button1_Click(object sender, System.EventArgs e) {
// Takes the selected text from a text box and puts it on the clipboard.
if(textBox1.SelectedText != ”")
Clipboard.SetDataObject(textBox1.SelectedText);
}
粘贴:
private void button2_Click(object sender, System.EventArgs e) {
// Declares an IDataObject to hold the data returned from the clipboard.
// Retrieves the data from the clipboard.
IDataObject iData = Clipboard.GetDataObject();
// Determines whether the data is in a format you can use.
if(iData.GetDataPresent(DataFormats.Text)) {
// Yes it is, so display it in a text box.
textBox2.Text = (String)iData.GetData(DataFormats.Text);
}
}
主要通过调用Clipborad的API完成。
本文详细介绍了如何在Windows应用程序中利用C#语言实现文本的复制与粘贴功能,通过调用Clipboard类的API,实现文本在不同控件之间的便捷传递。包括如何从文本框获取选定文本并复制到剪贴板,以及如何从剪贴板读取数据并显示到另一个文本框中。

2336

被折叠的 条评论
为什么被折叠?



