Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

System.Windows.Forms.KeyEventArgs

Status
Not open for further replies.

RileyCat

Programmer
Apr 5, 2004
124
US
Is it possible to access the System.Windows.Forms.KeyEventArgs inside of the KeyPress event? I know System.Windows.Forms.KeyPressEventArgs gets passed to the KeyPress event and System.Windows.Forms.KeyEventArgs gets passed to the KeyDown event, but I was wondering if there's anyway to access both in a single function so I don't have to use both the KeyDown and KeyPress to process a key stroke.

Thanks!
 
Here's more info ...

I'm doing the following with each ...

private void txtCouponData_On_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
// Initialize the key press flags to false.
InvalidKeyPressed = false;
EnterKeyPressed = false;

// Determine if this keystroke is the enter key or tab key.
if ((e.KeyCode == Keys.Enter) || (e.KeyCode == Keys.Tab))
{
EnterKeyPressed = true;
}
// Handle the escape key press as a click on the cancel button.
else if (e.KeyCode == Keys.Escape)
{
btnCancel.Focus();
}
// Determine whether the keystroke is a number from the top of the keyboard.
else if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
{
// Determine whether the keystroke is a number from the keypad.
if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
{
// A non-numerical keystroke was pressed.
// Set the flag to true and evaluate in KeyPress event.
InvalidKeyPressed = true;
}
}
}

private void txtCouponData_On_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
// Check to see if we're pressing the enter key.
if (EnterKeyPressed == true)
{
// TODO: This is the point that we'll go off to validate
// the scanned coupon data.
// For now, we'll just simulate it.
simulate_btnLogon_GotFocus(sender, e) ;
}
}


I want to try to do it all within the KeyPress event.

Any ideas/suggestions/recommendations would be appreciated.

Thanks in advance!
 
From the doc:
"A KeyPressEventArgs specifies the character that is composed when the user presses a key. For example, when the user presses SHIFT + K, the KeyChar property returns an uppercase K.

A KeyPress event occurs when the user presses a key. Two events that are closely related to the KeyPress event are KeyUp and KeyDown.
The KeyDown event precedes each KeyPress event when the user presses a key, and a KeyUp event occurs when the user releases a key.
When the user holds down a key, duplicate KeyDown and KeyPress events occur each time the character repeats. One KeyUp event is generated upon release.

With each KeyPress event, a KeyPressEventArgs is passed. A KeyEventArgs is passed with each KeyDown and KeyUp event. A KeyEventArgs specifies whether any modifier keys (CTRL, SHIFT, or ALT) were pressed along with another key.

Set Handled to true to cancel the KeyPress event. This keeps the control from processing the key press."
-obislavu-


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top