A way to prevent Paste (or verify it before allowing it) is to
override ProcessCmdKey and check for Shift+Ins or Ctrl+V
(standard paste shortcuts)
Like this:
Protected Overrides Function ProcessCmdKey(ByRef msg As
System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As
Boolean
' We need to handle the paste command to prevent illegal characters
If keyData = (Keys.Shift Or Keys.Insert) OrElse keyData = (Keys.Control
Or Keys.V) Then
Dim data As IDataObject = Clipboard.GetDataObject()
If data Is Nothing Then
Return MyBase.ProcessCmdKey(msg, keyData)
Else
Dim text As String = CStr(data.GetData(DataFormats.StringFormat,
True))
If text = String.Empty Then
Return MyBase.ProcessCmdKey(msg, keyData)
Else
' TODO: Verify that the text only contains valid characters
' and return True if it doesn't. Otherwise call the base
class
End If
End If
Else
Return MyBase.ProcessCmdKey(msg, keyData)
End If
End Function
I don't see a need to limit cut and copy since those commands doesn't put
anything into the textbox.
For normal keyboard typing, KeyPress is the way to go.
/claes
"Altramagnus" <al*********@hotmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
I have searched throught the newsgroups
on how to restrict entry in textboxes, for example,
I only want the textbox to only accept numberic.
The standard answer is to use the KeyPress event.
However, this do not address the paste event
User is still able to copy and paste any characters.
How do I create a textbox so that regardless what I do ( including cut
and paste ),
the result must only contain certain prefdefined chars.
Thanks.