Solved

Force input and focus to daughter form

Posted on 2002-03-27
7
280 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

707 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

19 Experts available now in Live!

Get 1:1 Help Now