I am using C++Builder with a VCL Forms Application. I am trying to close a VCL Form that is docked on a TPageControl. My Close button is on a toolbar on the program's main form. My approach to do this is the three steps below: I can step through all this code but when it is done nothing happens, the form is not closed. What am I doing wrong here?
- When a docked form is clicked I save the form name to global variable.
- When the CloseButton is clicked, I use Screen->Forms[] to loop through all forms and find the correct form. Then I call the event form->OnCloseQuery.
- In the FormCloseQuery event I call the FormClose event.
.
void __fastcall TAboutForm::FormClick(TObject *Sender)
{
MainForm1->LastSelectedFormName = AboutForm->Name;
}
void __fastcall TMainForm1::CloseButtonClick(TObject *Sender)
{ //Identify The Form to Delete by Name
bool q=true;
UnicodeString FormName="";
int cnt = Screen->FormCount;
for(int i=0; i<cnt; i++ )
{
TForm* form = Screen->Forms[i];
FormName = form->Name;
if(CompareText(FormName, LastSelectedFormName)==0){
form->OnCloseQuery(form, q); //close this form
break;
}
}
}
void __fastcall TAboutForm::FormCloseQuery(TObject *Sender, bool &CanClose)
{
int Code = Application->MessageBox(L"Close Form", L"Close Form", MB_YESNO|MB_ICONINFORMATION);
if(Code ==IDYES){
TCloseAction Action = caFree;
FormClose(Sender, Action);
}
}
void __fastcall TAboutForm::FormClose(TObject *Sender, TCloseAction &Action)
{
Action = caFree;
}
Below is an edit after reading the answer from Spektre
Calling form->OnClose(form, MyAction); will not trigger the FormCloseQuery event. I have to call FormCloseQuery manually. The only way I could get the docked form to close is by adding, delete Sender; to the FormCloseQuery.
This does not look like a correct solution. I am very surprised Embarcadero does not have a recommended way to close a docked form. This seems like a very common action. I read the doc-wiki and can not find any solution to close a docked form.
void __fastcall TMainForm1::CloseButtonClick(TObject *Sender)
{ //Identify The Form to Delete by Name
bool MyCanClose=true;
UnicodeString FormName="";
TCloseAction MyAction = caFree;
int cnt = Screen->FormCount;
for(int i=0; i<cnt; i++ )
{
TForm* form = Screen->Forms[i];
FormName = form->Name;
if(CompareText(FormName, LastSelectedFormName)==0){
// form->OnClose(form, MyAction);
form->OnCloseQuery(form, MyCanClose);
break;
}
}
}
void __fastcall TAboutForm::FormCloseQuery(TObject *Sender, bool &CanClose)
{
int Code = Application->MessageBox(L"Close Form", L"Close Form", MB_YESNO|MB_ICONINFORMATION);
if(Code ==IDYES){
delete Sender;
Sender = NULL;
}
}