GotFocus Event

See AlsoWCKX8J              Example2MOWPSE>Low

Applies To

Form14TJ2LN (not MDI form), check box9P3BU5, combo box1YZXFF6, command buttonXJSPC0, directory list boxO9U5A0, drive list box5WJO0PW, file list box1M6S8UX, grid2VGT0PT, horizontal scroll bar1JSJOS7, list boxG11UCK, OLE control2HQDVVU, option buttonJYBO08, picture box31MYIWX, text boxYPYZDG, vertical scroll bar1JSJOS7.


Occurs when an object receives the focus1L3L8ZY, either by user action such as tabbing to or clicking the object, or by changing the focus in code using the SetFocus4AVBOY method.  Note that a form receives the focus only when all visible controls are disabled.


Sub Form_GotFocus ( )

Sub ctlname_GotFocus (Index As Integer)


The argument Index uniquely identifies a control if it is in a control array.8G7Y0UU  Typically, you use a GotFocus procedure to specify the actions that occur when a control or form first receives the focus.  For example, by attaching a GotFocus procedure to each control on a form, you can guide the user by displaying brief instructions or status bar messages.  You can also provide visual cues by enabling, disabling, or showing other controls that depend on the control that has the focus.


Note   An object can receive the focus only if its Enabled3XD4FN and Visible32KNZA4 properties are set to True.  To customize Visual Basic's keyboard interface for moving the focus, set the Tab order1JL4HCO or specify access keysIFK11S for controls on a form.


See Also


ActiveControl PropertyJCXG6P

ActiveForm PropertyGCUI9X

LostFocus Event2ZXQDAR

SetFocus Method4AVBOY

TabIndex PropertyH38QPT

TabStop Property81MAWZ

GotFocus Event Example

The example displays a status bar message when a button in an option group gets the focus.  To try this example, paste the code into the Declarations section of a form that contains two option buttons and a label.  Set the Name property for both option buttons to OptionGroup. Then press F5 and click the option buttons.

Sub Form_Load ()

   Label1.AutoSize = True

End Sub


Sub OptionGroup_GotFocus (Index As Integer)

   Select Case Index

      Case 0

         Label1.Caption = "Option 1 has the focus."

      Case 1

         Label1.Caption = "Option 2 has the focus."

   End Select

End Sub


Sub OptionGroup_LostFocus (Index As Integer)

   Label1.Caption = ""

End Sub