Simply put i would like to open a new form as simply as possible using a button
i looked at this 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?

Recommended Answers

All 8 Replies

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

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

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.

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?

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);
  Action:= caFree;

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.

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?

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.

procedure TForm1.Button1Click(Sender: TObject);
  Visible := False; // Makes Form1 invisible
    Form2.ShowModal; // Shows the Form2
    Visible := True; // Makes Form1 visible again

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

procedure TForm1.btnCloseClick(Sender: TObject);
//this will call your forms FormClose...which will call your caFree.
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.