Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 290
  • Last Modified:

Force input and focus to daughter form

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
vladh
Asked:
vladh
1 Solution
 
intheCommented:
hi,
show the form modal

ie:

my_second_form.showmodal;

this stops the user form getting to the form below.
0
 
vladhAuthor Commented:
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
 
ginsonicCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
geobulCommented:
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
 
vladhAuthor Commented:
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
 
vladhAuthor Commented:
Geo,

Simple, cheap and a VERY effective trick! Thanks :)
Vlad
0
 
geobulCommented:
My pleasure.
Geo
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now