Recent

Author Topic: [Solved] Give an error when a third screen is open  (Read 1166 times)

madref

  • Hero Member
  • *****
  • Posts: 949
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
[Solved] Give an error when a third screen is open
« on: February 22, 2020, 03:46:57 pm »
I am redesigning my database.
I have added a ribbon form to my program and I have a logo form also active (see screen 1).


When I open a third screen (as seen in screen 2) and I then press the exit-button in my ribbon I want it to give an error with the text "Please close active screen first !"


How can I achieve this?
« Last Edit: February 22, 2020, 11:47:21 pm by madref »
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 3.99 (rev main_3_99-649-ge13451a5ab) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Monterey

Otto

  • Full Member
  • ***
  • Posts: 226
Re: Give an error when a third screen is open
« Reply #1 on: February 22, 2020, 04:22:40 pm »
Hello madref.

If I understand your request correctly, you could use a hook. In Windows I've fixed issues similar to yours in this way.

Otto.
Kind regards.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Give an error when a third screen is open
« Reply #2 on: February 22, 2020, 04:46:16 pm »
In the event handler count the number of visible forms and, if more than a certain threshold, ShowMessage the error. A way get the count of forms showing is, for example:

Code: Pascal  [Select][+][-]
  1. function TForm1.FormsShowing: Integer;
  2.  
  3.   function IsVisibleForm(constref AComponent: TComponent): Boolean;
  4.   begin
  5.     Result := AComponent.InheritsFrom(TForm)
  6.               and TForm(AComponent).Showing;
  7.   end;
  8.  
  9. var
  10.   i: Integer;
  11. begin
  12.   Result := 0;
  13.   with Application do
  14.     for i := 0 to ComponentCount-1 do
  15.       if IsVisibleForm(Components[i]) then
  16.         Inc(Result);
  17. end;

Note that it will work only for forms whose parent is the application, i.e. those created with TForm.Create(Application) or through Application.CreateForm()

Note also that using:
Code: Pascal  [Select][+][-]
  1. if Application.Components[i].InheritsFrom(TSomeForm)
you can test for specific forms by their class.
« Last Edit: February 22, 2020, 04:50:38 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Give an error when a third screen is open
« Reply #3 on: February 22, 2020, 07:49:39 pm »
A way get the count of forms showing is...
I believe the same information is returned by
Code: Pascal  [Select][+][-]
  1. Screen.CustomFormZOrderCount;

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Give an error when a third screen is open
« Reply #4 on: February 22, 2020, 08:26:01 pm »
A way get the count of forms showing is...
I believe the same information is returned by
Code: Pascal  [Select][+][-]
  1. Screen.CustomFormZOrderCount;

You're almost right. I remebered having seen Forms[] and FormCount properties but I thought (incorrectly) they were in TApplication and was somewhat baffled when I couldn't find them; they are, indeed, properties of TScreen. The one I was looking for is, of course, Screen.FormCount.

Sorry for the mistake :-[
And btw, every place I talked about Application.Components[i] should be replaced by Screen.Forms[i] which indeed lists all forms, not just those parented by Application, and returns a TForm reference.

Good catch, Howard
« Last Edit: February 22, 2020, 08:27:51 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

madref

  • Hero Member
  • *****
  • Posts: 949
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Give an error when a third screen is open
« Reply #5 on: February 22, 2020, 11:47:04 pm »
Thanks
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 3.99 (rev main_3_99-649-ge13451a5ab) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Monterey

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: [Solved] Give an error when a third screen is open
« Reply #6 on: February 23, 2020, 12:04:42 am »
Screen.FormCount may not give you the answer you need though in this situation.

Screen.FormCount counts the number of form instances that are currently created. They may or may not all be visible or active.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: [Solved] Give an error when a third screen is open
« Reply #7 on: February 23, 2020, 11:20:38 am »
Screen.FormCount may not give you the answer you need though in this situation.

Screen.FormCount counts the number of form instances that are currently created. They may or may not all be visible or active.

The same happens with CustomFormCount and CustomFormZOrderCount, AFAICT there is no property giving the number of visible forms; that's why you need a function counting the number of Forms[i].Showing, as in:

Code: Pascal  [Select][+][-]
  1. function TForm1.VisibleFormCount: Integer;
  2. var
  3.   i: Integer;
  4. begin
  5.   Result := 0;
  6.   for i := 0 to Screen.FormCount-1 do
  7.     if Screen.Forms[i].Showing then
  8.       Inc(Result);
  9. end;
« Last Edit: February 23, 2020, 11:24:41 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: [Solved] Give an error when a third screen is open
« Reply #8 on: February 23, 2020, 03:42:12 pm »
Lucamar you are quite right.
I did not actually check the return result of Screen.CustomFormZOrderCount, merely assumed from its name that it would take account of window visibility.
I consider this function misleadingly named. How can an invisible window have a z-order and get included in the count?

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: [Solved] Give an error when a third screen is open
« Reply #9 on: February 23, 2020, 04:44:37 pm »
That name comes about because it's the count of forms in the CustomFormsZOrdered[] property, which is an otherwise ordered version of CustomForms[].

And of course, every created control (including forms) has its own Z-order, whether it's visible or not; when you Show it, it's shown in its proper "Z" place unless and until you do, for example, a BringToFront or SendToBack.Though there are some exceptions, IIRC ...
« Last Edit: February 23, 2020, 04:46:13 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018