Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form1 to Form2

Status
Not open for further replies.

Andy230

Technical User
Oct 11, 2007
5
0
0
BE
Hi, I am a nemwbe to Delphi and I am trying out small programs to get acquainted.
Now I just wonder how you switch from Form1 to Form2.

I have this coded in Form1:
procedure TForm1.Button1Click(Sender: TObject);
begin
Form1.Close;
Form2.Show;
end;

Compiling is OK, but when I run the program, nothing happens. Only Form1 shows, when clicking on the Button1
nothing happens. What is wrong?

Next, how can I make available an array defined in Form1 to be in the scope of Form2.
 
Are both forms created when the program starts?
If you are not sure what I am talking about, then they probably are since that is the default.
(Can find out in menu 'Project'->'Options' and then the first selection 'Forms', at least thats where it is in BDS2006).

Also, try showing Form2 before closing Form1.

At the very least with that setup I would expect the first form to disappear. Sounds a little odd that it would sit there and nothing happens. Then again, a few of the others .. as in most, know far more than I about Delphi. I am still young in this area.

~
Give a man some fire, he will be warm for a day, Set a man on fire, he will be warm for the rest of his life.
 
Oh, another thing you could double check, is that the button points to the event.
In the design of Form1 click on the button, look at its Events and just make sure that its 'Click' even points to 'Button1Click'.

~
Give a man some fire, he will be warm for a day, Set a man on fire, he will be warm for the rest of his life.
 
@Opieo Both forms were created at the start. The button acts on a click because the procedure name is Button1.Click....
the button points to a click event. Switching the close and open instructions don't help.
 
The event doesn't automatically point to the procedure just because you named it Button1Click, you know. Check your events for Button1 to make sure the OnClick event is assigned that procedure.

Lee
 
Yes I checked the event and it is OnClick.
 
Why not put a showmessage in the event handle to make sure that the event is being triggered ??
 
Yes, I inserted following:
Button1.caption := 'next form';

and yes it showed very quickly, because Delphi is going back to edit mode instead of showing form2.
 
I think you want to hide Form1, not close it because it is the main form.

from the help

Closes the form.

procedure Close;

Description

Call Close to close a form.

Close calls the CloseQuery method to determine if the form can close. If CloseQuery returns False, the close operation is aborted. Otherwise, the OnClose event occurs.

Note: When the main form of the application closes, the application terminates.
 
I have this coded in Form1:
procedure TForm1.Button1Click(Sender: TObject);
begin
Form1.Close;
Form2.Show;
end;
Change it to this:
Code:
procedure TForm1.Button1Click(Sender: TObject);
begin
  (* Form1.Close; no-no THIS CLOSES YOUR APP! *)
  //Hide {optional - remove "//" to activate}
  Form2.ShowModal;
  //Show; {optional if you use "Hide" - remove "//" to activate}
end;
Remember, with "showModal", form2 has to be closed (exit) to return control to form1, whether you used "Hide" & "Show" or not. The only difference is, if you don't hide form1, it still cant be selected. You CAN if you use "Form2.Show" but it will confuse your user. ShowModal is best because of this.

Play with it by remving the "//" comments to see what I mean.

GOOD LUCK and happy coding!

Roo
Delphi Rules!
 
And the last possible thing that should make Zero difference right now, is the Uses.
Form2 should be in the Uses of Form1.
However, Delphi will technically give an error when compiling and ask if you want to add it, to which you would have to reply yes, and Delphi would add it for you.
If by some odd quirk of fate that didn't work, Form2 would need to be added to the uses of Form1.

~
Give a man some fire, he will be warm for a day, Set a man on fire, he will be warm for the rest of his life.
 
Opieo - He already said it "Compiled OK", so what is you point?
 
I changed the procedure, but get an error on compile:
"formchange1.pas, missing operator or semicolon"

placing a semicolon after Hide gives me 5 other errors.

procedure TForm1.Button1Click(Sender: TObject);
begin
(* Form1.Close; no-no THIS CLOSES YOUR APP! *)
Hide {optional - remove "//" to activate}
Form2.ShowModal;
Show; {optional if you use "Hide" - remove "//" to activate}
end;
 
Code:
procedure TForm1.Button1Click(Sender: TObject);
begin
  Form1.Hide;
  Form2.ShowModal;
  Form1.Show;
end;

try that :)
 
Andy230, You've missed the semi-colon from the 'Hide' line.

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top