For a C# Windows Forms application, how do I set the default focus to a given control when my application starts?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With