views:

16

answers:

1

I have a MDI windows forms application and my child forms mostly have "OK" and "Cancel" buttons. However I do not want them to be activated with ENTER/ESC keys to prevent accidental saves/aborts. So, the form has both AcceptButton and CancelButton set to none. The ESC button indeed does nothing, but the ENTER button still closes the form by "clicking" on the first button found, sorted by TabOrder.

Why is this so? Must I really start doing workarounds and catching the ENTER key?

Added: OK, this is way weirder. Reflector tells me that apparently if the first control by Tab Order (well, actually the control which is active by default when the form is opened) is a button, then it gets assigned as the default control. Otherwise nothing happens. Now the question changes to: WTF?!

A: 

It sounds that as OK button is the first control in the tab order, it automatically gets keyboard focus when the form is loaded, meaning that hitting enter will click it. If you would prefer the keyboard focus to go to another control, try setting the ActiveControl property to another element on the forum.

Button okButton = new Button();
TextBox someOtherControl = new TextBox();

// Add controls to form.
this.Controls.Add(okButton);
this.Controls.Add(someOtherControl);

// Specifically set the ActiveControl on the form.
this.ActiveControl = someOtherControl;
Fara
No, it's not that. The ENTER key works even if another control is selected. As I said - it's actually made the **Default** button of the form (which means "the button that gets hit when user presses ENTER").
Vilx-