473,414 Members | 1,577 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Overriding ProcessKeyPreview, Enter WM_KEYDOWN...

Hi,

I have 3 questions regarding the code below:

1) Why can't I trap the KEYDOWN while I can trap KEYUP?
2) Is it correct that I use Return True within the IF-Statement? (I've
already read the documentation but it is rather hard to understand so please
don't refer to it :)
3) Many examples in the newsgroups use Return MyBase.ProcessKeyPreview(m) as
the last code line while I have used Return MyBase.ProcessKeyEventArgs(m)
according to the MSDN, is this correct or is it a typo in MSDN? (watch for
line breaks in the url)

ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemwindowsformscontrolclasspr
ocesskeypreviewtopic.htm

I aslo wonder if my code is correct as to subtitude Enter key with Tab in a
specifik column of the a datagrid. It seems to function well when trapping
the WM_KEYUP but to get the desired effect I need
to trap the WM_KEYDOWN, and doesn't work!

Protected Overrides Function ProcessKeyPreview(ByRef m As
System.Windows.Forms.Message) As Boolean
Dim keyCode As Keys = CType(m.WParam.ToInt32, Keys) And Keys.KeyCode
Const WM_KEYDOWN As Integer = &H100
Const WM_KEYUP As Integer = &H101

If m.Msg = WM_KEYDOWN AndAlso keyCode = Keys.Enter _
AndAlso DataGridColumnStyle1.TextBox Is ActiveControl _
AndAlso DataGrid1.DataSource.GetType Is GetType(DataView) Then
SendKeys.Send("{Tab}")
Return True
End If
Return MyBase.ProcessKeyEventArgs(m)
End Function

--
Thanks in advance
Ali Eghtebas Sweden
Jul 19 '05 #1
3 4130
> 1) Why can't I trap the KEYDOWN while I can trap KEYUP?
??? Whats happening when you try to trap keydown?
2) Is it correct that I use Return True within the IF-Statement? (I've
already read the documentation but it is rather hard to understand so please don't refer to it :)
The return value is dependant on what the function does. If you process the
key return true, if you don't, return false.
3) Many examples in the newsgroups use Return MyBase.ProcessKeyPreview(m) as the last code line while I have used Return MyBase.ProcessKeyEventArgs(m)
according to the MSDN, is this correct or is it a typo in MSDN? (watch for
line breaks in the url)
I can't see where ProcessKeyEventArgs appears anywhere, but it's wrong
anyway. It should be:

Return MyBase.ProcessKeyPreview(m)

This is because you are overriding the function, and so you are now letting
the original function process the key. The original function, that you
overrode, is not called, because you have overridden it, so calling that
line will call the original function and return the value that the original
function would have done.

(I hope that make sense)

Also, just FYI, Theres a better way to select the next control in a form,
other than sending the 'Tab' key (if you've seen my post about simulating
keystrokes earlier you'll realise I think it's bad). If you look at the
Me.SelectNextControl method, you can select the next control, based on the
ActiveControl:

Me.SelectNextControl(Me.ActiveControl, True)

More information on this:
<WatchForWrapping>
ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemwindowsformscontrolclassse
lectnextcontroltopic.htm
</WatchForWrapping>

--
Happy to help,
-- Tom Spink
(th**********@ntlworld.com)

" There's no place like 127.0.0.1 "

Please respond to the newsgroup,
so all can benefit.
One Day,
"Ali Eghtebas" <al***@home.se> wrote in message
news:eO**************@tk2msftngp13.phx.gbl... Hi,

I have 3 questions regarding the code below:

1) Why can't I trap the KEYDOWN while I can trap KEYUP?
2) Is it correct that I use Return True within the IF-Statement? (I've
already read the documentation but it is rather hard to understand so please don't refer to it :)
3) Many examples in the newsgroups use Return MyBase.ProcessKeyPreview(m) as the last code line while I have used Return MyBase.ProcessKeyEventArgs(m)
according to the MSDN, is this correct or is it a typo in MSDN? (watch for
line breaks in the url)

ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemwindowsformscontrolclasspr ocesskeypreviewtopic.htm

