I have a form with a button hooked up to the AcceptButton
property so that logic occurs and the form is closed when the user presses the Return key.
On that form I display a dynamically created TextBox
that appears when the user double-clicks a certain area then hides when the user presses Return.
How do I prevent the form from processing the key press when the user presses Return while the TextBox
has focus?
I was attempting to say the key press was handled in the TextBox.KeyDown
event handler via KeyEventArgs.Handled
but the Button.Click
event of my accept button is being fired first...
While resetting the Forms AcceptButton
while the TextBox has focus will work, I believe that the more appropriate solution is to use the TextBoxes AcceptsReturn
Property.
If you set
myTextBox.AcceptsReturn = true;
the Form will not accept on pressing RETURN, but your TextBox can handle this itself.
This will avoid the need to reset the AcceptButton in multiple places in your code.
Use the Enter
and Leave
events of the TextBox
to set the AcceptButton
property to null
(on Enter
) and re-assign the button to it (on Leave
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With