I'm having trouble closing Form1 and opening Form3 through a button click.
I have tried this code:
#include "Welcome.h"
#include "Login.h"
void __fastcall TForm1::LoginButtonClick(TObject *Sender)
{
TForm1 *CurrentForm = new TForm1(this);
CurrentForm->Close();
TForm3 *NewForm = new TForm3(this);
NewForm->Show();
}
This is the part of the code that is not working:
TForm1 *CurrentForm = new TForm1(this);
CurrentForm->Close();
What can I change to make Form1 close upon the button click?
Get rid of
CurrentForm. You are creating a whole new Form object just to close it immediately. You need to instead callClose()on the Form object that the button belongs to. And since theOnClickhandler is a method of that Form, you can simply use the method'sthispointer to access the Form object, eg:Just be aware that
TForm1is usually the default name of theMainForm, and if youClose()theMainFormthen your app will exit. If you need to "close" theMainFormwithout exiting the app, you canHide()theMainForminstead.