Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


ID: 7, Set Focus to contained controls

by Mike Orriss Email: Anonymous


Set Focus to contained controls
Download Details
FTP  download also available
CDN Login Required to Download. (You will be redirected to the login page if you click on the Download Link)
To download this, you must have registered:
A free membership

For Delphi, Version 1.0  to 4.0 285 downloads
Copyright: No significant restrictions


Size: 675 bytes
Updated on Thu, 18 Oct 2001 23:34:46 GMT
Originally uploaded on Sun, 09 May 1999 00:00:00 GMT
SHA1 Hash: 5C9FC2479AF6E7566320400578EB36F5B744E2DA
MD5 Hash: BC7A7CAB15C2BCBDD4FE02BF278600EB

    Explore the files in this upload

Description
Question: Is there any way of setting focus to a control even if its parent control is not currently visible?


Answer: The following function supports TPageControl, TTabbedNotebook and TNotebooks at any level of nesting:


function SetFocusOK(frm: TForm; ctl: TWinControl): boolean;
begin
Result := False;
if (frm=nil) or (not frm.Visible) then exit;

Result := ctl.CanFocus;
if not Result then begin
SetFocusOKhelper(frm,ctl);
Result := ctl.CanFocus;
end;
if Result then ctl.SetFocus;
end;


To give focus to button3 on form2 you then just say:

if SetFocusOK(form2,button3) then ....


The main work is done by recursive calls to SetFocusOKhelper, which travels up the control tree as far as is necessary:

function SetFocusOKhelper(frm: TForm; ctl: TWinControl): boolean;
begin
ctl.Enabled := True;
ctl.Visible := True;
Result := (ctl.Parent <> nil);
if (not Result) or (ctl.Parent=frm) then exit;

if ctl is TTabSheet then
(ctl.Parent as TPageControl).ActivePage := TTabSheet(ctl)
else if ctl is TPage then
(ctl.Parent as TNoteBook).ActivePage := TPage(ctl).Caption
else if ctl is TTabPage then
(ctl.Parent as TTabbedNoteBook).ActivePage := TTabPage(ctl).Caption;

Result := (ctl.CanFocus and ctl.Parent.CanFocus);
if not Result then Result := SetFocusOKhelper(frm,ctl.Parent);
end;

   Latest Comments  View All Add New

Move mouse over comment to see the full text

Server Response from: ETNACDC03