Home All Groups Group Topic Archive Search About

How to prevent Pasting into textbox?

Author
7 Oct 2006 3:40 PM
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

Author
7 Oct 2006 3:45 PM
rowe_newsgroups
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 wrote:
Show quoteHide quote
> 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
Author
7 Oct 2006 3:51 PM
Tom Shelton
HKSHK wrote:
Show quoteHide quote
> 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....

--
Tom Shelton
Author
8 Oct 2006 9:56 AM
HKSHK
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
Author
8 Oct 2006 8:13 PM
Tom Shelton
HKSHK wrote:
Show quoteHide quote
> 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

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 Shelton
Author
9 Oct 2006 7:38 PM
Tom Shelton
Tom Shelton wrote:
Show quoteHide quote
> HKSHK wrote:
> > 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
>
> 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.

--
Tom Shelton
Author
9 Oct 2006 8:28 PM
HKSHK
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