Copy/Paste a Text using C#
Submitted by donbermoy on Monday, May 26, 2014 - 02:04.
In this tutorial, we will create a program that has a copy and paste functions of using the ClipBoard class of C#.
Now, let's start this tutorial!
1. Let's start with creating a Windows Form Application in C# for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application.
2. Next, insert two Buttons for the Copy and Paste Functions named btnCopy labeled it as "Copy", and btnPaste labeled it as "Paste". Add also two TextBox named txtInput for inputting text to be copied and txtOutput for displaying the copied text. You must design your interface like this:
3. For btnCopy as Copy button, put this code below.
We called the Clipboard class in C# which provides methods to place data from the system Clipboard that we have set the text value of txtInput. SetText method from the clipboard class means to have the clipboard set a text out of the string inputted in our txtInput. Then it will prompt the user that the text was copied.
4. For btnPaste as Paste button, put this code below.
We used again the Clipboard class together with the GetText method to have a paste function that txtOutput textbox can now contain the text copied from the clipboard.
Full source code of this tutorial:
Output:
Hope this helps! :)
Best Regards,
Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]
Add and Follow me on Facebook: https://www.facebook.com/donzzsky
Visit and like my page on Facebook at: https://www.facebook.com/BermzISware
- Clipboard.SetText(txtInput.Text);
- MessageBox.Show("Text copied","Copy/Paste in C#", MessageBoxButtons.OK, MessageBoxIcon.Information);
- txtOutput.Text = Clipboard.GetText();
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- namespace Copy_and_Paste_Text_in_the_ClipBoard_using_CSharp
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- Clipboard.SetText(txtInput.Text);
- MessageBox.Show("Text copied","Copy/Paste in C#", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- private void button2_Click(object sender, EventArgs e)
- {
- txtOutput.Text = Clipboard.GetText();
- }
- }
- }
Add new comment
- 1390 views