简体   繁体   English

如何从一个文本框复制和粘贴到同一Windows窗体上的另一个文本框

[英]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.

相关问题 从文本框中检索文本以放置在同一Windows窗体上的另一个文本框中 - Retrieve text from a textbox to place in another textbox on same windows form 使用C#Windows Form以相同的形式将文本从一个文本框传递到另一个文本框 - Passing text from one textBox to another in a same form using C# Windows Form 如何在Windows应用程序中将数据从一种形式传递到另一种形式的文本框? - How to pass data from one form to another form textbox in windows application? 如何使用C#在Windows Phone的文本框中禁用复制/粘贴? - how to disable copy/paste in textbox in windows phone using c#? 如何使用文本框的值从一种形式到另一种形式? - How to use value of textbox from one form to another form? 如何将值从一个窗体传递到另一个窗体的私有TextBox? - How to pass a value from one Form to a private TextBox in another Form? 将文本框信息从一种形式调用到另一种形式 - Calling textbox information from one form to another 在 Windows 窗体中将一种形式的文本框值传递到另一种形式 - Passing Textbox Values one form to another form in Windows Forms 如何在禁用的TextBox上启用复制粘贴 - How to enable copy paste on disabled TextBox 将类似的列从一个表复制到另一个表并将文本从文本框中复制 - copy like columns from one table to another and text from textbox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM