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
Solved

Force input and focus to daughter form

Posted on 2002-03-27
7
285 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
FMX enumerated colours 2 115
Convert GUI app into console app for Win32 Env 5 109
how can i search if string exist in array ? 3 63
Delphi Seattle StructureView color 1 14
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…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

860 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