How to prevent Pasting into textbox?

  • Thread starter Thread starter HKSHK
  • Start date Start date
H

HKSHK

Hi,

I'm developing an application with VB.NET 2003 and I want to forbid
pasting into a textbox.

The problem is not the context menu, but the Ctrl+V shortcut.
How can I prevent that the user can paste anything into a textbox, even
if (maybe) another shortcut for Pasting has been defined?

Thanks in advance!

Beste Regards,

HKSHK
 
Ctrl+V can be can trapped using the keydown event. Not sure what you
could do about the "other shortcuts" though.

Thanks,

Seth Rowe
 
HKSHK said:
Hi,

I'm developing an application with VB.NET 2003 and I want to forbid
pasting into a textbox.

The problem is not the context menu, but the Ctrl+V shortcut.
How can I prevent that the user can paste anything into a textbox, even
if (maybe) another shortcut for Pasting has been defined?

Thanks in advance!

Beste Regards,

HKSHK

Well, you can override the textbox's wndproc method to capture WM_PASTE
messages. Basically, you would do something like:

Private Const WM_PASTE As Integer = &H302

Protected Override Sub WndProc (ByRef m As Message)
If m.Msg = WM_PASTE
m.Result = IntPtr.Zero
Else
MyBase.WndProc (m)
End If
End Sub

You might have to play with that a little, but that should be fairly
close....
 
Dear Mr. Shelton,
Well, you can override the textbox's wndproc method to capture WM_PASTE
messages. Basically, you would do something like:

Private Const WM_PASTE As Integer = &H302

Protected Override Sub WndProc (ByRef m As Message)
If m.Msg = WM_PASTE
m.Result = IntPtr.Zero
Else
MyBase.WndProc (m)
End If
End Sub

Thank you for the code. But do you know any way without having to create
a custom control? I would like to use it as a function which I can use
on any textbox.

Thanks again for your help!

Best Regards,

HKSHK
 
HKSHK said:
Dear Mr. Shelton,


Thank you for the code. But do you know any way without having to create
a custom control? I would like to use it as a function which I can use
on any textbox.

Thanks again for your help!

Best Regards,

HKSHK

Probably not with a function - but I am contemplating a couple of other
alternatives... Like using a class that implements IMessageFilter. Or
maybe something using NativeWindow. Let me play a little and get back
to you :)
 
Tom said:
Probably not with a function - but I am contemplating a couple of other
alternatives... Like using a class that implements IMessageFilter. Or
maybe something using NativeWindow. Let me play a little and get back
to you :)

Ok, System.Windows.Forms.NativeWindow is the way to go. They even show
an example of subclassing a form using the NativeWindow class. I will
try and throw together a simple example for you if that isn't
sufficeint.
 
Dear Mr. Shelton,
Ok, System.Windows.Forms.NativeWindow is the way to go. They even show
an example of subclassing a form using the NativeWindow class. I will
try and throw together a simple example for you if that isn't
sufficeint.

Thanks for the tip and the offer. I'll see what I can do by myself since
the example seems to be quite clear (Keep my fingers crossed... ;-) ).

Thanks again!

Best Regards,

HKSHK
 
Back
Top