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
topRequirements
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.
- On the Project menu, click Add
Windows Form, and then click Open.
By default,
Form2 is created. - Add three
TextBox controls to Form2.
- Add three Label controls to
Form2.
Note Put each Label control on the left
side of the TextBox control. - 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.
- 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.
- 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 = "John"
MyForm.TextBox3.Text = "123456"
'Display the form.
MyForm.Show()
- 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 "TextBox1" Or Me.Controls(i).Name Is "TextBox2" Or Me.Controls(i).Name Is "TextBox3" Then
'Find the TextBox for which Text is not set.
If Me.Controls(i).Text = "" Then
'Set the focus on the control without any text.
Me.ActiveControl = Me.Controls(i)
End If
End If
Next
- On the Build menu, click Build
Solution.
- On the Debug menu, click
Start.
- Click Set Focus.
The
focus is set to the TextBox control with no text.
back to the
topThe 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:
- Replace the following statement in
the Form2_Load event handler
Me.ActiveControl = Me.Controls(i)
with the following statement: Me.Controls(i).Select()
- On the Build menu, click Build
Solution.
- On the Debug menu, click
Start.
- Click Set Focus.
The
focus is set to the TextBox control with no text.
back to the
topThe 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:
- 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 "TextBox1" Or Me.Controls(i).Name Is "TextBox2" Or Me.Controls(i).Name Is "TextBox3" Then
'Find the TextBox for which Text is not set.
If Me.Controls(i).Text = "" Then
'Set the focus on the control with no text.
Me.Controls(i).Focus()
End If
End If
Next
- On the Build menu, click Build
Solution.
- On the Debug menu, click
Start.
- Click Set Focus.
The focus is
set to the TextBox control with no text.
back to the
topTroubleshoot
- 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