I aslo wonder if my code is correct as to subtitude Enter key with Tab in a specifik column of the a datagrid. It seems to function well when trapping
the WM_KEYUP but to get the desired effect I need
to trap the WM_KEYDOWN, and doesn't work!

Protected Overrides Function ProcessKeyPreview(ByRef m As
System.Windows.Forms.Message) As Boolean
Dim keyCode As Keys = CType(m.WParam.ToInt32, Keys) And Keys.KeyCode Const WM_KEYDOWN As Integer = &H100
Const WM_KEYUP As Integer = &H101

If m.Msg = WM_KEYDOWN AndAlso keyCode = Keys.Enter _
AndAlso DataGridColumnStyle1.TextBox Is ActiveControl _
AndAlso DataGrid1.DataSource.GetType Is GetType(DataView) Then
SendKeys.Send("{Tab}")
Return True
End If
Return MyBase.ProcessKeyEventArgs(m)
End Function

--
Thanks in advance
Ali Eghtebas Sweden

Jul 19 '05 #2
The issue was solved by overriding ProcessCmdKey instead ;)
This might be an issue in .Net Framework 1.1.
--
Regards
Ali Eghtebas Sweden

"Ali Eghtebas" <al***@home.se> wrote in message
news:e6**************@TK2MSFTNGP10.phx.gbl...
1) Why can't I trap the KEYDOWN while I can trap KEYUP? ??? Whats happening when you try to trap keydown?


Actually what happens is that nothing happens since the execution never

gets into the If-Statement where I want to replace the Enter key with Tab key.
NOTE that I use .Net Framework 1.1.
This must be a bug or something since according to the documentation I
should be able to trap both KEYDOWN and KEYUP, which seems to only apply on KEYUP in this case.
To be more specifik m.Msg = WM_KEYUP returns True while m.Msg = WM_KEYDOWN
returns FALSE!
3) Many examples in the newsgroups use Return MyBase.ProcessKeyPreview(m)
as
the last code line while I have used Return MyBase.ProcessKeyEventArgs(m) according to the MSDN, is this correct or is it a typo in MSDN? (watch for line breaks in the url)
I can't see where ProcessKeyEventArgs appears anywhere, but it's wrong
anyway. It should be:

Return MyBase.ProcessKeyPreview(m)

This is because you are overriding the function, and so you are now

letting
the original function process the key. The original function, that you
overrode, is not called, because you have overridden it, so calling that
line will call the original function and return the value that the

original
function would have done.

(I hope that make sense)

This makes sence to me and that is also what I thought but the only
confusing thing about it is MSDN:
"Notes to Inheritors: When overriding the ProcessKeyPreview method in a
derived class, a control should return true to indicate that it has
processed the key. For keys that are not processed by the control, the
result of calling the base class's ProcessKeyEventArgs method should be
returned."
Also, just FYI, Theres a better way to select the next control in a form, other than sending the 'Tab' key (if you've seen my post about simulating keystrokes earlier you'll realise I think it's bad). If you look at the
Me.SelectNextControl method, you can select the next control, based on the ActiveControl:

Me.SelectNextControl(Me.ActiveControl, True)

More information on this:
<WatchForWrapping>

ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemwindowsformscontrolclassse
lectnextcontroltopic.htm
</WatchForWrapping>


The reason I want to replace the Enter Key with Tab is not that I want

give the focus to the next control on my form. I do this since in the last row in a DataGrid when editing a Cell the Enter Key doesn't assign the new value to the Cell since this is the last row and hence there is no next row to move
the focus on. I simply send Replace the Enter with Tab there to make the
change in the Cell and trigger other code within
CurrentCellChanged.
One Day,
"Ali Eghtebas" <al***@home.se> wrote in message
news:eO**************@tk2msftngp13.phx.gbl...
Hi,

I have 3 questions regarding the code below:

1) Why can't I trap the KEYDOWN while I can trap KEYUP?
2) Is it correct that I use Return True within the IF-Statement? (I've
already read the documentation but it is rather hard to understand so

please
don't refer to it :)
3) Many examples in the newsgroups use Return

MyBase.ProcessKeyPreview(m)
as
the last code line while I have used Return

