Solved

eternal OnShow chaining problem

Posted on 2002-06-09
24
1,256 Views
Last Modified: 2007-12-19
I only want FormA to show FormB when/after FormA is shown. As you know, using FormA's onShow event causes a "cannot change Visible in OnShow or OnHide".

Using onPaint,onActivate or onCreate events to call FormB.show gives the same error.

I tried to call FormB's show event before application.run in project.dpr -> same error.

I also have tried with Timer events -> same error despite of how many seconds it waits.

Why Delphi guys don't let programmers do this?. The same Delphi shows multiple forms on startup: Object Inspector,main menu and toolbar,... :[[

I hope that such a basic feature must have an easy and smart way to be implemented (i.e.: afterShow event). I don't want to create threads or complex experiments like the ones I found on similar questions here.

I will give 150+ points if the answer is excellent.
0
Comment
Question by:boc7900
  • 10
  • 6
  • 3
  • +3
24 Comments
 
LVL 4

Expert Comment

by:nestorua
Comment Utility
HI, boc7900,
If I understood you right, the possible solution is the following (the shortest I know by this moment):

procedure TFormA.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  FormB.Show;
end;

procedure TFormA.FormShow(Sender: TObject);
begin
 PostMessage(Handle, WM_LBUTTONDOWN, 0, 0);
 PostMessage(Handle, WM_LBUTTONUP, 0, 0);
end;
 Although, to my mind more reasonable is to create a new Message and override TFormA.WndProc method.
(this solution is very simple as well).
Sincerely,
Nestorua.
0
 
LVL 12

Expert Comment

by:Lee_Nover
Comment Utility
umm that happens when you call Show or Hide of the same form that's being shown or hidden


this works just fine :


uses unit2;

procedure TForm1.FormShow(Sender: TObject);
begin
     Form2.Show;
end;
0
 
LVL 3

Expert Comment

by:marcoszorrilla
Comment Utility
Try this example, we create form2 from form1, form2 is available not in autocreate Mode.

Put a timer in Form1 and programm the timer to create the form 2

Procedure CreaFormulario;
begin
Application.CreateForm(Tform2, form2);
form2.Show;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
creaformulario();
Timer1.Enabled :=False;// to avoid a second creation of the form
end;

Best Regards.
Marcos.
0
 

Author Comment

by:boc7900
Comment Utility
Very interesting Nestoura, I suppose that with PostMessage you trigger a mouseDown (which shows FormB) and mouseUp event (to not call show again). But what should I pass as Handle? (note that all I know of PostMessage is what you have typed :) ).

About creating a new message, wouldn't that requiere a new event too?
0
 

Author Comment

by:boc7900
Comment Utility
Lee Nover, your example not only crush but the exception it gives is the essence of the problem I want to solve. :)
0
 

Author Comment

by:boc7900
Comment Utility
Marcos, I have tried an example like yours but without Application.CreateForm(Tform2, form2); and it gave the same error ("cannot change..."). I don't think that creating the form before showing it will help.

Thanks anyway. :)
0
 
LVL 12

Expert Comment

by:Lee_Nover
Comment Utility
boc7900 then your code is flaw
you must do something wrong in your code
or you didn't read carefully what I wrote
you CAN call any forms Show in another forms OnShow or OnHide event
but you CANNOT call Show or Hide in the form that is being Shown or Hidden

you could also create the other form with:

Form2:=TForm2.Create(Application);
0
 
LVL 4

Expert Comment

by:nestorua
Comment Utility
HI,
Handle is the Handle of the FormA - exactly as it' written.
Sincerely,
Nestorua.
0
 

Author Comment

by:boc7900
Comment Utility
Nestorua, I've executed your proposal step by step and it gives the exception "Cannot change Visible in onShow or onHide" when the program executes the line FormB.Show;
despite that it isn't (aparently) in any OnShow or OnHide.

Thanks

0
 

Author Comment

by:boc7900
Comment Utility
Lee Nover, when I call anotherForm.Show from the current form OnShow event, Delphi gives me the exception "Cannot change Visible in onShow or onHide". Don't ask me why.

And it also gives the same exception if I call anotherForm.Show; from OnCreate,OnPaint,... of the current form. So it is impossible to me to show formB sequentially after showing formA.
0
 
