Board index » delphi » 'Cannot change visible in OnShow or OnHide' error

'Cannot change visible in OnShow or OnHide' error

I have a simple form with a memo that I update with while
bringing up my application (This all occurs prior to
Application.run)

Usually, there are no issues, but sometimes, I get an exception:
Einvalid Operation - 'Cannot change visible in OnShow or OnHide'

I have no event handlers set for either of these events.  I get
this error in this code (again, only sometimes - have not been
able to determine the scenario):

Procedure ShowMsg( s : string);
begin
  if not assigned(fMsgWnd) then
    fMsgWnd := tfMsgWnd.create(application);
  fMsgWnd.memoStatus.lines.add(s);
  fMsgWnd.Show;   <= exception is generated here.
  Application.ProcessMessages;
end;

For a test I called .show twice to ensure it was safe to do so
while it was showing and it worked so why would it be raising an
exception occasionally?

What can I check to see if it is already showing before calling
show? (Or do I need to keep track of it myself?)

Thanks,
-Eric

 

Re:'Cannot change visible in OnShow or OnHide' error


Quote
In article <4332ae2...@newsgroups.borland.com>, Eric wrote:
> I have a simple form with a memo that I update with while
> bringing up my application (This all occurs prior to
> Application.run)

> Usually, there are no issues, but sometimes, I get an exception:
> Einvalid Operation - 'Cannot change visible in OnShow or OnHide'

> I have no event handlers set for either of these events.  I get
> this error in this code (again, only sometimes - have not been
> able to determine the scenario):

> Procedure ShowMsg( s : string);
> begin
>   if not assigned(fMsgWnd) then
>     fMsgWnd := tfMsgWnd.create(application);
>   fMsgWnd.memoStatus.lines.add(s);
>   fMsgWnd.Show;   <= exception is generated here.
>   Application.ProcessMessages;
> end;

> For a test I called .show twice to ensure it was safe to do so
> while it was showing and it worked so why would it be raising an
> exception occasionally?

No idea. The exception is created if some code executing in response to
a CM_SHOWINGCHANGED message tries to show or hide the form. You
Application.ProcessMessages call opens the doors for such madness <g>.
Try to call the forms Update method instead.

Quote

> What can I check to see if it is already showing before calling
> show? (Or do I need to keep track of it myself?)

Given the code snipped above that is trivial: you only need to show the
window if you just created it. Or test the Showing property. Or use the
same test TCustomForm.CMShowingChanged uses to fire off the error:

  if not (fMsgWnd.Showing or (fsShowing in fMsgWnd.FormState))
  then begin
    fMsgWnd.Show;
    fMsgWnd.Update;
  end;  

--
Peter Below (TeamB)  
Use the newsgroup archives :
http://www.mers.com/searchsite.html
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be

Re:'Cannot change visible in OnShow or OnHide' error


"Peter Below (TeamB)" <100113.1...@compuXXserve.com> wrote:

Quote
>In article <4332ae2...@newsgroups.borland.com>, Eric wrote:
>Application.ProcessMessages call opens the doors for such madness <g>.

Yes, I think this is the source of my woes.  

This form was originally only used prior to application.run and
so needed the application.processesmessages in order to see the
updates while creating other startup stuff.  However, I have
started using it in other areas and I believe that is why
I am now seeing the issue.

I have set a boolean directly before application.run and do not
call application.processmessages if it is set.  So far, I have not
seen any issues.

-Eric

Other Threads