Small problem with a e.cancel in a KeyPress event Plz help

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!(Char.IsLetterOrDigit(e.KeyChar) == true ||(e.KeyChar)==8))
{

e.cancel = true;

}

}

i tried the above code for my alphanumeric filter as a textbox

it is giving me the following error.:

its saying :

--->System.Windows.Forms.KeyPressEventArgs' does not contain a definition for 'cancel' <-----

help!

how can i get it to work

my main aim is to only let the user type alphanumeric strings into teh textfield.

-Ankur



Answer this question

Small problem with a e.cancel in a KeyPress event Plz help

  • BobExpress

    u can try

    e.handled = false instead of e.cancel = true;

    Regards,

    Nitin


  • eriksen22

    i got it working with e.handle

    thanks .....


  • Titus63

    aite!

    Thanks..

    will let u know on this thread if i cracked it! :-)

    its 4am here.. will call it a day. will try it tomorrow!.

    -Ankur


  • Elias Marin

    Just remember that C# is case sensitive - lol

    Try: e.Cancel



  • JeffK_

    I know it old but i managed to find this forum while search and the answer is...

    e.Handled = true;

    but you might want to put a MessageBox there too, as there is no warning it just doesn't type anything


  • JasMR

  • Brenton98

    Hey Ankur,

    Actually i do not have a .NET installed box right nw at my disposal(hence no intellisense :-)) hence all the code i am putting is from my head. I beleive there are two more events to handle the keyboard events like keyDown, keyUp. Why don't u try playing around with them, u'll surely get a solution else whenever i get my hand on a .NET installed box i'll be able to help u.

    Regards,

    Nitin


  • cybersol

    yeah tried it myself also before this thread.. it doesnt stop the non alphanumeric to be printed into the text box. so was curious if i was doing it right.

    anyhow. thanks..

    regards,

    Ankur

    PS: i will try someother way to stop those darn non-alpha chars to be typed.


  • zfmicrosoft

    How have you done it
  • Small problem with a e.cancel in a KeyPress event Plz help