LVL 4

Expert Comment

by:nestorua
Comment Utility
HI,
I don't know what you are doing to get those exceptions.
Let the FormA be your main form and it's Visible property =True, the FormB be autocreate form and it's Visible property is False. Then all that I proposed must work fine (it does on my comp).
Sincerely,
Nestorua.
0
 

Author Comment

by:boc7900
Comment Utility
Sorry Nestoura,with "autocreate form" do you mean a form created from project.dpr? If you mean that, then this is exactly how I have it all.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 12

Expert Comment

by:Lee_Nover
Comment Utility
well I have D6 and I don't get that error
as it doesn't make sence why changing another forms visible state would raise an error
can you paste your exact code ?
0
 
LVL 12

Expert Comment

by:Lee_Nover
Comment Utility
btw .. you want to show both forms at startup ?

then you can simply do:

// in the project file

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.CreateForm(Tform2, Form2);
  Form.Show;
  Application.Run;
end;

:)
0
 
LVL 14

Expert Comment

by:DragonSlayer
Comment Utility
boc7900, I think you have some code in your Form2's OnShow that is causing all these... try commenting out the code of your Form2's OnShow and I believe you can call Form2.Show from Form1's OnShow.
0
 
LVL 7

Expert Comment

by:Cynna
Comment Utility
boc7900,

This error means that you are trying to show form that can't be shown. It has nothing to do with OnShow event itself, despite exception message.  This can happen for a number of reasons: your app is using MDI layout and Form1 is shown as modal, Form2 is in process of creation or destruction, Form2 is in process of Showing/Hiding (it's Visible property is already changing, during which you try to change it to some 'hardcoded' value)...

Write bare-bones program (clean application with two empty fsNormal forms, and *only* line of code is 'Form2.Show' in OnForm1Show event). Error shouldn't occur.
If it still does, try non-elegant TTimer method.
If it's still there, something is very wrong with your system.

My (and, it seems, all of the others) guess is that bare- bones test app should work OK - that is, error is caused by other piece of code, that you overlooked. If it works OK, tell us and we can work from there as base ground...
0
 

Author Comment

by:boc7900
Comment Utility
You are right, it doesn't happen in a bare-bones project. But it still fails in my project, perhaps because it has to create more forms and to initialize them all on startup.

//Main-form's onShow event:
procedure TFPrincipal.FormShow(Sender: TObject);
begin
 inherited;
 Inicialitza;
end;

//procedure called by onShow;
procedure TFPrincipal.Inicialitza;
begin
 inherited;
 Usuari:='';
 TBPrincipal.Height:=24;
 Application.CreateForm(TFFons, FFons);
 FFons.Show; //exception "cannot change Visible..."
end;

//project.dpr source code:
  Application.Initialize;
  FInicialitza := TFInicialitza.Create(Application);
  FInicialitza.Show;
  FInicialitza.Update;
  Application.CreateForm(TFPrincipal, FPrincipal);
  Application.CreateForm(TFModulDades, FModulDades);
  Application.CreateForm(TFInicialitza, FInicialitza);
  Application.CreateForm(TFIdentitat, FIdentitat);
  Application.CreateForm(TFCartera, FCartera);
  Application.CreateForm(TFOperacio, FOperacio);
  FInicialitza.Hide;
  FInicialitza.Free;
  ServeisG:=TServeis.Create('BDProjecte');
  try
   Application.Run;
  finally
   ServeisG.Free;
  end;

Thanks to all of you for your help.
0
 
LVL 12

Expert Comment

by:Lee_Nover
Comment Utility
why do you have inherited; there ?
did you override those procedures ? if not then remove those

Application.CreateForm(TFFons, FFons);
FFons.Show; //exception "cannot change Visible..."

with this you set the FFons as your MainForm !! because it is the first form that is created with Applicat.CreateForm !!!
so it will be shown without calling .Show

if you don't want it to be your mainform then create it like :
FFons:=TFFons.Create(Application);
otherwise simply remove .Show because it will be shown anyway as it is your mainform

another thing would be to set Application.ShowMain:=false; just after you call Application.Initialize;
then you have to call FFons.Show; manually as you have it now
0
 

Author Comment

