Inherit it and override ProcessCmdKey. The exact code depends on if you want
to abort the pasting if it contains invalid characters or if you want to
strip them out and paste the rest. The following does the former (i.e.
aborts the paste if it contains any invalid character):
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal
keyData As Keys) As Boolean
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 String.IsNullOrEmpty(text) Then
Return MyBase.ProcessCmdKey(msg, keyData)
Else
For Each ch As Char In text.ToCharArray
If Not IsValidChar(ch) Then
Return True
End If
Next
Return MyBase.ProcessCmdKey(msg, keyData)
End If
End If
Else
Return MyBase.ProcessCmdKey(msg, keyData)
End If
End Function
Private Function IsValidChar(ByVal ch As Char) As Boolean
'TODO: Return True or False depending on the validity of the character
End Function
/claes
"Johnny Jörgensen" <jo**@altcom.sewrote in message
news:03**********************************@microsof t.com...
I've got a textbox where I filter the input in the KeyPress event to allow
only certain characters.
However, If the user cuts and pastes a text into the textbox, all text is
entered - not only the characters allowed. That doesn't surprise me, but
what is the best way of avoiding that and still only allowing certain
characters even when pasting???
TIA,
Johnny J.