Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set which control gets the focus on application start

For a C# Windows Forms application, how do I set the default focus to a given control when my application starts?

like image 805
Dribbel Avatar asked Jan 14 '10 11:01

Dribbel


People also ask

How do you set a focus control?

Use the SetFocus method when you want a particular field or control to have the focus so that all user input is directed to this object. To read some of the properties of a control, you need to ensure that the control has the focus. For example, a text box must have the focus before you can read its Text property.

When a control has focus it can accept?

A control can be selected and receive input focus if all the following are true: the Selectable value of ControlStyles is set to true , it is contained in another control, and all its parent controls are both visible and enabled.


1 Answers

The one with the minimum tab index automatically gets the focus (assuming the TabStop property is set to true). Just set the tab indices appropriately.

By the way, Visual Studio provides a way to easily set tab indices by just clicking on the controls in the order you want. You can activate this feature by choosing "Tab Order" option in the "View" menu when you are in the form design view.

You can also manually give the focus to a control by calling its Select method when the form loads.

like image 170
mmx Avatar answered Sep 19 '22 13:09

mmx