Board index » cppbuilder » Opening a modal form from a modeless form

Opening a modal form from a modeless form

I need to be able to open a modal form from a modeless one.  The catch
is I want it to only suspend control of the modeless form not the entire
application.  I have attached three forms that are a part of a test
project.
Unit 1 is the main application form.  It opens the modeless dialogs

Unit 2 is the modeless dialog.  It is opened by unit 1 and opens unit 3
modal.

Unit 3 is the modal dialog.  It is opened by unit 2 and only contains a
close button.

Thanks
Chris

[ Unit3.h < 1K ]
//---------------------------------------------------------------------------
#ifndef Unit3H
#define Unit3H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm3 : public TForm
{
__published:    // IDE-managed Components
     TButton *Button1;
     void __fastcall Button1Click(TObject *Sender);
private:        // User declarations
public:         // User declarations
     __fastcall TForm3(TComponent* Owner);

Quote
};

//---------------------------------------------------------------------------
extern PACKAGE TForm3 *Form3;
//---------------------------------------------------------------------------
#endif

[ Unit1.dfm < 1K ]
?
 TMAINFORM 0 P   TPF0   TMainForm MainForm Left B  Top q Width ?  Height y Caption  Main Color         clBtnFace Font.Charset  DEFAULT_CHARSET
Font.Color clWindowText Font.Height ? Font.Name
MS Sans Serif
Font.Style   OldCreateOrder
PixelsPerInch `
TextHeight
  TButton Button1 Left   Top   Width 1  Height Q Caption  Open TabOrder   OnClick Button1Click  

[ Unit1.h < 1K ]
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TMainForm : public TForm
{
__published:    // IDE-managed Components
     TButton *Button1;
     void __fastcall Button1Click(TObject *Sender);
private:        // User declarations
public:         // User declarations
     __fastcall TMainForm(TComponent* Owner);

Quote
};

//---------------------------------------------------------------------------
extern PACKAGE TMainForm *MainForm;
//---------------------------------------------------------------------------
#endif

[ Unit2.cpp 1K ]
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Unit2.h"
#include "Unit3.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TMyForm *MyForm;
//---------------------------------------------------------------------------
__fastcall TMyForm::TMyForm(TComponent* Owner)
     : TForm(Owner)
{

Quote
}

//---------------------------------------------------------------------------

void __fastcall TMyForm::Button1Click(TObject *Sender)
{
     Close();

Quote
}

//---------------------------------------------------------------------------

void __fastcall TMyForm::FormClose(TObject *Sender, TCloseAction &Action)
{
     Action = caFree;

Quote
}

//---------------------------------------------------------------------------
void __fastcall TMyForm::CreateParams(
        Controls::TCreateParams &           _params )
{
        TCustomForm::CreateParams( _params );

     _params.WndParent = 0;

Quote
}

void __fastcall TMyForm::Button2Click(TObject *Sender)
{
     MessageBox(this->Handle,"This is a modal messagebox\n for a modeless dialog","MessageBoxTest",MB_OK);
Quote
}

//---------------------------------------------------------------------------

void TMyForm::Initialize()
{
     Show();

Quote
}

void __fastcall TMyForm::Button3Click(TObject *Sender)
{
     TForm3 *pForm = new TForm3(this);
     pForm->ShowModal();    
Quote
}

//---------------------------------------------------------------------------

  Unit2.dfm
< 1K Download

[ Unit2.h 1K ]
//---------------------------------------------------------------------------
#ifndef Unit2H
#define Unit2H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TMyForm : public TForm
{
__published:    // IDE-managed Components
     TButton *Button1;
     TButton *Button2;
     TButton *Button3;
     void __fastcall Button1Click(TObject *Sender);
     void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
     void __fastcall Button2Click(TObject *Sender);
     void __fastcall Button3Click(TObject *Sender);
private:        // User declarations
public:         // User declarations
     __fastcall TMyForm(TComponent* Owner);
        virtual void __fastcall CreateParams( Controls::TCreateParams & _params );
     void Initialize();    

Quote
};

//---------------------------------------------------------------------------
extern PACKAGE TMyForm *MyForm;
//---------------------------------------------------------------------------
#endif

[ Unit3.cpp < 1K ]
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Unit3.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm3 *Form3;
//---------------------------------------------------------------------------
__fastcall TForm3::TForm3(TComponent* Owner)
     : TForm(Owner)
{

Quote
}

//---------------------------------------------------------------------------

void __fastcall TForm3::Button1Click(TObject *Sender)
{
     Close();    

Quote
}

//---------------------------------------------------------------------------

[ Unit3.dfm < 1K ]
?
 TFORM3 0 T   TPF0 TForm3 Form3 Left    Top  Width ?  Height G Caption Another Form Color         clBtnFace Font.Charset  DEFAULT_CHARSET
Font.Color clWindowText Font.Height ? Font.Name
MS Sans Serif
Font.Style   OldCreateOrder
PixelsPerInch `
TextHeight
  TButton Button1 Left ( Top   Width Y Height   Caption  &Close TabOrder   OnClick Button1Click  

[ Unit1.cpp < 1K ]
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TMainForm *MainForm;
//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
     : TForm(Owner)
{

Quote
}

//---------------------------------------------------------------------------
void __fastcall TMainForm::Button1Click(TObject *Sender)
{
     TMyForm * pForm = new TMyForm(this);
     pForm->Initialize();
//     pForm->Show();
Quote
}

//---------------------------------------------------------------------------
 

Re:Opening a modal form from a modeless form


Hi, Martin!

Have you tried having the dialog be instead a non-modal form? You could
have the caller of the dialog set its own form's Enabled property to
false before the invocation, and have the dialog somehow notify its
caller when it is done.

------
Mark Cashman, TeamB C++ Builder
http://www.temporaldoorway.com/programming/index.htm
C++ Builder, JBuilder programming information
Home of The C++ Builder Programmer's Webring - Join us!
------

Re:Opening a modal form from a modeless form


Quote
> have the dialog somehow notify its caller when it is done.

How would that be done?

Re:Opening a modal form from a modeless form


Also I need it to respond to the task bar activations the same way the
message box does.

ie.  If I click on the taskbar for the modeless dialog, if that dialog
has a modal form opened I need the modal form to be acitvated.

This is the same functionality I get with a messageBox using the
MessageBox(this->Handle,"...","",MB_OK);

call

Re:Opening a modal form from a modeless form


Hi, Chris!

The dialog could set a variable to be checked periodically by a timer in
the form.

It could send a message to the main form window with SendMessage.

The main form could wait on a TEvent that gets signaled by the dialog (I
think).

The dialog could call a main form method.

There are probably other ways.

------
Mark Cashman, TeamB C++ Builder
http://www.temporaldoorway.com/programming/index.htm
C++ Builder, JBuilder programming information
Home of The C++ Builder Programmer's Webring - Join us!
------

Other Threads