Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 6 of 6 total
Thread SetFocus to control in an embedded form
Thu, Oct 6 2016 9:26 AMPermanent Link

thomh

Hi,

Could someone take a look at the attached project?

I have a main Form1 (with a base panel) and a Child Form2.

On the Form1.OnCreate I code:

Form2 := TForm2.Create(pnlMain)
Form2.Show;

Why isn't the Edit1 control on Form2 focused?
I have to click it for it gain focus.

Edit1.SetFocus does not seem to work either.

// Thom



Attachments: FocusTest.zip
Fri, Oct 7 2016 2:35 AMPermanent Link

Michael Dreher

thomh wrote:

// Could someone take a look at the attached project?

I observed that after calling "Form2.Show" the ActiveForm ist still the parent main form. I checked that the
focus can be set after the child form becomes active. This is different from calling a form in modal state, where it becomes the ActiveForm automatically.

The help on SetFocus says:
 // The ability of a control to obtain the input focus is dependent upon:
 //   - Whether the control is focusable
 //   - Whether the control is visible
 //   - Whether its parent container control is visible

All three conditions are true in this case. There's no
 - Whether its parent container's form is the active form

But how to set the active form from the code? Application.Surface.ActiveForm is a readonly property. Should this be done by the code behind the SetFocus method?

Michael Dreher
Fri, Oct 7 2016 4:41 AMPermanent Link

Matthew Jones

Michael Dreher wrote:

> focus can be set after the child form becomes active

I have been putting a function in the form that sets the focus to where I want it, and in my OnShow for the form, I call that with the async keyword first. That allows the focus to work as all the changes have settled.

--

Matthew Jones
Fri, Oct 7 2016 8:47 AMPermanent Link

thomh

>>"Matthew Jones" wrote:
>>I have been putting a function in the form that sets the focus to where I want it, and in my OnShow for the form, I >>call that with the async keyword first. That allows the focus to work as all the changes have settled.

That worked for me as well, Matthew.

Thanks.

// Thom
Fri, Oct 7 2016 12:30 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Thom,

<< Why isn't the Edit1 control on Form2 focused? I have to click it for it gain focus. >>

If the first form (Form1) is being auto-created, then it is being created with the async keyword, which means that you'll also need to use the async keyword for any focus operations in the OnCreate event for the form.

The other alternative is to use the OnShow event instead for this type of functionality.

Tim Young
Elevate Software
www.elevatesoft.com
Fri, Oct 7 2016 12:32 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Michael,

<< But how to set the active form from the code? Application.Surface.ActiveForm is a readonly property. Should this be done by the code behind the SetFocus method? >>

Embedded forms are excluded from the ActiveForm property because EWB only considers top-level forms to be those that are direct children of the application surface.

Tim Young
Elevate Software
www.elevatesoft.com
Image