Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to create a dynamic modeless

Status
Not open for further replies.

johncp

Technical User
Aug 28, 2005
47
GB
Folks

I am using BCB6 to code a dialog box which pops up
when a drop down menu option is clicked. I have chosen a dynamic
modeless form to be the dialog box. This will offer the user multiple
mutually exclusive choices.

This looks easy enough to do on the main form with
radio buttons in a Radio Button Group, but my form is dynamic.
and wil look something like this

Code:
//mainmenu event handler 
void __fastcall TForm1::createdialog1(TObject *Sender)  
{
TForm* Form2 = new TForm(this) ;
Form2->ShowModal();
//code to size and position the form 
//code to create & display "close dialog box" button 
//code to display radio buttons giving user one choice 
only
//code to detect which is radio button user selects
delete Form2 ;   
Form2 = NULL ;    //safe way to release memory 
}

Do I have to programatically create the instances of TRadioGroup & TRadioButton or is there a simpler way to create dynamic forms containing components, using the component palette ?

johnp
 
I create the form(s) via the IDE and include their header(s) in the main form. The one trick to this is to go to your project options and remove the form(s) from being created automtically. It worn't hurt if they are auto-created but it will save some memory.

One more thing. My notes say:
Code:
// use this 
Form2 = 0;        //safe way to release memory 
// instead of this
Form2 = NULL ;    //safe way to release memory

James P. Cottingham
-----------------------------------------
I'm number 1,229!
I'm number 1,229!
 
Hi James

Thanx for the reply
My previous post should have read Modal form, not Modeless. Apologies

I followed your advice & have a IDE Form2 which I can place components on. My code now reads

Code:
//------unit1.h---------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
  TMainMenu *MainMenu1;
  TMenuItem *Options1;
  TMenuItem *OpenDialog11;
  void __fastcall createdialog1(TObject *Sender);
private:	// User declarations
public:		// User declarations
  __fastcall TForm1(TComponent* Owner);
};
extern PACKAGE TForm1 *Form1 ;
//---------------unit2.h-------------
class TForm2 : public TForm
{
__published:	// IDE-managed Components
	TButton *Button1;
 	void __fastcall buttonmsg(TObject *Sender);
private:	// User declarations
public:		// User declarations
	__fastcall TForm2(TComponent* Owner);
};
//-------Unit1.cpp--------
TForm1 *Form1 ;
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
}

void __fastcall TForm1::createdialog1(TObject *Sender)
{
  TForm2* Form2 = new TForm2(this);//without this statement get an access violation
  Form2->ShowModal();
  Canvas->TextOutA(40,20, "Modeless form dialog box exiting") ;
  delete Form2 ;
  Form2 = 0 ;   //exception "safe"  (NULL = 0x30 = ASCII 0)
}
//-------------unit2.cpp----------
#include "Unit2.h"
#pragma package(smart_init)
#pragma resource "*.dfm"
//TForm2 *Form2;

__fastcall TForm2::TForm2(TComponent* Owner)
	: TForm(Owner)
{
//Form defined in Unit2.h 
}

void __fastcall TForm2::buttonmsg(TObject *Sender)
{
Canvas->TextOutA(20,80, "Button1 clicked") ;
}

But "TForm2* Form2 = new TForm(this)" doesn't generate an
instance of Form2 as described in the Object Inspector.
F'instance changing Form2 properties in the OI has no effect on the pop up form at run time. Also I cannot make button1 visible.

What am I doing wrong ?

johnp
 
I don't know if this will fix your problem or not but try this first:
Code:
//------unit1.h---------
class TForm1 : public TForm
{
__published:    // IDE-managed Components
  TMainMenu *MainMenu1;
  TMenuItem *Options1;
  TMenuItem *OpenDialog11;
private:    // User declarations
[red]  void createdialog1(TObject *Sender);[/red]

public:        // User declarations
  __fastcall TForm1(TComponent* Owner);
};
extern PACKAGE TForm1 *Form1 ;

Code:
//-------Unit1.cpp--------
TForm1 *Form1 ;
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
}

[red]void TForm1::createdialog1(TObject *Sender)[/red]
{
  TForm2* Form2 = new TForm2(this);//without this statement get an access violation
  Form2->ShowModal();
  Canvas->TextOutA(40,20, "Modeless form dialog box exiting") ;
  delete Form2 ;
  Form2 = 0 ;   //exception "safe"  (NULL = 0x30 = ASCII 0)
}

Code:
//---------------unit2.h-------------
class TForm2 : public TForm
{
__published:    // IDE-managed Components
    TButton *Button1;
private:    // User declarations
[red]     void buttonmsg(TObject *Sender);[/red]

public:        // User declarations
    __fastcall TForm2(TComponent* Owner);
};

Code:
//-------------unit2.cpp----------
#include "Unit2.h"
#pragma package(smart_init)
#pragma resource "*.dfm"
//TForm2 *Form2;

__fastcall TForm2::TForm2(TComponent* Owner)
    : TForm(Owner)
{
//Form defined in Unit2.h
}

[red]void TForm2::buttonmsg(TObject *Sender)[/red]
{
Canvas->TextOutA(20,80, "Button1 clicked") ;
}

For your header files, your functions should be declared in either the private or public sections. Also notice that I removed the __fastcall (it really isn't related to your problem, though). For the reason why, see
James P. Cottingham
-----------------------------------------
I'm number 1,229!
I'm number 1,229!
 
Hi James

I could not move buttonmsg() and createdialog1() from the _published section of the class 'cos they're event handlers.

While fiddling with the code I noticed that the IDE had moved one of the files from the \project to the \lib directory (the BCB6 IDE screws up sometimes). I edited the .Bpr file to make it use only files in the project directory & to my surprise the 2 forms worked ok without code changes. Still not sure why !

Thanx for the help

johnp
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top