Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can you prevent pasting into a TextBox ?

Status
Not open for further replies.

TerDavis

Technical User
Sep 18, 2002
36
US
Hi,

I have a textbox that i don't want users to be able to use the Ctrl-V (Paste) feature to enter information.

So, how do i prevent users from pasting text into a text box ?

Thanks,
Ter

 
Capture the Keydown event then check if V is being pressed with the Control modifier

txtBox1.KeyDown += blahblah

private void txtBox1_KeyDown(object sender, KeyEventArgs e)
{
if ((e.KeyData == Keys.V) && (e.Modifiers.Control))
{
e.Handled = true; //Cancel the keypress
}
}
 
You might have to trap the right click too? Just a thought.
 
Yes, you have to "trap" the mouse also.

Here's how you can do that:

Code:
ContextMenu aDummyCtxMenu = new ContextMenu();

aTextBox1.ContextMenu = aDummyCtxMenu;
aTextBox2.ContextMenu = aDummyCtxMenu;
aTextBox3.ContextMenu = aDummyCtxMenu;

(repeat for all the text boxes you need)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top