472,328 Members | 2,165 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

KeyUp + KeyDown Event Handler

I would like to handle the KeyUp & KeyDown events in the same event
handler but can't find how to determine which event was fired -

Private Sub ListBox1_KeyUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) _
Handles ListBox1.KeyUp, ListBox1.KeyDown

If e.KeyValue = Keys.PageDown Or e.KeyValue = Keys.PageUp Or e.KeyValue
= Keys.End Or e.KeyValue = Keys.Home Then

'If User "Pressed" key (KeyDown) then do something...
'If User "Released" key (KeyUp) then do something else...

End If

End Sub
Apart from duplicating the code in the individual KeyUp and KeyDown
Events, does anyone know how to determine which action triggered the
event handler in this case?

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
Feb 6 '06 #1
4 7008
ShaneO wrote:
I would like to handle the KeyUp & KeyDown events in the same event
handler but can't find how to determine which event was fired -

Private Sub ListBox1_KeyUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) _
Handles ListBox1.KeyUp, ListBox1.KeyDown

If e.KeyValue = Keys.PageDown Or e.KeyValue = Keys.PageUp Or e.KeyValue
= Keys.End Or e.KeyValue = Keys.Home Then

'If User "Pressed" key (KeyDown) then do something...
'If User "Released" key (KeyUp) then do something else...

End If

End Sub

Apart from duplicating the code in the individual KeyUp and KeyDown
Events, does anyone know how to determine which action triggered the
event handler in this case?


Have you tried writing a common method, then having the KeyUp and KeyDown
events call it? Something like this...

Protected Sub CommonKeyHandler( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs, _
ByVal SentBy As Byte)
(insert code here)
End Sub

Private Sub Button1_KeyDown( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs)
CommonKeyHandler(sender, e, 0)
End Sub

Private Sub Button1_KeyUp( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs)
CommonKeyHandler(sender, e, 1)
End Sub
--
Gregory Gadow
te******@serv.net
Feb 6 '06 #2
>
Have you tried writing a common method, then having the KeyUp and KeyDown
events call it? Something like this...

--
Gregory Gadow
te******@serv.net

Thanks for your reply Gregory.

I have thought about doing that, however that would require entries in 3
separate Event Handlers. (1 for KeyUp, 1 for KeyDown & 1 for the final
Event Handler).

I guess I really just like to have my code as compact as possible and by
having only one Handler to cover the two events it would help me in that
regard.

Maybe my desire isn't achievable with these events?

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
Feb 6 '06 #3
ShaneO wrote:
I would like to handle the KeyUp & KeyDown events in the same event
handler but can't find how to determine which event was fired -

Private Sub ListBox1_KeyUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) _
Handles ListBox1.KeyUp, ListBox1.KeyDown

If e.KeyValue = Keys.PageDown Or e.KeyValue = Keys.PageUp Or e.KeyValue
= Keys.End Or e.KeyValue = Keys.Home Then

'If User "Pressed" key (KeyDown) then do something...
'If User "Released" key (KeyUp) then do something else...

End If

End Sub
Apart from duplicating the code in the individual KeyUp and KeyDown
Events, does anyone know how to determine which action triggered the
event handler in this case?

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.


How are you duplicating code if you want and if statement seperating the
two.

You can call the same sub from both if you want the two to runsome of
the same code, but not all.

Chris
Feb 6 '06 #4
Chris wrote:

Apart from duplicating the code in the individual KeyUp and KeyDown
Events, does anyone know how to determine which action triggered the
event handler in this case?


How are you duplicating code if you want and if statement seperating the
two.

You can call the same sub from both if you want the two to runsome of
the same code, but not all.

Chris

Thanks Chris for your reply.

I suppose I should have written my example as -

If e.KeyValue = Keys.PageDown Or e.KeyValue = Keys.PageUp Or
e.KeyValue = Keys.End Or e.KeyValue = Keys.Home Then

'Do Some Stuff....
'Do More Stuff....
..
..
..
'If User "Pressed" key (KeyDown) then do something...
'If User "Released" key (KeyUp) then do something else...

End If

Please see my reply to Gregory Gadow regarding the option of creating a
common handler.

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
Feb 6 '06 #5

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

Similar topics

34
by: Andrew DeFaria | last post by:
I thought this would be fairly straight forward but apparently it's not. Given the following html file: <!DOCTYPE html PUBLIC "-//w3c//dtd html...
0
by: Ramnadh | last post by:
Hi, I am inherting the Panel call and making another class BasePanel By extending all the properties and including some other members as the...
1
by: Ori | last post by:
Hi Guys, Here is my problem, but maybe someone can help me with this. Background: 1. Using C#. 2. I'm having a form which the KeyPreview...
3
by: trint | last post by:
Ok, I have tried to do this with the System.Web.UI and can't find anything for the webform. It seems much easier for a Winform. Any help in...
2
by: ZS | last post by:
Hi, On a form , I'm trying to trap when a shift key is pressed. Can someone explain how the KeyUp,KeyDown and Key Press event works for Forms....
0
by: Gene Hubert | last post by:
I'm trying to catch the KeyUp event in textbox of a DataGrid. I'm picking up the keydown and keypress events ok, but not keyup. Can anyone see...
2
by: Adam J. Schaff | last post by:
I have recently noticed an unwanted behavior that I do not know how to get rid of. To Recreate Problem: Windows Forms App with 2 forms. Form 1...
0
by: Gidi | last post by:
Hi, I have a DataGrid cell which the user enters dates to it. i want that after the user has entered 2 digits (dd) a "/" will be entered...
2
by: Tony Johansson | last post by:
Hello! I have created a Control that consist of a label and a textbox.I have called this class ctlLabelTextbox. public partial class...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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.