Solved

Force input and focus to daughter form

Posted on 2002-03-27
7
288 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month9 days, 23 hours left to enroll

623 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