Microsoft KB Archive/822493

= How to set the focus on a control when the form loads in Visual Basic .NET or in Visual Basic 2005 =

Article ID: 822493

Article Last Modified on 11/26/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



IN THIS TASK

 * SUMMARY
 * Requirements
 * The Form.ActiveControl Property
 * The Control.Select Method
 * The Control.Focus Method
 * Troubleshoot
 * REFERENCES



SUMMARY
This article describes how to set the focus on a control when the form loads. This article describes how to use the Form.ActiveControl property, how to use the Control.Focus method, and how to use the Control.Select method to set the focus on a control.

The Form.ActiveControl property returns the active control or sets the active control on the form. When the ActiveControl property of the form is set to a control on the form, the focus moves to the control.

The Control.Select method activates the specified control and sets the focus on the control.

The Control.Focus method sets the input focus to the control. A control can receive input focus by using the Control.Focus method if the following conditions are true:
 * The ControlStyles.Selectable style bit of the control is set to True.
 * The control that will receive focus is contained in another control, and all the parent controls are visible and are enabled.

Note You can use the Control.Focus method to set the focus on a control in the Load event of the form when the Visible property of the form is set to True.

back to the top

Requirements
This article assumes that you are familiar with the following topics:
 * Microsoft Visual Basic syntax
 * Microsoft Visual Studio .NET or Microsoft Visual Studio 2005 environment
 * Microsoft Visual Basic controls

back to the top

The Form.ActiveControl Property
The following example describes how to use the ActiveControl property of Form2 to set the focus on the TextBox control with no text:  Start Visual Studio .NET or Visual Studio 2005. On the File menu, point to New, and then click Project. Under Project Types, click Visual Basic Projects.

Note In Visual Studio 2005, click Visual Basic under Project Types. Under Templates, click Windows Application, and then click OK.

By default, Form1 is created. Add a Button control to Form1. Right-click Button1, and then click Properties. In the Properties dialog box, set the Text property to Set Focus.</li> On the Project menu, click Add Windows Form, and then click Open.

By default, Form2 is created.</li> Add three TextBox controls to Form2.</li> Add three Label controls to Form2.

Note Put each Label control on the left side of the TextBox control.</li> In the Properties dialog box, set the Text property of Label1 to Name, set the Text property of Label2 to Company, and then set the Text property of Label3 to ContactNo.</li> In the Properties dialog box, remove the text in the Text property of TextBox1, remove the text in the Text property of TextBox2, and then remove the text in the Text property of TextBox3.</li>  Add the following code to the Button1_Click event handler in Form1: 'Create an instance of Form2. Dim MyForm As New Form2 'Set the Text in the TextBoxes on Form2. MyForm.TextBox1.Text = &quot;John&quot; MyForm.TextBox3.Text = &quot;123456&quot; 'Display the form. MyForm.Show </li>  Add the following code to the Form2_Load event handler: Dim i As Integer For i = 0 To 5 If Me.Controls(i).Name Is &quot;TextBox1&quot; Or Me.Controls(i).Name Is &quot;TextBox2&quot; Or Me.Controls(i).Name Is &quot;TextBox3&quot; Then 'Find the TextBox for which Text is not set. If Me.Controls(i).Text = &quot;&quot; Then 'Set the focus on the control without any text. Me.ActiveControl = Me.Controls(i) End If End If Next </li> On the Build menu, click Build Solution.</li> On the Debug menu, click Start.</li> Click Set Focus.

The focus is set to the TextBox control with no text.</li></ol>

back to the top

The Control.Select Method
The following example describes how to find the TextBox control with no text on Form2, and then describes how to use the Control.Select method to set the focus for the control: <ol>  Replace the following statement in the Form2_Load event handler Me.ActiveControl = Me.Controls(i) with the following statement: Me.Controls(i).Select </li> On the Build menu, click Build Solution.</li> On the Debug menu, click Start.</li> Click Set Focus.

The focus is set to the TextBox control with no text.</li></ol>

back to the top

The Control.Focus Method
The following example describes how to find the TextBox control with no text on Form2, describes how to set the Visible property of Form2 to True, and then describes how to set the focus on the control by using the Focus method of the control: <ol>  Replace the existing code in the Form2_Load event handler with the following code: 'Set the visible property of the form to true, 'and then set the focus to the control. Me.Visible = True Dim i As Integer For i = 0 To 5 If Me.Controls(i).Name Is &quot;TextBox1&quot; Or Me.Controls(i).Name Is &quot;TextBox2&quot; Or Me.Controls(i).Name Is &quot;TextBox3&quot; Then 'Find the TextBox for which Text is not set. If Me.Controls(i).Text = &quot;&quot; Then 'Set the focus on the control with no text. Me.Controls(i).Focus End If End If Next </li> On the Build menu, click Build Solution.</li> <li>On the Debug menu, click Start.</li> <li>Click Set Focus.

The focus is set to the TextBox control with no text.</li></ol>

back to the top

Troubleshoot

 * Remove any text in the TextBox controls on Form2 before you run the application.
 * You can use the Control.Focus method in the Load event of the form to set the focus on a control only after the Visible property of the form is set to True.

back to the top

<div class="references_section">