[英]Retrieve text from a textbox to place in another textbox on same windows form
[英]How to Copy and Paste from One TextBox to another TextBox on the Same Windows Form
當用戶單擊按鈕時,我試圖將值從一個文本框復制到另一個文本框。 這似乎是一個簡單的解決方案,但是由於某些原因,當我單擊表單上的coppyButton1時,uid1(TextBox1)中的值未復制到uid2(TextBox2)中。 希望得到反饋。
碼:
private void copyButton1_Click(object sender, EventArgs e)
{
uid2.Text = uid1.Text;
}
您可以非常輕松地將數據關聯到剪貼板:
Clipboard.SetText(txtCopyText.Text);
那將使用文本框的值,然后存儲到剪貼板。
protected void btnCopy_Click(object sender, EventArgs e)
{
// You would want to validate the contents of the textbox before copying.
if(!string.IsNullOrEmpty(txtCopy.Text))
Clipboard.SetText(txtCopy.Text);
}
如果您只是想將值從一個字段強制轉換為另一字段,那么上面的代碼將強制設置該值。 但是要應用到剪貼板上進行復制和粘貼,您需要執行上述操作。
代碼可能不起作用的唯一原因是,如果您沒有實例化文本框,或者這些字段位於與btnCopy
另一種形式上。 或者,您制表並允許智能感知來反轉您復制的數據,即一對一。 您的代碼:
ui2.Text = ui1.Text;
您以為您是從ui1.Text
復制的ui1.Text
嗎?
更新資料
要從剪貼板獲取數據,請執行以下操作:
if(Clipboard.ContainsText(TextDataFormat.Text))
txtPaste.Text = Clipboard.GetText(TextDataFormat.Text);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.