Set focus to a child window

Posted on 2004-08-17
Last Modified: 2010-04-05
i make that a parent of a form2 is a PageControl component under another form1.
when showing the form1 the focus is on it ,and i want to give the focus to form2.
notice : i defined form2.parent as  PageControl who belong to form1.
how can i gave the focus to form2 ???
Question by:mars_r
LVL 17

Expert Comment

by:Wim ten Brink
ID: 11821043
SetFocusedControl(Form2) perhaps?

Expert Comment

ID: 11821796
You may have a few small problems with a form in a pagecontrol.
by adding;
form2.Enabled := true;

buttons will work, and tabs will work fine, but you will not be able to click on an edit control.
you could try adding a Tapplicationevent to the form then doing something like in the onmessage

 if msg.message=wm_lbuttondown then
     GetCursorPos( pt );
     pt := form2.screentoclient(pt);

    wc := controlatpos(pt,false,false);

Expert Comment

ID: 11822241
ok, had a sec to spare. here is the code.
when you set the forms parent do a :

form2.parent := tabsheet1; := 0;
form2.left := 0;
form2.enabled := true;;

place a tApplicationevent on form 2.
on the onmessage event do this:

   wc : tcontrol;
   pt : tpoint;
if msg.message=wm_lbuttondown then //only look at the mouse going down
     GetCursorPos( pt ); //where is the mouse?
     pt := form2.screentoclient(pt); //where is it relative to our form
    wc := controlatpos(pt,false,true); //get the control related to our form from the mouse pos
    if wc is twincontrol then (wc as twincontrol).setfocus; //make sure it is a win control, if it is then setfocus.

tested and works.

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

LVL 27

Expert Comment

ID: 11822979
usual u should use as borderstyle for your form2 bsNone,
as the parent is a TTabSheet (i guess) of a TPageControl

otherwise use

    PostMessage(form2.Handle, WM_NCACTIVATE, 1, 0);

hope this helps

meikl ;-)

Expert Comment

ID: 11849797
is there reason why you want to insert a form into the pagecontrol?

why don't you insert the content of form2 into a panel and
set the parent of that panel to the pagecontrol?
LVL 27

Accepted Solution

kretzschmar earned 250 total points
ID: 11878244
any results?

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi procedure  - Unsatisfied forward or external declaration. 2 161
Intraweb download file link ? 1 134
Magic Software info 18 131
Error E2158 compiling with Delphi XE10 Seattle 2 100
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question