Click here to Skip to main content
15,879,047 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
Hi,

I hv to disable the Paste/Ctrl+v in C# textbox. Please help me?
Posted
Updated 14-Apr-22 0:15am
v2

This code will disable cut/copy and paste options( Ctrl+X,Ctrl+C and Ctrl+V) through keys.
C#
private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control == true)
            {
                MessageBox.Show("Cut/Copy and Paste Options are disabled");
            }
        }
 
Share this answer
 
Comments
LUCAS LUCAS.PRRSR 25-Feb-22 17:45pm    
doesnt work fully as the person just has to hold through control the message box and then press V
Hi dear try this code,

C#
private void textbox1_KeyPress(object sender, KeyPressEventArgs e)
       {
           if (e.KeyChar == 22)
               e.Handled = true;
         
        }
 
Share this answer
 
For disabling right click on your textbox:

Put a blank contextmenuestrip on your form and rename it for ex: mycontext, then set the Contextmenuestrip property of your text box in mycontext. User will not be able right click on your textbox.

For disabling copy and paste put this code on KeyDown Event handler of your textbox


C#
private void textBox1_KeyDown(object sender, KeyEventArgs e)
  {
   if (e.Modifiers == Keys.Control)
   {
     e.Handled = true;
    textBox1.SelectionLength = 0;
   }
  }
 
Share this answer
 
Comments
Sandeep Mewara 7-Jun-12 4:26am    
Good answer! 5!
Private Sub TxtPassword_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs)
           If e.Control And e.KeyCode = Key.V Then
               e.Handled = True
               e.SuppressKeyPress = True
           End If
           If e.Control And e.KeyCode = Key.C Then
               e.Handled = True
               e.SuppressKeyPress = True
           End If
       End Sub
 
Share this answer
 
Comments
Vipin_Arora 17-Apr-22 10:22am    
Code was needed in C# (not in VB.NET) and that too in 2012. However, appreciate your efforts :)

~ Happy Coding
Friend, please find the code for same also use the true block to generate your custom event.

C#
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Control && e.KeyValue == 86)
    {
        e.Handled = true;
        e.SuppressKeyPress = true;

    }
}



please vote if this solution is accepted or relevant for you.

Thanks,
Ambesha
 
Share this answer
 
Comments
Wajid Khaksar 15-Dec-16 21:34pm    
prompt is executed i.e copy/paste not allowed after pressing ctrl+v but data also paste in datagridview text field using your code. its not fruitful. kindly give a authentic solution for it
Vipin_Arora 19-Dec-16 5:11am    
You are little bit late..just 4 years

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900