SetFocus Method

Applies To

CheckBox control, ComboBox control, CommandButton control, ListBox control, MultiPage control, OptionButton control, ScrollBar control, SpinButton control, TabStrip control, TextBox control, ToggleButton control.

Description

Moves the focus to this instance of an object.

Syntax

object.SetFocus

The SetFocus method syntax has these parts:

Part

Description

object

Required. A valid object.


Remarks

If setting the focus fails, the focus reverts to the previous object and an error is generated.

By default, setting the focus to a control does not activate the control's window or place it on top of other controls.

The SetFocus method is valid for an empty Frame as well as a Frame that contains other controls. An empty Frame will take the focus itself, and any subsequent keyboard events apply to the Frame. In a Frame that contains other controls, the focus moves to the first control in the Frame, and subsequent keyboard events apply to the control that has the focus.

See Also

Frame control, ZOrder method.

Example

The following example adds and deletes the contents of a ListBox using the AddItem, RemoveItem, and SetFocus methods, and the ListIndex and ListCount properties.

To use this example, copy this sample code to the Declarations portion of a form. Make sure that the form contains:

  • A ListBox named ListBox1.
  • Two CommandButton controls named CommandButton1 and CommandButton2.
    Dim EntryCount As Single
    Private Sub CommandButton1_Click()
        EntryCount = EntryCount + 1
        ListBox1.AddItem (EntryCount & " - Selection")
    End Sub
    
    Private Sub CommandButton2_Click()
        ListBox1.SetFocus
    
        'Ensure ListBox contains list items
        If ListBox1.ListCount >= 1 Then
            'If no selection, choose last list item.
            If ListBox1.ListIndex = -1 Then
                ListBox1.ListIndex = ListBox1.ListCount - 1
            End If
            ListBox1.RemoveItem (ListBox1.ListIndex)
        End If
    End Sub
    
    Private Sub UserForm_Initialize()
        EntryCount = 0
        CommandButton1.Caption = "Add Item"
        CommandButton2.Caption = "Remove Item"
    End Sub
Example

The following example counts the characters and the number of lines of text in a TextBox by using the LineCount and TextLength properties, and the SetFocus method. In this example, the user can type into a TextBox, and can retrieve current values of the LineCount and TextLength properties.

To use this example, copy this sample code to the Declarations portion of a form. Make sure that the form contains the following controls:

  • A TextBox named TextBox1.
  • A CommandButton named CommandButton1.
  • Two Label controls named Label1 and Label2.
    'Type SHIFT+ENTER to start a new line in the text box.
    
    Private Sub CommandButton1_Click()
        'Must first give TextBox1 the focus to get line count
        TextBox1.SetFocus
        Label1.Caption = "LineCount = " & TextBox1.LineCount
        Label2.Caption = "TextLength = " & TextBox1.TextLength
    End Sub
    
    Private Sub UserForm_Initialize()
        CommandButton1.WordWrap = True
        CommandButton1.AutoSize = True
        CommandButton1.Caption = "Get Counts"
    
        Label1.Caption = "LineCount = "
        Label2.Caption = "TextLength = "
    
        TextBox1.MultiLine = True
        TextBox1.WordWrap = True
        TextBox1.Text = "Enter your text here."
    End Sub