MyBase.ProcessKeyEventArgs(m) according to the MSDN, is this correct or is it a typo in MSDN? (watch for line breaks in the url)

ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemwindowsformscontrolclasspr
ocesskeypreviewtopic.htm

I aslo wonder if my code is correct as to subtitude Enter key with Tab in
a
specifik column of the a datagrid. It seems to function well when

trapping the WM_KEYUP but to get the desired effect I need
to trap the WM_KEYDOWN, and doesn't work!

Protected Overrides Function ProcessKeyPreview(ByRef m As
System.Windows.Forms.Message) As Boolean
Dim keyCode As Keys = CType(m.WParam.ToInt32, Keys) And

Keys.KeyCode
Const WM_KEYDOWN As Integer = &H100
Const WM_KEYUP As Integer = &H101

If m.Msg = WM_KEYDOWN AndAlso keyCode = Keys.Enter _
AndAlso DataGridColumnStyle1.TextBox Is ActiveControl _
AndAlso DataGrid1.DataSource.GetType Is GetType(DataView) Then
SendKeys.Send("{Tab}")
Return True
End If
Return MyBase.ProcessKeyEventArgs(m)
End Function

--
Thanks in advance
Ali Eghtebas Sweden


Jul 19 '05 #3
Glad you've got it sorted!!
"Notes to Inheritors: When overriding the ProcessKeyPreview method in a
derived class, a control should return true to indicate that it has
processed the key. For keys that are not processed by the control, the
result of calling the base class's ProcessKeyEventArgs method should be
returned."
Technically this is correct, though I see why you might find it confusing.
It's all about how the new events in VB are delegate methods. It simply
means call the base class' version of the method to make sure it gets
notified of the message.

--
Happy to help,
-- Tom Spink
(th**********@ntlworld.com)

" There's no place like 127.0.0.1 "

Please respond to the newsgroup,
so all can benefit.
One Day,
"Ali Eghtebas" <al***@home.se> wrote in message
news:#m**************@TK2MSFTNGP12.phx.gbl...
The issue was solved by overriding ProcessCmdKey instead ;)
This might be an issue in .Net Framework 1.1.
--
Regards
Ali Eghtebas Sweden

"Ali Eghtebas" <al***@home.se> wrote in message
news:e6**************@TK2MSFTNGP10.phx.gbl... > 1) Why can't I trap the KEYDOWN while I can trap KEYUP?
??? Whats happening when you try to trap keydown?
Actually what happens is that nothing happens since the execution never

gets
into the If-Statement where I want to replace the Enter key with Tab key.
NOTE that I use .Net Framework 1.1.
This must be a bug or something since according to the documentation I
should be able to trap both KEYDOWN and KEYUP, which seems to only apply

on
KEYUP in this case.
To be more specifik m.Msg = WM_KEYUP returns True while m.Msg = WM_KEYDOWN returns FALSE!
> 3) Many examples in the newsgroups use Return

MyBase.ProcessKeyPreview(m)
as
> the last code line while I have used Return

MyBase.ProcessKeyEventArgs(m)
> according to the MSDN, is this correct or is it a typo in MSDN? (watch
for
> line breaks in the url)

I can't see where ProcessKeyEventArgs appears anywhere, but it's wrong
anyway. It should be:

Return MyBase.ProcessKeyPreview(m)

This is because you are overriding the function, and so you are now

letting
the original function process the key. The original function, that you
overrode, is not called, because you have overridden it, so calling
that line will call the original function and return the value that the

original
function would have done.

(I hope that make sense)

This makes sence to me and that is also what I thought but the only
confusing thing about it is MSDN:
"Notes to Inheritors: When overriding the ProcessKeyPreview method in a
derived class, a control should return true to indicate that it has
processed the key. For keys that are not processed by the control, the
result of calling the base class's ProcessKeyEventArgs method should be
returned."
Also, just FYI, Theres a better way to select the next control in a

