473,378 Members | 1,321 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,378 software developers and data experts.

.NET Kepresses

I am currently trying to teach myself .NET (specifically vb.NET). I have
been working with VBA in Access 97 for a few years now, and consider myself
to be quite proficient.

At present, I have an issue with the concept of detecting keypresses. I use
this frequently in Access 97, generally for giving the user an
keyboard-based alternative to clicking on command buttons.

For example, if I have a form where the user will be entering records, as
well as navigating through them, the user has the choice of either clicking
on the various command buttons for navigation and record saving, or pressing
the PgUp and PgDn keys for forward and backward navigation respectively, as
well as the F2 key to save a record. If the user presses CTRL-PgUp/Dn, they
will be able to move to the first or last record in the recordset.

I want to be able to provide this functionality in vb.NET, but can't seem to
find a way of doing it.

I've attached a sample of the code that I would generally use on an Access
form, which I hope will explain what I mean:

Thanks In Advance
Ian Henderson
Database Developer and .NET novice

Private Sub Form_KeyDown(Keycode As Integer, Shift As Integer)

intShiftDown = (Shift And acShiftMask) > 0
intCtrlDown = (Shift And acCtrlMask) > 0
intAltDown = (Shift And acAltMask) > 0

If intShiftDown Then
intShiftDown = 0
Keycode = 0
Exit Sub
End If

If intAltDown Then
intAltDown = 0
Keycode = 0
Exit Sub
End If

If intCtrlDown Then
Select Case Keycode
Case vbKeyQ
strMsg = "Are you sure you wish to quit the " &
DLookup("ShortName", "DatabaseName") & "?"
StrTitle = "Confirm Action"
IntStyle = vbYesNo + vbDefaultButton2
Response = MsgBox(strMsg, IntStyle, StrTitle)
If Response = vbYes Then DoCmd.Quit acQuitPrompt
Keycode = 0
Exit Sub
Case Else
Keycode = 0
Exit Sub
End Select
intCtrlDown = 0
Keycode = 0
Exit Sub
Else
Select Case Keycode
Case vbKeyW
WorkForms.Visible = True
Keycode = 0
Exit Sub
Case vbKeyL
LookupTables.Visible = True
Keycode = 0
Exit Sub
Case vbKeyR
Reports.Visible = True
Keycode = 0
Exit Sub
Case vbKey1
WorkOnBankAccountsBtn_Click
Keycode = 0
Exit Sub
Case vbKey2
WorkOnTelephoneAccountsBtn_Click
Keycode = 0
Exit Sub
Case Else
Keycode = 0
Exit Sub
End Select
End If

End Sub
Jul 21 '05 #1
7 4074
"Ian Henderson" <ho**@ihenderson.co.uk> wrote in
news:bj**********@titan.btinternet.com:
I am currently trying to teach myself .NET (specifically vb.NET). I
have been working with VBA in Access 97 for a few years now, and
consider myself to be quite proficient.

At present, I have an issue with the concept of detecting keypresses.
I use this frequently in Access 97, generally for giving the user an
keyboard-based alternative to clicking on command buttons.

For example, if I have a form where the user will be entering records,
as well as navigating through them, the user has the choice of either
clicking on the various command buttons for navigation and record
saving, or pressing the PgUp and PgDn keys for forward and backward
navigation respectively, as well as the F2 key to save a record. If
the user presses CTRL-PgUp/Dn, they will be able to move to the first
or last record in the recordset.

I want to be able to provide this functionality in vb.NET, but can't
seem to find a way of doing it.

Forms and Controls still have KeyDown, KeyPress, and KeyUp events. In the
form designer highlight the form or control you want to work with, then in
the properties window there is a button that shows all the events (instead
of properties). Look through that list of events for the Key event(s) you
are interested in, and double click on it. It will create an event handler
and create the wire-up code needed in the win forms generated code section.
Then all you have to do is write the code you want to run when the event
fires.

--
Michael Lang, MCSD

Jul 21 '05 #2
Thanks Michael.

I've managed, in actual fact, to get around the problem using hidden menus,
which very handily allows me to (for example) remove/enhance the standard
ALT-F4 functionality. However, what it doesn't allow me to do is to assign
events to keys such as PgUp and PgDn, which I generally define as being the
keys to use for navigating back and forth through recordsets (seems kinda
logical, right).

There's probably something quite simple that I'm not doing, however, at the
moment I can't see it.

Any clues?
Ian Henderson

"Michael Lang" <ml@nospam.com> wrote in message
news:Xn********************************@207.46.248 .16...
"Ian Henderson" <ho**@ihenderson.co.uk> wrote in
news:bj**********@titan.btinternet.com:
I am currently trying to teach myself .NET (specifically vb.NET). I
have been working with VBA in Access 97 for a few years now, and
consider myself to be quite proficient.

At present, I have an issue with the concept of detecting keypresses.
I use this frequently in Access 97, generally for giving the user an
keyboard-based alternative to clicking on command buttons.

