Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Need KeyPreview on userControl

1,433 views
Skip to first unread message

Ludovic Lemarinel

unread,
May 20, 2003, 10:55:24 AM5/20/03
to
Hi,

I need to use Keypreview on my Usercontrol, in order to use KeyDown Event.

This property works great on Forms, but is not implemented in UserControls
??

Is there a way to use it anyway ?

Thanks.


Witold Jaworski

unread,
May 20, 2003, 11:12:39 AM5/20/03
to
Override the protected ProcessKeyPreview function,
if your control is hosted by .NET Forms or OLE Container.
If not, you can also override the protected ProcessCmdKey
function - it is called before ProcessKeyPreview, for most
keystrokes.

Witold

>.
>

Ludovic Lemarinel

unread,
May 20, 2003, 11:25:39 AM5/20/03
to
Thanks for your answer.
As I'm new to VB.NET, I don't even know how to do what you are telling me ;)

could you provide a litlle example please ?

Thanks a lot...


"Witold Jaworski" <witold....@tadmar.com.pl> a écrit dans le message de
news: 001e01c31ee2$4112a910$a301...@phx.gbl...

Witold Jaworski

unread,
May 20, 2003, 12:19:07 PM5/20/03
to
'An User Control, which overrides the protected function
'to catch the keyboard messages (events):

Public Class UserControl1
Inherits System.Windows.Forms.UserControl
Private Const WM_KEYDOWN = &H100

#Region " Windows Form Designer generated code "
...
#End Region

Protected Overrides Function ProcessKeyPreview(ByRef m
As System.Windows.Forms.Message) As Boolean
Dim Processed As Boolean = False 'Set to true, if
we have "consumed" this keystroke
Select Case m.Msg 'This Select can be longer -
it's an example
Case WM_KEYDOWN
Select Case m.WParam.ToInt32 'WParam it is
a virtual key code, in this case
Case Keys.Escape 'An Escape key?
'Do something here
Processed = True
Case Keys.Down 'A Down Arrow?
'Do something here
Processed = True
'Case ...
End Select
End Select
If Not Processed Then Processed =
MyBase.ProcessKeyPreview(m) 'When we did nothing -
delegate to base class
ProcessKeyPreview = Processed
End Function
End Class

Witold

>.
>

Ludovic Lemarinel

unread,
May 21, 2003, 2:52:59 AM5/21/03
to
thanks A LOT !!!

It works, and it helped a lot !!!!


"Witold Jaworski" <witold....@tadmar.com.pl> a écrit dans le message de

news: 002d01c31eeb$898d42f0$a401...@phx.gbl...

0 new messages