Board index » cppbuilder » Multiple forms and closing one form

Multiple forms and closing one form


2003-07-17 06:35:59 PM
cppbuilder55
Hi,
I have just added a second form (Form2) to my project which is a small
window where the user can change some setting for the main program. This
Form2 can be opened by clicking in the main menu of Form1. The problem is,
when I close Form2 (by clicking the top right corner of the window) then not
just Form2 closes but the entire program terminates.
I want only Form2 to become non-visible but the main program to continue
running, how do I achieve that?
Peter
 
 

Re:Multiple forms and closing one form

Sorry, I've accidentally posted this question to the wrong newsgroup. It's
off-topic here.
 

Re:Multiple forms and closing one form

Has form2 become the main form for some reason - check project options.
This doesn't normally happen.
HTH Pete
"Peter Venis" < XXXX@XXXXX.COM >wrote in message
Hi,
I have just added a second form (Form2) to my project which is a small
window where the user can change some settings for the main program. This
Form2 can be opened by clicking in the main menu of Form1. The problem is,
when I close Form2 (by clicking the top right corner of the window) then not
just Form2 closes but the entire program terminates.
I want only Form2 to become non-visible but the main program to continue
running, how do I achieve that?
Peter
 

{smallsort}

Re:Multiple forms and closing one form

Thanks, that did the trick.
I had changed the order in which the forms were created in WinMain()
function. I think that was the reason that form 2 became the main form.
Peter
"Pete Fraser" < XXXX@XXXXX.COM >schreef in bericht
Quote
Has form2 become the main form for some reason - check project options.
This doesn't normally happen.
HTH Pete

"Peter Venis" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...
Hi,

I have just added a second form (Form2) to my project which is a small
window where the user can change some settings for the main program. This
Form2 can be opened by clicking in the main menu of Form1. The problem is,
when I close Form2 (by clicking the top right corner of the window) then
not
just Form2 closes but the entire program terminates.

I want only Form2 to become non-visible but the main program to continue
running, how do I achieve that?

Peter




 

Re:Multiple forms and closing one form