For example, if I have a form where the user will be entering records,
as well as navigating through them, the user has the choice of either
clicking on the various command buttons for navigation and record
saving, or pressing the PgUp and PgDn keys for forward and backward
navigation respectively, as well as the F2 key to save a record. If
the user presses CTRL-PgUp/Dn, they will be able to move to the first
or last record in the recordset.

I want to be able to provide this functionality in vb.NET, but can't
seem to find a way of doing it.

Forms and Controls still have KeyDown, KeyPress, and KeyUp events. In the
form designer highlight the form or control you want to work with, then in
the properties window there is a button that shows all the events (instead
of properties). Look through that list of events for the Key event(s) you
are interested in, and double click on it. It will create an event

handler and create the wire-up code needed in the win forms generated code section. Then all you have to do is write the code you want to run when the event
fires.

--
Michael Lang, MCSD

Jul 21 '05 #3
"Ian Henderson" <ho**@ihenderson.co.uk> wrote in
news:bj**********@sparta.btinternet.com:
Thanks Michael.

I've managed, in actual fact, to get around the problem using hidden
menus, which very handily allows me to (for example) remove/enhance
the standard ALT-F4 functionality. However, what it doesn't allow me
to do is to assign events to keys such as PgUp and PgDn, which I
generally define as being the keys to use for navigating back and
forth through recordsets (seems kinda logical, right).

There's probably something quite simple that I'm not doing, however,
at the moment I can't see it.

Any clues?


Ok, I'll explain again.
1) Create a Form
2) override the KeyDown method as follows:

protected override void OnKeyDown(KeyEventArgs e)
{
if (e.KeyCode == Keys.PageDown)
{ // handle PageDown key press
}
else if(e.KeyCode == Keys.PageUp)
{ //handle PageUp key press
}
else
{ //only raise event to subscribers if not
// one of the above keys
base.OnKeyDown (e);
}
}

(In VS 2003, when you type "protected override " intellisense pops up
with all the methods you could override. Pick one and it gets filled out
automatically)

3) done!

Alternatively you could subscribe to the event as I described the first
time.
1) go to forms designer view
2) go to properties window
3) press lightning bolt icon (events view)
4) scroll to KeyDown
5) double-click on KeyDown and the desiger automatically creates the
following event handler:

private void Form1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
}

6) add the same handler code I'n the first example
7) done!

--
Michael Lang, MCSD
See my .NET open source projects
http://sourceforge.net/projects/colcodegen (simple code generator)
http://sourceforge.net/projects/dbobjecter (database app code generator)
http://sourceforge.net/projects/genadonet ("generic" ADO.NET)

Jul 21 '05 #4
You might need also to check/set the value of KeyPreview property for your
form in addition to KeyDown. And difference in handling keys in KeyPress and
KeyDown

HTH
Alex
"Ian Henderson" <ho**@ihenderson.co.uk> wrote in message
news:bj**********@sparta.btinternet.com...
Thanks Michael.

I've managed, in actual fact, to get around the problem using hidden menus, which very handily allows me to (for example) remove/enhance the standard
ALT-F4 functionality. However, what it doesn't allow me to do is to assign events to keys such as PgUp and PgDn, which I generally define as being the keys to use for navigating back and forth through recordsets (seems kinda
logical, right).

There's probably something quite simple that I'm not doing, however, at the moment I can't see it.

Any clues?
Ian Henderson

"Michael Lang" <ml@nospam.com> wrote in message
news:Xn********************************@207.46.248 .16...
"Ian Henderson" <ho**@ihenderson.co.uk> wrote in
news:bj**********@titan.btinternet.com:
I am currently trying to teach myself .NET (specifically vb.NET). I
have been working with VBA in Access 97 for a few years now, and
consider myself to be quite proficient.

At present, I have an issue with the concept of detecting keypresses.
I use this frequently in Access 97, generally for giving the user an
keyboard-based alternative to clicking on command buttons.

For example, if I have a form where the user will be entering records,
as well as navigating through them, the user has the choice of either
clicking on the various command buttons for navigation and record
saving, or pressing the PgUp and PgDn keys for forward and backward
navigation respectively, as well as the F2 key to save a record. If
the user presses CTRL-PgUp/Dn, they will be able to move to the first
or last record in the recordset.

I want to be able to provide this functionality in vb.NET, but can't
seem to find a way of doing it.

Forms and Controls still have KeyDown, KeyPress, and KeyUp events. In the form designer highlight the form or control you want to work with, then in the properties window there is a button that shows all the events (instead of properties). Look through that list of events for the Key event(s) you are interested in, and double click on it. It will create an event

handler
and create the wire-up code needed in the win forms generated code

section.
Then all you have to do is write the code you want to run when the event
fires.

--
Michael Lang, MCSD


Jul 21 '05 #5
One more thing... If you have a control on the form, and you still want
the form to handle the event when the control has focus, set the
"KeyPreview" property of the form to "true".

