[英]How to Copy and Paste from One TextBox to another TextBox on the Same Windows Form
I am trying to copy values from one textbox to another textbox when user clicks a button. 当用户单击按钮时,我试图将值从一个文本框复制到另一个文本框。 It seems to be a simple solution but for some reason when I click the coppyButton1 on the form, the value from uid1 (TextBox1) not getting copied into uid2(TextBox2). 这似乎是一个简单的解决方案,但是由于某些原因,当我单击表单上的coppyButton1时,uid1(TextBox1)中的值未复制到uid2(TextBox2)中。 Hoping for feedback. 希望得到反馈。
Code: 码:
private void copyButton1_Click(object sender, EventArgs e)
{
uid2.Text = uid1.Text;
}
You can associate data to the clipboard incredibly easy: 您可以非常轻松地将数据关联到剪贴板:
Clipboard.SetText(txtCopyText.Text);
That would take the value of the textbox, then store to the clipboard. 那将使用文本框的值,然后存储到剪贴板。
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);
}
If you simply want to force the value from one field to another, then the code you have above would force the value to be set. 如果您只是想将值从一个字段强制转换为另一字段,那么上面的代码将强制设置该值。 But to apply to the clipboard for copy and paste, you would do the above. 但是要应用到剪贴板上进行复制和粘贴,您需要执行上述操作。
The only reason that code might not work would be if you don't have the textbox instantiated, or those fields are on another form that deviates from your btnCopy
. 代码可能不起作用的唯一原因是,如果您没有实例化文本框,或者这些字段位于与btnCopy
另一种形式上。 Or you tabbed and allowed intellisense to reverse your copied data, ie one vs two. 或者,您制表并允许智能感知来反转您复制的数据,即一对一。 Your code: 您的代码:
ui2.Text = ui1.Text;
Is the field you thought you were copying from ui1.Text
? 您以为您是从ui1.Text
复制的ui1.Text
吗?
Update 更新资料
To get data from the clipboard, you would do the following: 要从剪贴板获取数据,请执行以下操作:
if(Clipboard.ContainsText(TextDataFormat.Text))
txtPaste.Text = Clipboard.GetText(TextDataFormat.Text);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.