Event cancel

A

AntoS

I would you like to capture a keyPress or KeyDown event and checking wich
characters was pressed and if the char is not allowed I want to Cancel the
event.

That's what I want:
In a textBox I want to allow insert of only number or character that I
select and if sameone press another char I simply want to do nothing.

I got a Windows.Form
and there a textBox

Thanks

AntoS
 
M

Morten Wennevik

Hi Antos,

The KeyPressEventArgs and KeyDownEventArgs has a Handled property. Set
this property to true to cancel the event.

It is easiest to do it in the KeyPressEvent and use

if(!Char.IsLetterOrDigit(e.KeyChar)) // e is a KeyPressEventArgs
e.Handled = true;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top