Lainkes 0 Posted March 20 Hello, I have a form with a button. When the button is clicked, a new form opens to input data. So when this button is clicked, I need to close the current form with the button. This is my code : Quote // OPEN INPUT SCREEN FOR BOR DOSSIER try inputCheckForm := Tfrm_InputCheck.Create(Self); try inputCheckForm.ShowModal; finally inputCheckForm.Free; Close; // CLOSE THE CURRENT INPUT FORM end; except on E: Exception do DoShowException(E); end; I have put the Close command in the Finally section. But that's not working. I thought that was the right place, so that even in case of an error the screen is closed. Maybe I'm thinkin wrong. Thanks for your feedback. Alain Share this post Link to post
Lajos Juhász 293 Posted March 20 The correct code would be: // OPEN INPUT SCREEN FOR BOR DOSSIER try inputCheckForm := Tfrm_InputCheck.Create(Self); try inputCheckForm.ShowModal; finally inputCheckForm.Free; Release; // CLOSE THE CURRENT INPUT FORM end; except on E: Exception do DoShowException(E); end; https://docwiki.embarcadero.com/Libraries/Athens/en/Vcl.Forms.TCustomForm.Release Share this post Link to post
Uwe Raabe 2059 Posted March 20 The question is: Do you want the current form to close before you have entered the data in the new one or after that? Share this post Link to post
Lainkes 0 Posted March 20 Thanks. This works as the called form is closed. So this works fine. My program starts with a small login screen where user credentials are needed. When the login is succesful the main form appears. But the login screen is still visible behind the mainform until the mainform is closed. How can I close the login screen directly when the main form is showed. // SHOW MAIN FORM try mainkForm := Tfrm_Main.Create(Self); try mainkForm.ShowModal; finally mainkForm.Free; Release; // CLOSE CURRENT LOGIN FORM end; except on E: Exception do DoShowException(E); end; Share this post Link to post
Lajos Juhász 293 Posted March 20 23 minutes ago, Lainkes said: My program starts with a small login screen where user credentials are needed. When the login is succesful the main form appears. But the login screen is still visible behind the mainform until the mainform is closed. How can I close the login screen directly when the main form is showed. You should code this in different way. You can find an example at https://www.thoughtco.com/display-a-login-password-dialog-1058469 unfortunately the code formatting is not correct but you can see the idea. Share this post Link to post
Lainkes 0 Posted March 20 I add a screenshot of my form settings. So it's more clear. (I hope) Share this post Link to post
Remy Lebeau 1403 Posted March 20 (edited) 2 hours ago, Lajos Juhász said: The correct code would be: // OPEN INPUT SCREEN FOR BOR DOSSIER try inputCheckForm := Tfrm_InputCheck.Create(Self); try inputCheckForm.ShowModal; finally inputCheckForm.Free; Release; // CLOSE THE CURRENT INPUT FORM end; except on E: Exception do DoShowException(E); end; https://docwiki.embarcadero.com/Libraries/Athens/en/Vcl.Forms.TCustomForm.Release Release() destroys the Form that it is called on. Whereas Close() merely closes (hides) the Form, allowing it to be reopened (shown) again at a later time if desired. Edited March 20 by Remy Lebeau Share this post Link to post
Lajos Juhász 293 Posted March 20 3 minutes ago, Remy Lebeau said: Whereas Close() merely closes the Form, allowing it to be reopened at a later time if desired. Close can cause access violation if the form is prematurely freed. Release makes sure that every event is finished before the form is freed. Share this post Link to post
Remy Lebeau 1403 Posted March 20 (edited) 5 minutes ago, Lajos Juhász said: Close can cause access violation if the form is prematurely freed. Close() doesn't free the Form unless you explicitly ask for that behavior in its OnClose event (and even that, that logic uses Release() internally). In 20+ years using Delphi, I've never run into an AV from closing a Form. If you are getting an AV when closing a Form then you are likely mismanaging your code that uses the Form. Edited March 20 by Remy Lebeau Share this post Link to post
dummzeuch 1506 Posted March 20 Closing the main form of an application terminates that application and thereby also frees that form and all other forms. The main form of an application is defined as the first form it creates and that cannot be changed easily. So, creating a login form as the first form, has the side effect of terminating the application as soon as it gets closed. which is usually not desirable. Therefore such a login form should never be the first form an application creates. (Unless something has been changed over the years and my experience is outdated (again).) Share this post Link to post
Lajos Juhász 293 Posted March 21 15 hours ago, dummzeuch said: The main form of an application is defined as the first form it creates and that cannot be changed easily. The main form is the first form created using Application.CreateForm you can create forms before using the normal form constructor. Share this post Link to post
Stano 143 Posted March 21 I've dealt with this in the dpr unit. I left things around in the demo as well. Maybe they will inspire you in something program MyProgram; {$R *.dres} uses ... subBillingRestEnBloc in 'SubForms\Settlement\Edit billing\subBillingRestEnBloc.pas' {frmsubBillingRestEnBloc}; var LogonForm: TfrmLoginForm; {$R *.res} begin ReportMemoryLeaksOnShutDown := True; Application.Initialize; Application.MainFormOnTaskbar := True; Application.CreateForm(TdtmdBasic, dtmdBasic); oAppEnv := TAppEnv.Create; Certificate := TCertificate.Create; oGlobVar := TGlobalVar.Create(Certificate); try Licence := TLicence.Create; try Licence.CheckUp; try LogonForm := TfrmLoginForm.Create(nil); except Application.Terminate; Exit; end; if LogonForm.ShowModal = mrOK then begin FreeAndNil(Licence); SupObjJson := TSupObjJson.Create; oAppearance := TAppearance.Create(nil); oAppearance.ReadOptions; if oAppearance.IsTMSStyle then oAppearance.SetStylers(oAppearance.StyleName) else begin TStyleManager.TrySetStyle(oAppearance.VclStyle); oAppearance.UpdateToolBarPager; end; IdentificationData := TIdentificationData.Create; try IdentificationData.Make; finally FreeAndNil(IdentificationData); end; Application.CreateForm(TfrmMainFormFOC, frmMainFormFOC); frmMainFormFOC.Position := poDesigned; SupObjJson.ReadForm(frmMainFormFOC); frmMainFormFOC.Show; Application.Run; end else begin FreeAndNil(LogonForm); FreeAndNil(oGlobVar); FreeAndNil(oAppEnv); end; finally FreeAndNil(Licence); end; finally FreeAndNil(Certificate); end; end. Share this post Link to post