Here goes an example from my tutorial which creates a Program Settings Form
at runtime
//***********
Unit1.cpp
//***********
//--------------------------------------------------------------------------
-
#include <vcl\vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "Options.h"
//--------------------------------------------------------------------------
-
#pragma resource "*.dfm"
TForm1 *Form1;
//--------------------------------------------------------------------------
-
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//--------------------------------------------------------------------------
-
void __fastcall TForm1::Exit1Click(TObject *Sender)
{
Application->Terminate();
}
//--------------------------------------------------------------------------
-
void __fastcall TForm1::Settings1Click(TObject *Sender)
{
// Show the Options : Program Settings Form Modally.
// Remember that a Modal form must be closed
// before being able to work in another open form of
// the application.
// The Owner of the Modal form (Form1) cannot be made active
// (by the user or by the application) until the modal
// form is closed (destroyed).
OptionsForm->ShowModal();
}
//--------------------------------------------------------------------------
-
//*******
//Unit1.h
//*******
//--------------------------------------------------------------------------
-
#ifndef Unit1H
#define Unit1H
//--------------------------------------------------------------------------
-
#include <vcl\Classes.hpp>
#include <vcl\Controls.hpp>
#include <vcl\StdCtrls.hpp>
#include <vcl\Forms.hpp>
#include <vcl\Menus.hpp>
//--------------------------------------------------------------------------
-
class TForm1 : public TForm
{
__published: // IDE-managed Components
TMainMenu *MainMenu1;
TMenuItem *File1;
TMenuItem *Exit1;
TMenuItem *Options1;
TMenuItem *Settings1;
void __fastcall Exit1Click(TObject *Sender);
void __fastcall Settings1Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//--------------------------------------------------------------------------
-
extern TForm1 *Form1;
//--------------------------------------------------------------------------
-
#endif
//*********
Options.cpp
//**********
//---------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Options.h"
#include "Unit1.h"
#pragma resource "*.dfm"
TOptionsForm *OptionsForm;
extern TForm1 *Form1;
const char* TabCaptions[] = {"Play", "File Formats", "Dimensions",
"Status Bar", "Zoom"};
const int ADDITIONAL_TABS_TOTAL = 10;
TTabSheet* AdditionalTabSheet[ADDITIONAL_TABS_TOTAL];
static count = 0;
//---------------------------------------------------------------------
__fastcall TOptionsForm::TOptionsForm(TComponent* AOwner)
: TForm(AOwner)
{
// Set the parameters of the "Options : Program Settins' Form
Width = 450;
Height = 400;
Position = poScreenCenter;
BorderStyle = bsDialog;
// Create a Panel where the PageControl will be created
TPanel* Panel1 = new TPanel(this);
Panel1->Parent = this;
Panel1->Align = alTop;
Panel1->Height = Height - 80;
// Create the Buttons to Save and to Close the Form
TButton* Button1 = new TButton(this);
Button1->Parent = this;
Button1->Top = Panel1->Top + Panel1->Height + 5;
Button1->Height = 30;
Button1->Width = 100;
Button1->Left = Width/2 - Button1->Width - 20;
Button1->Caption = "Save";
Button1->OnClick = Button1Click;
TButton* Button2 = new TButton(this);
Button2->Parent = this;
Button2->Top = Button1->Top;
Button2->Height = Button1->Height;
Button2->Width = Button1->Width;
Button2->Left = Width/2 + 20;
Button2->Caption = "Close";
Button2->OnClick = Button2Click;
// Add Tab
TButton* Button3 = new TButton(this);
Button3->Parent = this;
Button3->Top = Button1->Top;
Button3->Height = Button1->Height;
Button3->Width = 30;
Button3->Left = Button2->Left + Button2->Width + 20;
Button3->Caption = "Add Tab";
Button3->OnClick = Button3Click;
// Remove Tab Sheet
TButton* Button4 = new TButton(this);
Button4->Parent = this;
Button4->Top = Button1->Top;
Button4->Height = Button1->Height;
Button4->Width = 30;
Button4->Left = Button3->Left + Button3->Width + 20;
Button4->Caption = "Rem Tab";
Button4->OnClick = Button4Click;
// Create the PageControl Object
PageControl1 = new TPageControl(Panel1);
PageControl1->Parent = Panel1;
PageControl1->Align = alClient;
PageControl1->OnChanging = PageChangingHandler;
// Sets the ActivePage Property of PageControl1
PageControl1->OnChange = PageChangeHandler;
// Create the TabSheets objects
for (int i=0; i< ARRAY_SIZE(TabCaptions); i++)
{
TabSheet[i] = new TTabSheet(this);
TabSheet[i]->PageControl = PageControl1;
TabSheet[i]->Caption = String(TabCaptions[i]);
// Set the attributes od the Text written on the
// TabSheets
TabSheet[i]->Font->Name = "Times New Roman";
TabSheet[i]->Font->Size = 10;
}
CurrentTabIndex = 0;
// Create Controls on TabSheet[0] (First Tab)
for (int i=0; i<5; ++i)
{
CheckBox0[i] = new TCheckBox(TabSheet[0]);
CheckBox0[i]->Parent = TabSheet[0];
CheckBox0[i]->Top = 50 + i*30;
CheckBox0[i]->Left = 50;
CheckBox0[i]->Height = 20;
//Length returns the number of bytes in this AnsiString.
CheckBox0[i]->Width = TabSheet[i]->Font->Size *
CheckBCaptions0[i].Length();
CheckBox0[i]->Caption = CheckBCaptions0[i];
CheckBox0[i]->Checked = true;
CheckBox0[i]->OnClick = CheckBox1Click;
}
// Create Controls on TabSheet[1] (Second Tab) ------------------
for (int i=0; i<6; ++i)
{
CheckBox1[i] = new TCheckBox(TabSheet[1]);
CheckBox1[i]->Parent = TabSheet[1];
CheckBox1[i]->Top = 50 + i*30;
CheckBox1[i]->Left = 50;
CheckBox1[i]->Height = 20;
//Length returns the number of bytes in this AnsiString.
CheckBox1[i]->Width = 200;
CheckBox1[i]->Caption = CheckBCaptions1[i];
CheckBox1[i]->Checked = true;
CheckBox1[i]->OnClick = CheckBox1Click;
}
// Create Controls on TabSheet[2] (Third Tab) ------------------
for (int i=0; i<4; ++i)
{
ComboBox2[i] = new TComboBox(TabSheet[2]);
ComboBox2[i]->Parent = TabSheet[2];
ComboBox2[i]->Top = 50 + i*40;
ComboBox2[i]->Left = 50;
ComboBox2[i]->Height = 20;
ComboBox2[i]->Width = 60;
Label2[i] = new TLabel(TabSheet[2]);
Label2[i]->Parent = TabSheet[2];
Label2[i]->Top = ComboBox2[i]->Top;
Label2[i]->Left = ComboBox2[i]->Left + ComboBox2[i]->Width + 10;
Label2[i]->Height = 20;
Label2[i]->Width = 100;
Label2[i]->Caption = LabelCaptions2[i];
}
ComboBox2[0]->Text = "0";
ComboBox2[1]->Text = "0";
ComboBox2[2]->Text = "185";
ComboBox2[3]->Text = "120";
//Set the Scroll List (strings) of Combo Boxes
for (int combo=0; combo < 2; ++combo)
for (int index=0; index < ARRAY_SIZE(SepPanels); ++index)
ComboBox2[combo]->Items->Add( SepPanels[index] );
for (int combo=2; combo < 4; ++combo)
for (int index=0; index < ARRAY_SIZE(StdMovieDims); ++index)
ComboBox2[combo]->Items->Add( StdMovieDims[index] );
// Create Controls on TabSheet[3] (Fourth Tab) ------------------
CheckBox3 = new TCheckBox(TabSheet[3]);
CheckBox3->Parent = TabSheet[3];
CheckBox3->Top = 50;
CheckBox3->Left = 50;
CheckBox3->Height = 20;
CheckBox3->Width = 200;
CheckBox3->Caption = CheckBCaptions3;
CheckBox3->Checked = true;
CheckBox3->OnClick = CheckBox1Click;
// Create Controls on TabSheet[4] (Fifth Tab) ------------------
ComboBox4 = new TComboBox(TabSheet[4]);
ComboBox4->Parent = TabSheet[4];
ComboBox4->Top = 50;
ComboBox4->Left = 50;
ComboBox4->Height = 20;
ComboBox4->Width = 60;
ComboBox4->Text = "80";
//Set the Scroll List (strings) of Combo Box
for (int index=0; index < ARRAY_SIZE(Zooms); ++index)
ComboBox4->Items->Add( Zooms[index] );
Label4 = new TLabel(TabSheet[4]);
Label4->Parent = TabSheet[4];
Label4->Top = ComboBox4->Top;
Label4->Left = ComboBox4->Left + ComboBox4->Width + 10;
Label4->Height = 20;
Label4->Width = 100;
Label4->Caption = LabelCaptions4;
// Set the caption of Options Form to be the title of the first tab
Caption = "Program Settings : " + PageControl1->ActivePage->Caption;
}
//---------------------------------------------------------------------
void __fastcall TOptionsForm::PageChangingHandler(TObject *Sender,
bool &AllowChange)
{
//ShowMessage("HandlePageChanging");
// Save the index to the active TabSheet
CurrentTabIndex = PageControl1->ActivePage->PageIndex;
}
//--------------------------------------------------------------------------
-
void __fastcall TOptionsForm::PageChangeHandler(TObject *Sender)
{
//ShowMessage("PageChangeHandler");
// Set the caption of OptionsForm to be the title of the current tab
Caption = "Program Settings : " + PageControl1->ActivePage->Caption;
}
//--------------------------------------------------------------------------
-
void __fastcall TOptionsForm::Button1Click(TObject *Sender)
{
// Save the settings and exit the Options form
ShowMessage("Place the code to Save the Program Settings here");
OptionsForm->Close();
}
//--------------------------------------------------------------------------
-
void __fastcall TOptionsForm::Button2Click(TObject *Sender)
{
// Exit the Options Form without saving the settings
OptionsForm->Close();
}
//--------------------------------------------------------------------------
-
void __fastcall TOptionsForm::CheckBox1Click(TObject *Sender)
{
// If CheckBox is ticked set color to black
// If CheckBox is not ticked set color to red
for (int i=0; i<5; ++i)
if (CheckBox0[i]->Checked)
CheckBox0[i]->Font->Color = clBlack;
else
CheckBox0[i]->Font->Color = clRed;
for (int i=0; i<6; ++i)
if (CheckBox1[i]->Checked)
CheckBox1[i]->Font->Color = clBlack;
else
CheckBox1[i]->Font->Color = clRed;
for (int i=0; i<4; ++i)
if (CheckBox1[i]->Checked)
CheckBox1[i]->Font->Color = clBlack;
else
CheckBox1[i]->Font->Color = clRed;
if (CheckBox3->Checked)
CheckBox3->Font->Color = clBlack;
else
CheckBox3->Font->Color = clRed;
}
//*********
//Options.h
//**********
//--------------------------------------------------------------------------
-
#ifndef OptionsH
#define OptionsH
//--------------------------------------------------------------------------
-
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <comctrls.hpp>
#include <ExtCtrls.hpp>// Don't forget to include this file
//---------------------------------------------------------------------
#define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
extern const char* TabCaptions[5];
// Captions for CheckBoxes on Tab 0
String CheckBCaptions0[] = {"Repeat Forever (Cinema Mode)",
"Play After Opening",
"Play After Changing Movie Size",
"Rewind After Playing",
"Play Music After Opening"};
// Captions for CheckBoxes on Tab 1
String CheckBCaptions1[] = {"Play mpeg Files",
"Play avi Files",
"Play wmv Files",
"Play mp3 Files",
"Play wav Files",
"Play mid Files"};
// Captions for CheckBoxes on Tab 2
String LabelCaptions2[] = {"Horizontal Separation Between Panels",
"Vertical Separation Between Panels",
"Standard Movie Width",
"Standard Movie Height"};
String SepPanels[] = {"0", "1", "2", "3", "4",
"5", "6", "7", "8", "9", "10"};
String StdMovieDims[] = {"100", "105", "110", "115", "120", "125",
"130", "135", "140", "145", "150", "155",
"160", "165", "170", "175", "180", "185",
"190", "195", "200"};
String Zooms[] = {"50", "55", "60", "65", "70", "75",
"80", "85", "90"};
// Captions for CheckBoxes on Tab 3
String CheckBCaptions3 = "Display Full Movie Path";
// Captions for CheckBoxes on Tab 4
String LabelCaptions4 = "Mouse Driven Zoom In (%)";
//--------------------------------------------------------------------------
-
class TOptionsForm : public TForm
{
__published:
void __fastcall PageChangingHandler(TObject *Sender, bool &AllowChange);
void __fastcall PageChangeHandler(TObject *Sender);
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
void __fastcall CheckBox1Click(TObject *Sender);
void __fastcall Button3Click(TObject *Sender);
void __fastcall Button4Click(TObject *Sender);
private:
int CurrentTabIndex;
TPageControl* PageControl1; // Page Control
TTabSheet* TabSheet[ARRAY_SIZE(TabCaptions)]; // Tabsheet control
// Controls on tab 0 (First Tab)
TCheckBox* CheckBox0[5];
// Controls on tab 1 (Second Tab)
TCheckBox* CheckBox1[6];
// Controls on tab 2 (Third Tab)
TComboBox* ComboBox2[4];
TLabel* Label2[4];
// Controls on tab 3 (Fourth Tab)
TCheckBox* CheckBox3;
// Controls on tab 4 (Fifth Tab)
TComboBox* ComboBox4;
TLabel* Label4;
public: // User declarations
__fastcall TOptionsForm(TComponent* Owner);
};
//--------------------------------------------------------------------------
-
extern TOptionsForm *OptionsForm;
//--------------------------------------------------------------------------
-
#endif
Hope it helps
Rodolfo
"Peter Venis" < XXXX@XXXXX.COM >wrote in message
Quote
Hi,

I have just added a second form (Form2) to my project which is a small
window where the user can change some setting for the main program. This
Form2 can be opened by clicking in the main menu of Form1. The problem is,
when I close Form2 (by clicking the top right corner of the window) then
not
just Form2 closes but the entire program terminates.

I want only Form2 to become non-visible but the main program to continue
running, how do I achieve that?

Peter