Recent

Author Topic: Disabled or Invisible Window can't be FOCUSED  (Read 193 times)

Awesome Programmer

  • Sr. Member
  • ****
  • Posts: 421
  • Programming is FUN only when it works :)
    • Cool Technology
Disabled or Invisible Window can't be FOCUSED
« on: July 23, 2019, 08:48:56 pm »
I don't understand why I am getting this. I start my program and program's main window is displayed. I then click on a button that window to open another window. Except this time the second window is opened with SHOWMODAL command. The second window pops up with no issue at all. However, As soon as I close the second modal window and then click on the main window, I get this DISABLED OR INVISIBLE WINDOW CAN'T BE SETFOCUS error. Then, my program crashes.

Code: Pascal  [Select]
  1. ...
  2.  
  3. procedure TMainFrm.Button1Click(Sender:TObject);
  4. var
  5.      teststr:string;
  6. begin
  7.        if SecondFrm.ShowModal=MrOk then
  8.              teststr:=SecondFrm.Label1.caption;
  9.        
  10.        //This is what I have been trying and this workaround won't work at all.
  11.        MainFrm.Enabled:=true;
  12.        MainFrm.Visible:=true;
  13.        MainFrm.ActiveControl:=MainFrm;      
  14. end;
  15. ...
  16.  
  17.  

Thaddy

  • Hero Member
  • *****
  • Posts: 9188
Re: Disabled or Invisible Window can't be FOCUSED
« Reply #1 on: July 23, 2019, 09:18:43 pm »
If a form is modal, you prevent any other form to respond - visually -. First the modal form has to close . You can set properties for  other forms, however they will propagate visually once the  modal form is closed.
« Last Edit: July 23, 2019, 09:20:52 pm by Thaddy »
also related to equus asinus.

Awesome Programmer

  • Sr. Member
  • ****
  • Posts: 421
  • Programming is FUN only when it works :)
    • Cool Technology
Re: Disabled or Invisible Window can't be FOCUSED
« Reply #2 on: July 23, 2019, 09:26:28 pm »
Hi Thaddy

Yes, you are right that the modal form is always the one on the very top of all the other form. Plus, you wouldn't have access to other forms until you close the showmodal form. My windows or forms behave as they should, but when I go back to the main form by first closing the modal form, I get this error.
« Last Edit: July 23, 2019, 09:38:18 pm by Awesome Programmer »

lucamar

  • Hero Member
  • *****
  • Posts: 2082
Re: Disabled or Invisible Window can't be FOCUSED
« Reply #3 on: July 23, 2019, 09:58:20 pm »
Yu shouldn't have that problem unless the second form (the modal one) is not closing correctly. But in that case your code wouldn't pass from the
  if SecondFrm.ShowModal=MrOk then ....

Incidentally, this code:
Code: Pascal  [Select]
  1.    MainFrm.Enabled:=true;
  2.    MainFrm.Visible:=true;
  3.    MainFrm.ActiveControl:=MainFrm;
shouldn't be necessary. Once the modal form is closed the focus should revert to the caller wiyhout more ado. In any case, the usual setting order is:
1) Visible,
2) Enabled (which shouldn't have changed) and
3) ... don't set ActiveControl = form: it's allowed, because a form is effectively a control, but trying to set focus to a form makes the form try to "pass the ball" to its "ActiveControl", if set. You can see where that would lead :)

There must be soemthing strange going on in your program because usually all that is needed is something like:
Code: Pascal  [Select]
  1. procedure TMainFrm.ButtonClick(Sender: TObject);
  2. begin
  3.   if not Assigned(SecondFrm) then
  4.     SecondFrm := TSecondFrm.Create(Application);
  5.   if SecondFrm.ShowModal = mrOK then begin
  6.     {do something};
  7.   end;
  8. end;

Can you show (or attach) a more complete example?
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.