by:boc7900
Comment Utility
I'm going to try FFons:=TFFons.Create(Application) and I will comment the result. Thank you Lee!! :)
0
 

Author Comment

by:boc7900
Comment Utility
Hi!
I've tried it and got the same exception. When calling FFons.Show:

procedure TFPrincipal.Inicialitza;
begin
 inherited;
 Usuari:='';
 TBPrincipal.Height:=24;
 //Application.CreateForm(TFFons, FFons);
 FFons:=TFFons.Create(Application);
 FFons.Show; //exception
end;

Then I tried this:

procedure TFPrincipal.Inicialitza;
begin
 inherited;
 Usuari:='';
 TBPrincipal.Height:=24;
 Application.CreateForm(TFFons, FFons); //exception
 //FFons:=TFFons.Create(Application);
 //FFons.Show;
end;

I haven't tried ShowMainForm:=False

About the inherited, i haven't used override because I'm inheriting from non-virtual procedures, and it executes well the inherited code, I'm sure that the problem doesn't come from ancestors. I think that the problem is in FFons.Show in which I have no code and so I haven't post it, but its parent class has code:

//this code is reached steping inside FFons.Show with F7
procedure TFBackground.FormShow(Sender: TObject);
begin
 Inicialitza;
end;

//this code executes step by step perfectly but at the end
//gives the exception
procedure TFBackground.Inicialitza;
begin
 width:=screen.Width;
 top:=70;
 height:=screen.Height-100;
 WindowState:=wsMaximized;
 BorderStyle:=bsNone;
 //exception at finish
end;

What is wrong? :o
0
 
LVL 12

Expert Comment

by:Lee_Nover
Comment Utility
if you don't override then there's nothing to inherit :)

just try it without .Show :)
if it doesn't show the form then create it in the project file and use .Show after you create it

 Application.Initialize;
 FInicialitza := TFInicialitza.Create(nil); // later on you free it yourself so set it's owner as nil
 FInicialitza.Show;
 FInicialitza.Update;
// I'm guessing you have it this way coz you don't want FInicialitza as your main form - a splash screen readin from it's name :)
 // your mainform
 Application.CreateForm(TFPrincipal, FPrincipal);
 // create your FFons form here
 Application.CreateForm(TFFons, FFons);
 FFons.Show; // this really odda work if you remove all the .Create and .Show from the Inicialitza procedure
 Application.CreateForm(TFModulDades, FModulDades);
 Application.CreateForm(TFInicialitza, FInicialitza);
 Application.CreateForm(TFIdentitat, FIdentitat);
 Application.CreateForm(TFCartera, FCartera);
 Application.CreateForm(TFOperacio, FOperacio);
 FInicialitza.Hide;
 FInicialitza.Free;
 ServeisG:=TServeis.Create('BDProjecte');
 try
  Application.Run;
 finally
  ServeisG.Free;
 end;


this should work ...
0
 
LVL 14

Expert Comment

by:DragonSlayer
Comment Utility
also, what's in the OnShow of your FFons?
0
 
LVL 7

Accepted Solution

by:
Cynna earned 200 total points
Comment Utility
boc7900,

> What is wrong? :o

Exception was rised by:

  BorderStyle:=bsNone;

Comment it out and it should work.


Explanation:
-----------

When you change Forms BorderStyle, OnFormShow of that form is triggered. What you do is changing it from inside OnFormShow, effectively calling OnFormShow from OnFormShow. This is not allowed - recursion in window showing doesn't seem like a good idea, right?

Solutions:
-----------

1. Simple:  Change BorderStyle somewhere out of the OnShow event (for example, OnCreate would be fine)

2. Perverse: If you really, really want to keep it inside OnFormShow, you can trick Delphi:

procedure TFBackground.FormShow(Sender: TObject);
var ToInclude: Boolean;
begin
  ToInclude:=fsShowing in FFormState;
  Exclude(FFormState,fsShowing);
   Inicialitza;
  if ToInclude then Include(FFormState,fsShowing);
end;

0
 

Author Comment

by:boc7900
Comment Utility
Impressive, you got it Cynna. That was because I could not call FFons.Show from anywhere. As I promised 150+ points for an exellent answer, here you have these 200. :) Thanks to all of you for your interest in this question!!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now