Solved

Force input and focus to daughter form

Posted on 2002-03-27
7
282 Views
Last Modified: 2012-05-07
Hi Everyone,

My app creates a secondary form for input. I need to make sure that while this secondary form is active, the user couldn't click on the primary form (displayed right behind the daughter form). Setting the secondary form to AlwaysOnTop alone doesn't help - the form remains on top but I can still go back and click things/change settings on the main form behind it.

Any tips/ideas are appreciated.

Thanks
Vladimir
0
Comment
Question by:vladh
7 Comments
 
LVL 17

Expert Comment

by:inthe
ID: 6900898
hi,
show the form modal

ie:

my_second_form.showmodal;

this stops the user form getting to the form below.
0
 
LVL 3

Author Comment

by:vladh
ID: 6901600
inthe,

My app has 3 daughter forms. Your suggestion worked great for 2 of them, but the third one consistently blows up when I click a button to close it. All three forms are almost identical. When I click OK button on the bad one, it generates access violation on the ShowModal in the FormCreate method; not on the OKBtnClick!
Any suggestions?
Thanks
Vlad
0
 
LVL 9

Expert Comment

by:ginsonic
ID: 6901776
To keep on top and allways focusable... I found in past next trick on net :

8<--------

Handling WM_ENABLE and WM_WINDOWPOSCHANGING messages as follow will keep the window always on top.

procedure TAlwaysOnTopForm.WMEnable(var Message: TWMEnable);
begin
 inherited;
 if Visible and not Message.Enabled then
 begin
   SetWindowPos(Handle, HWND_NOTOPMOST, 0, 0, 0, 0,
     SWP_NOSIZE or SWP_NOMOVE or SWP_NOACTIVATE);
   EnableWindow(Handle, True);
   Application.NormalizeAllTopMosts;
   SetWindowPos(Handle, HWND_TOPMOST, 0, 0, 0, 0,
     SWP_NOSIZE or SWP_NOMOVE or SWP_NOACTIVATE);
 end;
end;

procedure TAlwaysOnTopForm.WMWindowPosChanging(var Message: TWMWindowPosMsg);
begin
 inherited;
 if Visible then
   with Message.WindowPos^ do
   begin
     if (flags and SWP_NOZORDER) = 0 then
       hwndInsertAfter := HWND_TOP;
   end;
end;
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 17

Accepted Solution

by:
geobul earned 100 total points
ID: 6902276
Hi,
You could use Form1.Enabled := false/true and a counter for opened child forms.

In the example below Form1.Button1 opens Form2, Form2.Button1 opens Form3. Form1 is inaccessible as long as there are opened child forms.

unit1 - main form
----
type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure ShowForm(fForm: TForm);
    procedure CloseForm;
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
    ChildFormsCount: integer;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
uses Unit2, Unit3;

procedure TForm1.ShowForm(fForm: TForm);
begin
  Inc(Form1.ChildFormsCount);
  Form1.Enabled := false;
  fForm.Show;
end;

procedure TForm1.CloseForm;
begin
  Dec(Form1.ChildFormsCount);
  if Form1.ChildFormsCount = 0 then Form1.Enabled := true;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  ChildFormsCount := 0;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowForm(Form2);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  ShowForm(Form3);
end;
end.
---

unit2 - subform - may open form3
----
type
  TForm2 = class(TForm)
    Button1: TButton;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation
uses unit1, unit3;

procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Form1.CloseForm;
end;

procedure TForm2.Button1Click(Sender: TObject);
begin
  Form1.ShowForm(Form3);
end;
end.
---

unit3 - another subform
---
type
  TForm3 = class(TForm)
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form3: TForm3;

implementation
uses unit1;

procedure TForm3.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Form1.CloseForm;
end;
end.

Regards, Geo
0
 
LVL 3

Author Comment

by:vladh
ID: 6908471
inthe,

Your solution almost worked - for 2 forms out of three. The third one totally refused to work this way and I can find no explanation for its misterious behaviour - all three forms are very similar.

Looks like ShowModal is a cool idea but I'll have to put it on the backburner until I figure out what went wrong with it.

Thanks anyway
Vlad
0
 
LVL 3

Author Comment

by:vladh
ID: 6908473
Geo,

Simple, cheap and a VERY effective trick! Thanks :)
Vlad
0
 
LVL 17

Expert Comment

by:geobul
ID: 6908750
My pleasure.
Geo
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
A short film showing how OnPage and Connectwise integration works.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

932 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now