1,105,416 Community Members

opening a new form using a button

Member Avatar
HelpMeIT
Newbie Poster
15 posts since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Simply put i would like to open a new form as simply as possible using a button
i looked at this http://www.daniweb.com/forums/thread99518.html# but it confused me
i dont want any values passed on i just simply want it to hide the old form and open then new form can this be done?

Member Avatar
HelpMeIT
Newbie Poster
15 posts since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

And also if its possible a way to on the click of a button exit all parts of a project as im having problems when trying to destroy forms of the actual program still running with nothing to there to use

Member Avatar
BitFarmer
Junior Poster in Training
51 posts since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 0 [?]
 
1
 

As I understood: You have a form "A" with a button to open a form"B", and also form "A" must close itself automatically.

First, you need a Application.MainForm always present, it may be a menu, a wellcome splash (make it invisible, don't free it) but anyway you NEED a mainform.

Usually this is the only autocreate form in your app. preferences, no more autocreate forms are needed as you are going to create them by code (delphi tends to autocreate one form of each type, take them out of the autocreate list).

So, the unit "A" should be like this:

1) Add in the unit A uses the unit B.

2) On the buton click, you do like this (or similar):

with TMyFormB.create(Application.MainForm) do
Show;

3) Next you must close this form with a self.close;

4) Closing is not the same a freeing, so on MyFormA.OnClose you need to add a line with "Action:= caFree;". With that line, closing a form will really kill it from memory. Ah! Add thisa to all your forms if you what not to keep them on memory after closing them!

I think it is all... it also answer your second question about closing forms.

Member Avatar
HelpMeIT
Newbie Poster
15 posts since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks for the help i have managed to get the forms to open but i cannot work out how to completely remove it from the memory with the .onclose bit where exactly should i put it? do i create a procedure?

Member Avatar
BitFarmer
Junior Poster in Training
51 posts since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 0 [?]
 
0
 

It should be placed on the MyForm.FromClose event, so closing (not being show in the screen) equals freeing the object:

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

The idea is that Creating is the oposite to Freeing the form, but closing is not, closing is the opposite to Showing the form. When user close the form, it is not shown, but it is not freed: You could make it visible again if you had a pointer to the form stored.

Member Avatar
HelpMeIT
Newbie Poster
15 posts since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I have now got that to work and it does close it but only when i exit with the cross in the top corner, i would like to be able to use a button. i have tried a couple of ways to close the form but none have worked is their an easy way to do this?

Member Avatar
BitFarmer
Junior Poster in Training
51 posts since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 0 [?]
 
0
 

If form is modal, just make ModalResult:= mrOK (or place a button of kind=ok, it is allready made in the standard buton properties).

If the form is not modal, just make "close" and that is all.

Member Avatar
finalist
Junior Poster in Training
89 posts since Dec 2009
Reputation Points: 1 [?]
Q&As Helped to Solve: 15 [?]
Skill Endorsements: 0 [?]
 
0
 
procedure TForm1.Button1Click(Sender: TObject);
begin
  Visible := False; // Makes Form1 invisible
  try
    Form2.ShowModal; // Shows the Form2
  finally
    Visible := True; // Makes Form1 visible again
  end;
end;
Member Avatar
House_of_Dexter
Newbie Poster
21 posts since Oct 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

If you have a button that should close the form...just add this to it...
Also look into using ActionList and use the execute...you can tie lots of code into one common action.

procedure TForm1.btnCloseClick(Sender: TObject);
begin
   Form1.Close;
//this will call your forms FormClose...which will call your caFree.
end
Question Answered as of 4 Years Ago by BitFarmer, House_of_Dexter and finalist
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: