D
D
Dmitry Gavrilenko2015-03-11 17:16:04
.NET
Dmitry Gavrilenko, 2015-03-11 17:16:04

Cancel adding character to TextBox?

Hello. I'm making a Windows Forms application.
There is a field. You need to enter certain characters into it. (MaskedTextBox not suggested)
In general, the PreviewKeyDown event checks the character that was just pressed (Convert.ToChar(e.KeyValue)). It is necessary that I could control the very addition of a character to the TextBox. When necessary, canceled, and when necessary, allowed to add.
It would be possible to pass e.KeyData = Keys.None, thereby canceling the pressed key, but all properties are read-only.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
AxisPod, 2015-03-12
@Maddox

In the KeyPress handler for objectionable
e.Handled = true;

A
Anton Fedoryan, 2015-03-11
@AnnTHony

if-else

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question