.NET Kepresses

  • Thread starter Thread starter Ian Henderson
  • Start date Start date
I

Ian Henderson

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
 
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.
 
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
 
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)
 
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".
 
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
 
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 said:
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
 
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

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/default.asp?url=/library/en-
us/cpref/html/frlrfsystemwindowsformskeyeventargsmemberstopic.asp
 
Back
Top