SetFocus Method

Purpose

Moves the focus to the specified control or form.

Syntax

object.SetFocus

object:Ocx Object

Description

The object must be a Form object, or an Ocx control that can receive the focus. After invoking the SetFocus method, any user input is directed to the specified form or control.

You can only move the focus to a visible form or control. Because a form and controls on a form aren't visible until the form's Load event has finished, you can't use the SetFocus method to move the focus to the form being loaded in its own Load event unless you first use the Show method to show the form before the Form_Load event procedure is finished.

You also can't move the focus to a form or control if the Enabled property is set to False. If the Enabled property has been set to False at design time, you must first set it to True before it can receive the focus using the SetFocus method.

Example

Ocx TextBox tb = "", 10, 10, 140, 14 : .BorderStyle = 1 : .Text = "TextBox" : .ReadOnly = True

Ocx Command cmd = "Command Button", 160, 10, 140, 22

Ocx Option opt(0) = "Give Focus to TextBox", 10, 40, 180, 14 : opt(0).Value = 1

Ocx Option opt(1) = "Give Focus to Command Button", 10, 56, 180, 14

tb.SetFocus

Do : Sleep : Until Me Is Nothing

 

Sub opt_Click(Index%)

If Index% = 0 : tb.SetFocus

Else : cmd.SetFocus

EndIf

EndSub

See Also

Form

{Created by Sjouke Hamstra; Last updated: 23/10/2014 by James Gaite}