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

Article translations Article translations
Article ID: 822493 - View products that this article applies to.
Expand all | Collapse all

On This Page

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.

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

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:
  1. Start Visual Studio .NET or Visual Studio 2005.
  2. On the File menu, point to New, and then click Project.
  3. Under Project Types, click Visual Basic Projects.

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

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

    By default, Form2 is created.
  9. Add three TextBox controls to Form2.
  10. Add three Label controls to Form2.

    Note Put each Label control on the left side of the TextBox control.
  11. 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.
  12. 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.
  13. 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()
  14. 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
  15. On the Build menu, click Build Solution.
  16. On the Debug menu, click Start.
  17. Click Set Focus.

    The focus is set to the TextBox control with no text.

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:
  1. Replace the following statement in the Form2_Load event handler
    Me.ActiveControl = Me.Controls(i)
    with the following statement:
     Me.Controls(i).Select()
  2. On the Build menu, click Build Solution.
  3. On the Debug menu, click Start.
  4. Click Set Focus.

    The focus is set to the TextBox control with no text.

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:
  1. 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
  2. On the Build menu, click Build Solution.
  3. On the Debug menu, click Start.
  4. Click Set Focus.

    The focus is set to the TextBox control with no text.

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.

REFERENCES

For more information, visit the following Microsoft Developer Network (MSDN) Web sites:
Form.Load Event
http://msdn2.microsoft.com/en-us/library/system.windows.forms.form.load(vs.71).aspx
Control.Focus Method
http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.focus(vs.71).aspx

Properties

Article ID: 822493 - Last Review: November 26, 2007 - Revision: 2.5
APPLIES TO
  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
Keywords: 
kbvs2005swept kbvs2005applies kbwindowsforms kbforms kbctrl kbcontrol kbhowtomaster kbhowto KB822493

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com