Hello all. I don't know if this is the right newsgroup to post this message,
so sorry for any cross-posting.
I have a class inheriting from System.Windows.Forms.TextBox which contraints
user input to only numeric digits. It works well until user paste some text
from clipboard or by right clicking in the textbox's area and selecting
'paste'. My textbox need by someway intercepts the paste message (or do
anything like that). How can I make it?
Thank you in advance.
---
Celio C. J.
Gabriele G. Ponti - 27 Oct 2003 21:20 GMT
Hi,
Override the WndProc of the control, and handle the WM_PAINT message. When
handling the message you want to check if the data in the clipboard is valid
and allow normal processing, otherwise don't let the base control get the
message.
For example:
Public Class CustomTextBox
Inherits System.Windows.Forms.TextBox
Private Const WM_PASTE = &H302
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
' check for WM_PASTE message
If m.Msg = WM_PASTE Then
Dim i As IDataObject
Dim s As String = ""
' get data currently in the clipboard
i = Clipboard.GetDataObject()
' check for text
If i.GetDataPresent(DataFormats.Text) Then
s = CType(i.GetData(DataFormats.Text), String)
End If
' exit if invalid data in the clipboard
If IsInvalid(s) Then Return
End If
' default processing
MyBase.WndProc(m)
End Sub
Private Function IsInvalid(ByVal s As String) As Boolean
' put your logic here
Return (s = "can't paste this")
End Function
End Class
Regards,
Gabriele