form, other than sending the 'Tab' key (if you've seen my post about simulating keystrokes earlier you'll realise I think it's bad). If you look at the Me.SelectNextControl method, you can select the next control, based on the ActiveControl:

Me.SelectNextControl(Me.ActiveControl, True)

More information on this:
<WatchForWrapping>

ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemwindowsformscontrolclassse
lectnextcontroltopic.htm
</WatchForWrapping>


The reason I want to replace the Enter Key with Tab is not that I want

give
the focus to the next control on my form. I do this since in the last

row in
a DataGrid when editing a Cell the Enter Key doesn't assign the new
value to
the Cell since this is the last row and hence there is no next row to

move the focus on. I simply send Replace the Enter with Tab there to make the
change in the Cell and trigger other code within
CurrentCellChanged.
One Day,
"Ali Eghtebas" <al***@home.se> wrote in message
news:eO**************@tk2msftngp13.phx.gbl...
> Hi,
>
> I have 3 questions regarding the code below:
>
> 1) Why can't I trap the KEYDOWN while I can trap KEYUP?
> 2) Is it correct that I use Return True within the IF-Statement? (I've > already read the documentation but it is rather hard to understand so please
> don't refer to it :)
> 3) Many examples in the newsgroups use Return

MyBase.ProcessKeyPreview(m)
as
> the last code line while I have used Return

MyBase.ProcessKeyEventArgs(m)
> according to the MSDN, is this correct or is it a typo in MSDN? (watch
for
> line breaks in the url)
>
>

ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemwindowsformscontrolclasspr > ocesskeypreviewtopic.htm
>
> I aslo wonder if my code is correct as to subtitude Enter key with Tab
in
a
> specifik column of the a datagrid. It seems to function well when

trapping
> the WM_KEYUP but to get the desired effect I need
> to trap the WM_KEYDOWN, and doesn't work!
>
> Protected Overrides Function ProcessKeyPreview(ByRef m As
> System.Windows.Forms.Message) As Boolean
> Dim keyCode As Keys = CType(m.WParam.ToInt32, Keys) And
Keys.KeyCode
> Const WM_KEYDOWN As Integer = &H100
> Const WM_KEYUP As Integer = &H101
>
> If m.Msg = WM_KEYDOWN AndAlso keyCode = Keys.Enter _
> AndAlso DataGridColumnStyle1.TextBox Is ActiveControl _
> AndAlso DataGrid1.DataSource.GetType Is GetType(DataView)

Then > SendKeys.Send("{Tab}")
> Return True
> End If
> Return MyBase.ProcessKeyEventArgs(m)
> End Function
>
> --
> Thanks in advance
> Ali Eghtebas Sweden



Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ali Eghtebas | last post by:
Hi, I have 3 questions regarding the code below: 1) Why can't I trap the KEYDOWN while I can trap KEYUP? 2) Is it correct that I use Return True within the IF-Statement? (I've already read...
1
by: Susanne Christe | last post by:
Hi Folks, I'm trying to override protected ProcessCmdKey function in writing my a custom MyDataGrid. I get the Keys I want, but it takes no effect to MyDataGrid, if I try for example execute...
1
by: Diogo Alves - Software Developer | last post by:
I'm tring to override the combination all the combination like this one Ctrl + Shift + . I just can't catch it... I have already done this for ctrl + or Shift + but for both at same timeI can't...
5
by: Niks | last post by:
I have a list control. On doubleclick of list item, a new dlg opens. Now, i want to handle 'OnEnter Key'also. i.e. When a list item is selected & Enter key is pressed, the new dlg shd open I tried...
4
by: jibran | last post by:
Hello. I have wrapped the DataGrid control with my own class (SmartDataGrid) adding some necessary functionality. My current webform has 2 SmartDataGrids. The first is populated by selected...
3
by: Melson | last post by:
hi can anyone help me how can i capture ENTER keystroke when the cell in datagrid is in editing mode. I'm now creating a data entry form with primary key in header and details in datagrid. So...
4
by: zacks | last post by:
Is there any way to distinguish the keypad Enter key from the standard Return key?
0
by: Raj | last post by:
Windows application C#: I have a datagrid, in the 2nd column the user has to press the enter key between the contents and once entered the data from the cursor point till end should move to next...
5
by: Peted | last post by:
Hello, i am lookinf for the best way to trap any alphanumeric keypress in all multi key combminations and execute some code For example , i have a form visible using the form.showdialog...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.