Click here to Skip to main content
15,917,174 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Posted
Updated 4-Jun-10 2:12am
v3
Comments
Johnny J. 4-Jun-10 8:12am    
I assume that he SHOULD be able to write in it? Otherwise just set it to ReadOnly

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...
 
Share this answer
 
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.
 
Share this answer
 

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