Srivastava
Srivastava

Reputation: 3578

How to select a button as default button of the form

I have a form that takes user input and then let the user get connected to the SQL-Server.

This is happening on Button.Click. But where can I set the property Default button so that when the user clicks Enter it does the work of that button.

Upvotes: 89

Views: 57164

Answers (4)

DRapp
DRapp

Reputation: 48139

I think you want the "AcceptButton" property at the FORM level... That will expose a combobox of available controls on your form, then select your "button" you want to use as the "Default" button on enter.

Upvotes: 22

mw509
mw509

Reputation: 2093

I have noticed severally how there is a mix up when it comes to an active button and an accept button. I just came out of it. So, I just thought I add a little option to the answers already given. Obviously, the best answer is;

this.AcceptButton = AcceptButton;

However, if you wish to have the button as an active control, this is what you do;

this.ActiveControl = OkButton;

details: https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.containercontrol.activecontrol?view=netcore-3.1

I hope it is helpful to anyone searching.

Upvotes: 1

walterV
walterV

Reputation: 31

In addition to Form.AcceptButton property the "OK" button must have the TabOrder property set to 0 and all other controls within the form should have a TabOrder >0.

This can be done using a form resouce contruction kit or by code eg. buttonOK.TabOrder = 0;

Upvotes: 3

BeemerGuy
BeemerGuy

Reputation: 8269

It is called AcceptButton now on the form; set that to the button that will be the default button.
Refer to Form.AcceptButton Property

Upvotes: 150

Related Questions