Solved

eternal OnShow chaining problem

Posted on 2002-06-09
24
1,302 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 6
  • 3
  • +3
24 Comments
 
LVL 4

Expert Comment

by:nestorua
ID: 7065404
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
ID: 7065408
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
ID: 7065416
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:boc7900
ID: 7065440
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
ID: 7065448
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
ID: 7065454
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
ID: 7065525
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
ID: 7065533
HI,
Handle is the Handle of the FormA - exactly as it' written.
Sincerely,
Nestorua.
0
 

Author Comment

by:boc7900
ID: 7065600
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
ID: 7065609
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
ID: 7065630
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
ID: 7065692
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
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 7065901
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
ID: 7065938
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
ID: 7066166
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
ID: 7066656
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
ID: 7068020
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
ID: 7068433
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
ID: 7068489
I'm going to try FFons:=TFFons.Create(Application) and I will comment the result. Thank you Lee!! :)
0
 

Author Comment

by:boc7900
ID: 7068556
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
ID: 7068640
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
ID: 7068788
also, what's in the OnShow of your FFons?
0
 
LVL 7

Accepted Solution

by:
Cynna earned 200 total points
ID: 7069382
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
ID: 7071067
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month7 days, 11 hours left to enroll

632 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