473,386 Members | 1,798 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,386 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 7110
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 4.0 transitional//en"> <html> <head>...
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 base for all the classes. After that i am inheriting...
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 is on (and must stay like this) and I'm...
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 trapping Webform keydown event and keyup event is...
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. Thanks -ZS
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 what is wrong with this code. I been fighting with...
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 has nothing on it and this code underneath: ...
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 automaticly, and then after the 2 next digits another "/"...
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 ctlLabelTextbox : UserControl { .... } The class that I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.