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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Opening a New Form

Status
Not open for further replies.

LoneRanger123

Programmer
Dec 19, 2000
62
0
0
GB
I have a form form2, and a main form, FrmMain, (im imaginative)

From a button click in FrmMain, how can I open a new copy of Form2, and pass it some variables?

Nice and simple 1, hopefully :)
 
Code:
#include "Unit2.h" // Assuming you saved it as Unit2

void __fastcall TFrmMain::Button1Click(TObject *Sender) {
     Form2->Show();
     // or
     // Form2->ShowModal();
     }

Then just set properties of objects on the form.
Note: If you want to create more than one instance of a form, that is more complex. If you need to know about that, just tell me and I will explain. [pc3]
 
also the Main CPP file has a createform command, would it involve that? :)
 
oh, and again, i dont want to edit Vars of the Program, but I want to pass it variables that it can use itself, but they would be different for each instance.
 
and another thing, if I want to reference the instance of a Form, and this will change on each, what reference do I use?
 
Here is an example which creates 5 forms with a paramater to set their caption.
Code:
/* UNIT1.CPP */
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include &quot;Unit1.h&quot;
#include &quot;Unit2.h&quot;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource &quot;*.dfm&quot;
TForm1 *Form1;
TForm2 *secondForms[5];
  // You probably could have put this in the header
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
for (int i = 0; i <= 5; i++) {
        secondForms[i] = new TForm2(Application, IntToStr(i));
        secondForms[i]->Left = i * 50;
        secondForms[i]->Top = i * 20;
        secondForms[i]->Show();
        }
}
//---------------------------------------------------------------------------




/* UNIT2.CPP */
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include &quot;Unit2.h&quot;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource &quot;*.dfm&quot;
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner, AnsiString newCapt)
        : TForm(Owner)
{
Caption = newCapt;
}
//---------------------------------------------------------------------------



/* UNIT2.H */
//---------------------------------------------------------------------------

#ifndef Unit2H
#define Unit2H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm2 : public TForm
{
__published:	// IDE-managed Components
private:	// User declarations
public:		// User declarations
        __fastcall TForm2(TComponent* Owner, AnsiString newCapt);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm2 *Form2;
//---------------------------------------------------------------------------
#endif
If you still need help on creating forms at runtime, create a new MDI app and look at the code in that (File->New->Projects->MDI Application) [pc3]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top