C# – Windows Forms: Multiple default buttons


In a simple Guess-The-Number game I'm making, the user inputs a couple of numbers and hits a button. A second panel becomes enabled and the original one is disabled. The user now enters a number and hits another button, multiple times. Since the two panels will never be enabled at the same time, I'd like for both buttons to be "default", i.e., they will be pushed if Enter is pressed. Is this possible? Apparently, I can only set one of these per window.

Best Solution

A window in Windows by definition has only one default button. This is a functionality intended for dialog windows where you would never disable the default button.

What you can do instead is to switch the default button when you disable one panel.

Another option would be to throw out default buttons altogether and use KeyPreview on the form and handle the enter key yourself, sending it to the appropriate button that is currently active.