Delphi Error: "Cannot focus a disabled or invisible window"

delphi global exception handler
delphi ignore exception
delphi try-except not working
delphi try except multiple exceptions
delphi exception eedfade
delphi compiler warnings
lazarus try
delphi messagedlg

I'm getting an error when running my compiled program. I've taken every precaution I can think of to prevent the error such as enabling forms before I hide or focus them. The application shows a DimmerForm (grey fade overlay) and then a NewFileForm on top of the overlay. The NewFileForm does its stuff and determines the file that is to be made and creates a new tabsheet in the page control on the MainForm and shows the newly created tab sheet. All this works so far (I can break before the NewFileForm closes and any error occurs and see that the tab sheet has been created and shown successfully). The NewFileForm then closes, which is again working fine.

The error happens when anything then tries to set the focus back to the MainForm. Hiding the DimmerForm, or clicking on the MainForm, or setting focus to the MainForm through code all cause the error message to occur. Placing the code in a {try, except, end} doesn't prevent the error message either. I've searched every other method in the code that could possibly be triggered by the focusing of the MainForm and none are entered before the error occurs (so can't be causing it).

I wrote pretty much the exact same code in a previous version of my application and then decided to restructure it all from the ground up. The previous version of the application worked without a hitch with the form focusing.

Here is the code to open up the NewFileForm:

procedure TMainForm.NewFilesToolButtonClick(Sender: TObject);
begin

  ShowDimmer;
  NewFileForm.ShowModal;
  HideDimmer;

end;

Here is the code to show the dimmer form:

procedure ShowDimmer;
begin

  // Enable to prevent errors
  DimmerForm.Enabled := true;

  // Hide dimmer form and show mainform
  DimmerForm.Show;

end;

Here is the code to hide the dimmer form:

procedure HideDimmer;
begin

  // Enable to prevent errors
  DimmerForm.Enabled := true;
  MainForm.Enabled := true;

  // Hide dimmer form and show mainform
  DimmerForm.Hide;
  MainForm.SetFocus;

end;

Generally you are calling focus to something that is not yet created (on the form you are in the process of creating), try moving some of the onCreate code to onShow. With out more detail it is hard to tell what is happening but try this.

Error and Warning Messages (Delphi), The except clause cannot determine the error type however. Version 2. This is similar to version 2, but specifies different actions for different exception types,  To resolve an internal error. If the error occurs immediately after you have modified code in the editor, go back to the place where you made your changes and make a

Sometimes this error is caused by some kind of activity by the database components during the Post event... If you tried everithing else you could try this quick solution: set the Datasource connected to the Dataset to NIL and then set it back once the Post event complete.

Something like this:

Try
    Datasource1.DataSet := NIL;

    Try
        Query1.Post;
    Except
        On E: Exception Do
        Begin
            ShowMessage('Post error!' + #13#10 + #13#10 + E.Message);
            Exit;
        End;
    End;

Finally
    Datasource1.DataSet := Query1;
End;

Error Messages (Delphi), This is how you raise a generic exception (using the SysUtils.Exception class): raise Exception.Create('Error Message');. An unhandled  When an unhandled exception is thrown in your code, Delphi again magically handles your exception by displaying the error dialog to the user. In most cases, this dialog will not provide enough data for the user (and finally you) to understand the cause of the exception.

Solution given in a comment posted by Arioch 'The

You can also enable "Use Debug DCUs" [...]

Except command, What's an error in an application? An error is an incorrectly coded solution to a problem. Such are logic errors that could lead to wrong function  Hi I would like to give you some background information on the today release of FastMM5. Exactly 2 years ago we (gs-soft) met Marco Cantu to discuss some important points in the long-term Delphi strategy. The efficient memory management within heavily multi threading applications was one of these

Create error messages Delphi 7, Whenever an error results from processing a line of code, Delphi creates (raises) an object descendant from TObject called the exception  The unit named in the error is a package unit and as such cannot be included in your project. A possible cause of this error is that somehow a Delphi unit and a package unit have been given the same name. The compiler is finding the package unit on its search path before it can locate a same-named Delphi file.

On Handling Exceptions in Delphi Exception Handling, I did try to make a project for Android within Delphi 10, but when compiling, I get 1 error and the following message: [MSBuild Erreur] The "  Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question.Provide details and share your research! But avoid …. Asking for help, clarification, or responding to other answers.

Handling Errors and Exceptions in Delphi Applications, This was quite easy, right? Solution: This code will most probably result in a runtime error (access violation). The reason is that FreeAndNil is  The equipment I work on is CHUWI HI10 Pro, this program has a problem finding the disk. I anticipate the question, earlier Delphi worked nice on this computer as well.

Comments
  • Try changing your HideDimmer to DimmerForm.Hide; MainForm.Show; with nothing else. I'm pretty sure that the MainForm.SetFocus is the problem, but you've posted three chunks of code out of context so it's hard to be sure. (The code is not good, BTW; two free-standing procedures that manipulate visible controls are usually not a good idea. It's hard to tell you how to do it better, though; again, no context.)
  • I guess your problem is related to the newly created tabsheet that cannot get focus. Isolate the problem and write a simple reproducible test that can be debugged.
  • Yep, clearly you are attempting to focus a disabled or invisible window. The error message is clear. But we probably need a reproduction to be able to help solve it.
  • After you pressed Break on exception look into Call Stack panel, with the trace perhaps you'd be able to see some event handler of whatever code that tried to visualize the control last. stackoverflow.com/search?q=exception+stack+trace+%5Bdelphi%5D You can also enable "Use Debug DCUs" and intercept the exception within VCL internals, there you'd be able to Evaluate or Inspect which object was tried to display: Self.ClassName and TComponent(Self).Name.
  • The HideDimmer and ShowDimmer methods were added for debugging to try to test several things among various occasions when the DimmerForm is shown and hidden, I'll be getting rid of them once the problems solved. I did try the DimmerForm.Hide; MainForm.Show; no joy. I believe that the issue is down to the newly created tab sheet. Removing the creation code stops the error occurring. The TTabSheet is fully created before the error occurs though which confuses me. Surely the error would happen in creation? None of its procedures are called (other than create) before the error occurs as well.
  • You are a genius, I was trying to focus on the DBGrid in the onCreate event, moving DBGrid.SetFocus to onShow solved the issue.
  • This answer is wrong, please delete it! If you get the error in the q title while posting an edit to a dataset, there is something else wrong with your code which is causing it. A dataset Post should never ever cause this error. Rather than try a quick solution, do some proper debugging and find the real cause of your problem!
  • Welcome to StackOverflow! However, this does not provide an answer to the question.You can search for similar questions, or refer to the related and linked questions on the right-hand side of the page to find an answer. If you have a related but different question, ask a new question, and include a link to this one to help provide context.
  • @toti08 It's an answer, but it had poor form. I've edited it.