Page 1 of 1

can not focus a disabled or invisible window

Posted: Sat 03 Sep 2011 20:55
by inageib
Hi
I have a required filed in my table and when I dont type any thing in that filed and try to post I get this error "can not focus a disabled or invisible window" instead of exception dialog for null value for required filed

when I unchecked the required fields property the normal exception appear again !! why is that ?

thanks

Posted: Mon 05 Sep 2011 12:06
by AndreyZ
Hello,

The "Cannot focus a disabled or invisible window" exception is raised first, that's why you don't see the exception about NULL value. The "Cannot focus a disabled or invisible window" exception is raised in the TCustomForm.SetActiveControl procedure of the Forms unit. It occurs when your application is trying to make an invisible or disabled control active (for example, by calling the SetFocus method). Please check where your application does this, and you will find the cause of this problem.

Posted: Thu 08 Sep 2011 21:26
by inageib
Thanks but I dont use setfocus I only read current focused control maybe that execute getfocus method and cause the problem ??

also why when I disable the option "requiredfields" I can see the normal exception of required fields ?

Thanks

Posted: Fri 09 Sep 2011 12:46
by AndreyZ
I cannot reproduce the problem. Please try creating a small sample to demonstrate this problem and send it to andreyz*devart*com, including a script to create and fill a table. Also please specify the following:
- the exact version of your IDE;
- the exact version of IBDAC. You can learn it from the About sheet of TIBCConnection Editor.

Posted: Sat 10 Sep 2011 03:08
by inageib
Thanks but I can not isolate the problem otherwise I would solve it myself

Is there any other situations than setfocus can cause this ?

Posted: Mon 12 Sep 2011 10:50
by AndreyZ
You can pinpoint the problem in the following way:
- enable the "Use Debug DCUs" option in Project-Options;
- set a breakpoint in the Forms unit on the following line: raise EInvalidOperation.Create(SCannotFocus);
This way you can find the reason of the "Cannot focus a disabled or invisible window" error.

Posted: Mon 12 Sep 2011 15:17
by inageib
Thanks alot for your advise. I did not face any kind of weird problem like that before

I did the steps you mentioned and I found two raise exceptions in the forms unit I break on both

when I got the error I saw in call stack that the error begin after calling the "post" method and as I said this because I was trying to save a new added record with a "not null" field set to null ! so the software was trying to popup the required exception message

then I looked at the code that launch the form which I set the border to bsdialog:

Code: Select all

  with myDM do begin
    table1.Append;
    myfrm := Tmyfrm.Create(self);
    if myfrm.ShowModal = mrOk then begin
      if table1.State in dseditmodes then
        table1.Post;

      if table2.State in dseditmodes then
        table2.Post;

      table1.Refresh;
    end
    else table1.Cancel;
  end;
then I try that:
I comment all the above code until line myfrm.ShowModal and comment the check for mrok and I put the call to post method in the form save button and the call to cancel method in the form cancel button

when I run I got the normal required exception dialog without getting "can not focus a disabled or invisible window" !!

any explanation why that happen ??

Posted: Tue 13 Sep 2011 09:00
by AndreyZ
I've reproduced the problem. This problem is caused by the TDataSet.CheckRequiredFields method in the DB unit. This method calls the FocusControl method that eventually causes the "Cannot focus a disabled or invisible window" error. The approach you are using is incorrect, it can cause different unexpected problems. You should move your code that works with table1 to the Tmyfrm form. Here is an example:

Code: Select all

with myDM do begin
  table1.Append;
  myfrm := Tmyfrm.Create(self);
  myfrm.ShowModal;
end;

procedure Tmyfrm.btnSaveClick(Sender: TObject);
begin
  if myDM.table1.State in dseditmodes then
    myDM.table1.Post;

  if myDM.table2.State in dseditmodes then
    myDM.table2.Post;

  myDM.table1.Refresh;
  ModalResult := mrOK;
end;

procedure Tmyfrm.btnCancelClick(Sender: TObject);
begin
  myDM.table1.Cancel;
  ModalResult := mrCancel;
end;

Posted: Tue 13 Sep 2011 12:08
by inageib
Thanks alot AndreyZ You are wonderful man

I understand now thanks to you but I remember since Delphi 2.0 many examples use ShowModal = mrok with post method !!

howeer your code in previous post is exactly what I use now except I dont check for mrok any more no point since I put he logic in the form save and cancel buttons.

Thanks again I appreciate your help[/code]

Posted: Tue 13 Sep 2011 13:10
by AndreyZ
I am glad I could help. If you have any other questions, please do not hesitate to contact us.