Click here to Skip to main content
13,835,194 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have a textbox, and the user should not be able to paste any data in it.

How can I achieve that?

Thanks in advance
Updated 4-Jun-10 3:12am
Johnny J. 4-Jun-10 8:12am
I assume that he SHOULD be able to write in it? Otherwise just set it to ReadOnly
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Why would you want to prevent paste, but allow typing?
If an App did that to me, then I would probably be quite annoyed.

If you really must, then derive a class from textbox, and override WndProc. You can then filter the WM_PASTE message, and disregard it. That should do it.
public class myTextBox : TextBox
    private const int WM_PASTE = 0x302;
    protected override void WndProc(ref Message m)
        if (m.Msg != WM_PASTE)
            base.WndProc(ref m);

I still think it's a bad idea though...
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Another method you may want to check is to set the property ShortcutsEnabled to false. Although that will disable both pasting and copying, and presumably any other short-cuts you can think of.

Otherwise as OriginalGriff said, I'd check for the WM_PASTE message. It's a lot more robust than checking the key presses and disabling the context menu.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web01 | 2.8.190114.1 | Last Updated 4 Jun 2010
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100