Copy/Paste a Text using C#

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: design 3. For btnCopy as Copy button, put this code below.
  1. Clipboard.SetText(txtInput.Text);
  2. MessageBox.Show("Text copied","Copy/Paste in C#", MessageBoxButtons.OK, MessageBoxIcon.Information);
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.
  1.  txtOutput.Text = Clipboard.GetText();
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:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8.  
  9. namespace Copy_and_Paste_Text_in_the_ClipBoard_using_CSharp
  10. {
  11.     public partial class Form1 : Form
  12.     {
  13.         public Form1()
  14.         {
  15.             InitializeComponent();
  16.         }
  17.  
  18.         private void button1_Click(object sender, EventArgs e)
  19.         {
  20.            
  21.                 Clipboard.SetText(txtInput.Text);
  22.                 MessageBox.Show("Text copied","Copy/Paste in C#", MessageBoxButtons.OK, MessageBoxIcon.Information);
  23.         }
  24.  
  25.         private void button2_Click(object sender, EventArgs e)
  26.         {
  27.          
  28.                 txtOutput.Text = Clipboard.GetText();
  29.         }
  30.     }
  31. }
Output: output 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

Add new comment