Hello,

I have a question (might be pretty easy, but please help!!): on my application's main form I have two buttons: open that opens Form1 and another one that opens Form2. Now, from Form1 I need to open Form2, and from Form2 I need to open back Form1, and go back and forth. The forms have to be modal and open one from the other. The Form2 button opens Form1, that has a button that opens Form2 back.
I am having some trouble with opening and closing the windows.

Doing this seems bad:

//on Form1:
procedure TForm1.Button1Clck(Sender : TObject);
begin
Close;
Form2.ShowModal;
end;

//on Form2:
procedure TForm2.Button1Clck(Sender : TObject);
begin
Close;
Form1.ShowModal;
end;

Please help me, any advice is highly appreciated!

Pici

Recommended Answers

All 9 Replies

You cant work like that what logic is it that produces such an effect?

Yes, I realized I can't work with that logic.

What happens is when I open Form2 from Form1, Form1 stays open as well and does not close when I do 'close'. And when I want to reopen Form1 clicking button on Form2, both of them close in the same time.

I don't know how to do this right, any ideas?

Thanks.

OK the biggest problem is that every app has a "main form" and when you close it, you'll close the app.

You didnt mention closing the forms..
You can *hide* the forms not close them, that kinda works :) but you should be careful as to be frank thats not always a great idea, as well as forms (IMHO) should only be created when about to be used, not lurking silently.

OK. first of all I want the forms to be modal, so I'm using SHowModal and not Show.

So you're suggesting that when I open Form2 from Form1 I do:

procedure TForm1.Button1Click(Sender : TObject);
begin
visible := false;
Form2.ShowModal;
end;

and when I open Form1 from Form2 I do:

procedure TForm2.Button1Click(Sender : TObject);
begin
visible := false;
Form1.ShowModal;
end;

I get 'Cannot make a visible window modal', although I am doing Visible := false every time..

I guess I'm missing something, but what?

About creating and FreeAndNill-ing the forms..that's a different issue. I can not destroy a form when I click a button on that form.

do you have any other forms on your app that are visible during this process? if not, then your problem is you shouldnt make them modal

As for your comment about freeandnil - you dont appear to use any data back after the modal state, so I would disagree.

I have the application's main form visible all the time, from this I open Form1, and from Form1 I open Form2. I need to make my two forms modal because the application's main form is bigger than these two and is always visible. But using ShowModal and making the forms invisible gives the error in my last post. So what can I do?

These two forms go back and forth, from the first one I open the second, from the second I open the first one again and so on.

Then you need to make the app form open the modal forms not the form1 and form2. Then you can hide them as previously said you cant make a modal form loop it doesnt work - this is your main problem. Also if the form thinks its already visible it cant be changed to modal.

Hello,

I have a question (might be pretty easy, but please help!!): on my application's main form I have two buttons: open that opens Form1 and another one that opens Form2. Now, from Form1 I need to open Form2, and from Form2 I need to open back Form1, and go back and forth. The forms have to be modal and open one from the other. The Form2 button opens Form1, that has a button that opens Form2 back.
I am having some trouble with opening and closing the windows.

Doing this seems bad:

//on Form1:
procedure TForm1.Button1Clck(Sender : TObject);
begin
Close;
Form2.ShowModal;
end;

//on Form2:
procedure TForm2.Button1Clck(Sender : TObject);
begin
Close;
Form1.ShowModal;
end;

Please help me, any advice is highly appreciated!

Pici

To start with I don't recommend that you have all the forms you intend using open at all times. You chew memory like that.
Rather call the form as follows as and when you need it.

procedure TForm1.Button1Clck(Sender : TObject);
begin
frmForm1 := TFrmForm1.Create(nil);
Form1.ShowModal;
end;

procedure TForm1.Button1Clck(Sender : TObject);
begin
frmForm2 := TFrmForm2.Create(nil);
Form2.ShowModal;
end;

TAKE NOTE.
1.
In Project->Options->Forms You need to move Form1 and Form2 to :Available Forms.
2.
You need to go to the EventHandler of each form and include the following ..

procedure TfrmForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
end;

procedure TfrmForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
end;

3.
You may need to do this also ..

procedure TfrmForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
var Res: Integer;
begin
if YourDataModule.qryWhatever.UpdatesPending then
YourUpDateModule.UpDateqryWhatever;
end;

try this...

make the following routine available for your instigating button.

procedure OpenFormModal(dForm:Integer);
var
tForm:Integer;
begin
tForm:=dForm;
while (tForm>0) do
begin
case tForm of
1:
begin
Form1.ShowModal;
tForm:=Form1.Tag;
end;
2:
begin
Form2.ShowModal;
tForm:=Form2.Tag;
end;
end;
end;
end;

Notes:
Call this routine from the mainform to start the process. set dForm to either 1 or 2 depending on which form you want to open.

on the modal forms finish the close routine by setting the tag for the following results...

0=return to mainform
1=open form1
2=open form2

although i used tag for this example in a professional development i would probably of declared a public integer for this purpose.

i wish you success.

i have an issue, i show a form modal and when i close it, it opens up a second time for no apparent reason. however, a click to close a second time is successful. got any ideas?
at the mo my solution is to set a bit true when the form closes and when the form open routine is triggered a second time if this bit is set then it closes and resets the bit.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.