Catch Enter Key

feurich

Centurion
Joined
Oct 21, 2003
Location
Holland
[csharp]
private void cboImportset_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
// Catch the enter keypressed
if (e.KeyChar = Keys.Enter)
// {
// Check if ImportSet already exists if so select ImportSet.
// if ImportSet doesn' exists add it to ImportSet list.
MessageBox.Show(e.KeyChar.ToString());
// }
[/csharp]

Errors are:
- Cannot implicitly convert type 'char' to 'bool'
- Property or indexer 'System.Windows.Forms.KeyPressEventArgs.KeyChar'
cannot be assigned to -- it is read only
- Cannot implicitly convert type 'System.Windows.Forms.Keys' to 'char'
 
Last edited:

feurich

Centurion
Joined
Oct 21, 2003
Location
Holland
Sorry for that :eek:

But even with the == inplace it doesn't compile.

error is:
Operator '==' cannot be applied to operands of type 'char' and 'System.Windows.Forms.Keys'

I know I have to convert the e.KeyChar. But how.
In VB.NET you have the Asc() function but what is that in C#????
 

feurich

Centurion
Joined
Oct 21, 2003
Location
Holland
No e.KeyCode

I don't have e.Keycode in my intellisence...
Also on a lot of website it says that there must be a e.KeyValue in the event arg.
But I don't see it.
Any info on that?
 

feurich

Centurion
Joined
Oct 21, 2003
Location
Holland
Tried it

I tried it but when i compile the code it says:
'System.Windows.Forms.KeyPressEventArgs' does not contain a definition for 'keycode'

See my last post. That does the trick.

Thanks for the effort.
 

Joe Mamma

Senior Contributor
Joined
Mar 1, 2004
Location
Washington DC
also. . .
KeyCode is a member of KeyEventArgs but not KeyPressEventArgs.

FYI
order of events -
KeyDown (a KeyEventHandler)
KeyPress (a KeyPressEventHandler)
KeyUp (a KeyEventHandler)
 

Anil_hyd

Newcomer
Joined
Jul 10, 2012
private void LoginForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
button1_Click(sender, e);
}

}:)
 
Top Bottom