--
Michael Lang, MCSD

Jul 21 '05 #6
Thanks again, you've been very helpful.

Incidentally, am I right in saying that the code you supplied in your
previous messages is relevant to VB2003? I'm using 2002, but anyway, I've
managed to get the code working. The only thing I want to know now is how
you detect that the user is pressing two keys at the same time. For
example, as stated previously, I use PgUp and PgDn for record navigation.
However, if the user holds down the CTRL key with either PgUp or PgDn,
they'll navigate to the first and last records respectively, which is useful
for a large dataset.

Sorry to be such a nuisance - I'm trying to teach myself VB.NET for work,
and it's a very slow process!!!

TIA

Ian Henderson

"Michael Lang" <ml@nospam.com> wrote in message
news:Xn********************************@207.46.248 .16...
"Ian Henderson" <ho**@ihenderson.co.uk> wrote in
news:bj**********@sparta.btinternet.com:
Thanks Michael.

I've managed, in actual fact, to get around the problem using hidden
menus, which very handily allows me to (for example) remove/enhance
the standard ALT-F4 functionality. However, what it doesn't allow me
to do is to assign events to keys such as PgUp and PgDn, which I
generally define as being the keys to use for navigating back and
forth through recordsets (seems kinda logical, right).

There's probably something quite simple that I'm not doing, however,
at the moment I can't see it.

Any clues?


Ok, I'll explain again.
1) Create a Form
2) override the KeyDown method as follows:

protected override void OnKeyDown(KeyEventArgs e)
{
if (e.KeyCode == Keys.PageDown)
{ // handle PageDown key press
}
else if(e.KeyCode == Keys.PageUp)
{ //handle PageUp key press
}
else
{ //only raise event to subscribers if not
// one of the above keys
base.OnKeyDown (e);
}
}

(In VS 2003, when you type "protected override " intellisense pops up
with all the methods you could override. Pick one and it gets filled out
automatically)

3) done!

Alternatively you could subscribe to the event as I described the first
time.
1) go to forms designer view
2) go to properties window
3) press lightning bolt icon (events view)
4) scroll to KeyDown
5) double-click on KeyDown and the desiger automatically creates the
following event handler:

private void Form1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
}

6) add the same handler code I'n the first example
7) done!

--
Michael Lang, MCSD
See my .NET open source projects
http://sourceforge.net/projects/colcodegen (simple code generator)
http://sourceforge.net/projects/dbobjecter (database app code generator)
http://sourceforge.net/projects/genadonet ("generic" ADO.NET)

Jul 21 '05 #7
"Ian Henderson" <ho**@ihenderson.co.uk> wrote in
news:bj**********@titan.btinternet.com:
Thanks again, you've been very helpful.

Incidentally, am I right in saying that the code you supplied in your
previous messages is relevant to VB2003? I'm using 2002, but anyway,
I've managed to get the code working. The only thing I want to know
now is how you detect that the user is pressing two keys at the same
time. For example, as stated previously, I use PgUp and PgDn for
record navigation. However, if the user holds down the CTRL key with
either PgUp or PgDn, they'll navigate to the first and last records
respectively, which is useful for a large dataset.

Sorry to be such a nuisance - I'm trying to teach myself VB.NET for
work, and it's a very slow process!!!

TIA

Ian Henderson

"Michael Lang" <ml@nospam.com> wrote in message
news:Xn********************************@207.46.248 .16...

protected override void OnKeyDown(KeyEventArgs e)
{
if (e.KeyCode == Keys.PageDown)
{ // handle PageDown key press
}
else if(e.KeyCode == Keys.PageUp)
{ //handle PageUp key press
}
else
{ //only raise event to subscribers if not
// one of the above keys
base.OnKeyDown (e);
}
}


The supplied code is in C#. both languages use the same framework. So
all you need to do to convert any C# code sample to VB.NET are syntax
changes.

Do you have help files? Take a look at the KeyEventArgs class, which is
what you are given in the KeyDown event. Take a look at all the
properties in that class. It tells you all you need to know about what
keys are being pressed at that time.

Here it is on MSDN (watch word wrap):
http://msdn.microsoft.com/library/de...l=/library/en-
us/cpref/html/frlrfsystemwindowsformskeyeventargsmemberstopic.as p

--
Michael Lang, MCSD

Jul 21 '05 #8

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

Similar topics

7
by: Ian Henderson | last post by:
I am currently trying to teach myself .NET (specifically vb.NET). I have been working with VBA in Access 97 for a few years now, and consider myself to be quite proficient. At present, I have...
3
by: squeak | last post by:
Hi there, I'm new to VB2005 so its probably a very simple answer too! But i just can't think how to do it... Basically i need to detect mouse (and preferably keyboard) movement and kepresses...
4
by: Anthony P. | last post by:
Hello Everyone, I'm building an application and need to keep track of the amount of text in a textbox to make sure the user knows how many characters they have left to type. I've set the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.