Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

6
  • this is not a general solutions as it works only for intergers. I had to implement such thing recently and i ended up with try-parsing resulting string to number and allowing input only if parsing succeeded Commented Jan 4, 2012 at 15:03
  • 1
    This may not work when multiple methods handle KeyPress events from the same text box. One event might set e.Handled to true, and then another could set it back to false. In general, it's better to use if (...) e.Handled = true; Commented May 27, 2014 at 16:55
  • 2
    You can disable ShortcutsEnabled property to prevent copy paste by keyboard or menu Commented Dec 11, 2014 at 13:25
  • 3
    Eh. A TextChanged event that goes over it with a regex can fix copy-paste ;) Commented Aug 10, 2015 at 22:34
  • 1st: The one liner CAN prevent copy/paste if you fix the bug in it (&& ! must be a ||). 2nd: You can fix it, so that it prevents only paste and yet allows copy/cut/select all. Commented Mar 20, 2020 at 9:18