tags:

views:

49

answers:

2

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...

+2  A: 

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).

Fredrik Mörk
It's so obvious once someone points it out! :)
Dave
+3  A: 

Set the AcceptButton property to null when you create that textbox, and set it back to the normal value when it loses focus:

var myTextBox = new TextBox ... ;
AcceptButton = null;

myTextBox.Leave += (s, e) => { AcceptButton = btnOK; };
Timwi