Sonosar
Sonosar

Reputation: 526

Is TextBox behavior correct

Why TextBox is using e.Handled = true in order to prevent specific characters from being typed.
Don't we have a better way?
Isn't this a wrong design?

Currently I want to not receive KeyDown event if specific TextBox is in focus is there a good way to do this?Or I'll have to check is the OriginalSource the TextBox I want...

Edit:

As It seems that my original question is a bit confusing Let me reform it.
Lets suppose we have a Canvas and textbox as a child of canvas. I registered for keyDown event of canvas to do something very interesting there.
I don't want to receive keydown events when user is typing in textBox though. Is there a way to do this without involving e.OriginalSource?

Upvotes: 1

Views: 172

Answers (1)

Konrad Rudolph
Konrad Rudolph

Reputation: 545528

If you want to prevent any input you can (in fact, should) also set ReadOnly = true instead of listening to the KeyDown event.

If you want to selectively prevent key strokes, listening to KeyDown and setting e.Handled is the only way.

However, selectively preventing keystrokes is bad for usability; don’t do it. Instead, use the Validating event of the text box control to validate the user input.

Upvotes: 5

Related Questions