Still same problem  
Author Message
Redlightpacket





PostPosted: Common Language Runtime, Still same problem Top

Do you replace the e.KeyCode with e.KeyChar. I just tried that and it didn't work I still hadn't figured it out.

Here is the error message

Operator '<' cannot be applied to operands of type 'char' and 'System.Windows.Forms.Keys'

private void textBox3_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)

{

if (e.KeyChar < Keys.D0 || e.KeyChar > Keys.D9)

{

TextBox3.Text = "_";

}

}




.NET Development17  
 
 
Vipul Patel - MSFT





PostPosted: Common Language Runtime, Still same problem Top

Try this

int a = e.KeyChar;

int b = Convert.ToInt32(Keys.D0);

private void textBox3_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)

{

int a = e.KeyChar;

int b = Convert.ToInt32(Keys.D0);

int c = Convert.ToInt32(Keys.D9);

if (a < b || a > c)

{

TextBox3.Text = "_";

}

}



 
 
Mark Benningfield





PostPosted: Common Language Runtime, Still same problem Top

Hello All.

Redlightpacket:

You have to remember, .NET is all about strong types. e.KeyChar returns type "char", and Keys returns "System.Windows.Forms.Keys", which is an enumeration.

If you recall from the docs, there are no implicit type casts applied to enumerations. You have to use an explicit type cast, like this:

if (e.KeyChar < (char)Keys.D0 || e.KeyChar > (char)Keys.D9)

This way, you are comparing items of the same type.

HTH.