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 7 4077
"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
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
"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)
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
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
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)
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: 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,...
|
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...
|
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...
|
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...
| |