Setting the focus in a dialog box

Online Data Entry Jobs

Online Data Entry Jobs

Get Instant Access

Only one component per form can be active, or have the focus, in a running application at any given time. The component having the focus by default corresponds to the ActiveControl property of the form, which can be set at design time, or used during run time to access the component that has focus. To change focus at run time, use the SetFocus method.

Setting focus at design time

When you design a dialog box, you usually want to specify which component should have focus when the dialog box first opens. For example, in a data entry form you might want to give focus to the edit or memo component where users will first be entering data. That way the user can begin typing immediately, without first having to tab to a certain component.

■ To specify the active component at design time,

1 Select the form, then use the Object Inspector to select the form's ActiveControl property.

2 From the property's drop-down list, select the component you want to have focus when the form first opens.

If no component is specified as the form's active component, Delphi gives initial focus to the component that is first in the tab order, excluding

  • Disabled components
  • Components that are not visible at run time
  • Components whose TabStop property is set to False

Setting focus at run time

Focus changes automatically at run time when the user tabs among components. You often want to actively specify a focus change. Taking the previous example, if you are designing a data entry form, you might want to specify that focus change from field to field each time the user presses an arrow key. You specify that a component gets focus by using the SetFocus method.

■ To change the active component during run time, type the following code in the appropriate event handler, where <componentn> is the name of the component, for example, Button1:

<componentn>.SetFocus;

For more information, see the online Help topic SetFocus method.

Note If you set a button as the active control for the form at design time, that setting overrides at run time any default button you might have specified. However, you'll often find ways to use an active control together with a default button to process user input.

► For an example of this, try the following.

1 On a blank form, add an Edit component, a Button component, and a Memo component.

2 Set the component properties as follows:

Table 3.4 Property settings for demonstrating ActiveControl

Component

Property

Setting

Formi

ActiveControl

Editi

Editi

Text

<blank>

Buttoni

Default

True

Buttoni

Caption

OK

3 Write the following OnClick event handler for Button1:

procedure TForm1.Button1Click(Sender: TObject); begin

Memol.Lines.Add(Editl.Text); Editl.Clear; end;

4 Run the program.

When the program starts, the cursor is in Edit1, the active control for the form.

5 Type some text in Edit1, and press Enter.

Because Button1 is the default button, pressing Enter activates its OnClick event.

Was this article helpful?

0 0
Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook


Responses

  • mariam
    How to set focus in a dialog box with delphi?
    9 years ago
  • Penny
    How to set focus for more than one control in delphi?
    9 years ago
  • Juan
    How delphi set focus text field?
    9 years ago

Post a comment