[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
CppBuilderModifyingTForm

(C++ Builder) Modifying your TForm

The C++ Builder IDE supplies you the TForm1 class. It is like any normal class, except of the __published part of it.

There are two ways to modify TForm1:

1) Modify the TForm using the IDE

This is easy for any beginner programmer.

I'll describe the following basic topics below

1.1) Adding a Component

Select a Component from the Component Palette by clicking on it (e.g. a TButton). Then click on the Form.

The IDE has automatically changed the class declaration of TForm1 to contain the Button: in the __published section you can now read:
__published:	// IDE-managed Components
  TButton *Button1;


Note the comment! It is managed by the IDE, so you do not need to call new or delete for this Button.

1.2) Changing a Component's Property

Select any C++ Builder Component, e.g. your TForm1. You should now see the name in the Object Inspector. This same Object Inspector has two tabs: Properties and Events.

Go to the Properties tab. Now you can easily change any Property you like.

1.3) Adding an Event to a Component

Select any C++ Builder Component, e.g. your TForm1. You should now see the name in the Object Inspector. This same Object Inspector has two tabs: Properties and Events. Go to the Events tab. Now double-click in the white square to the right of the Event you want, e.g. the OnClick Event. You will be taken to the code editor:

void __fastcall TForm1::FormClick(TObject *Sender)
{
}


Between the accolades you can put your code, e.g.:

void __fastcall TForm1::FormClick(TObject *Sender)
{
  ShowMessage("Hello world");
}


Note that in the class definition, you can find:

__published:	// IDE-managed Components
  void __fastcall FormClick(TObject *Sender);


As this code is managed by the IDE, do NOT remove it yourself. Remove the code between the accolades, then upon compiling the IDE will remove the empty Events itself.

2) Modify the TForm using the code editor

This takes slightly more knowledge of C++ then using the IDE. But all you need to know, is about classes, as the TForm1 is a class like any other, except of the __published part of it. But the public and private sections are yours!

I'll describe the following basic topics below

2.1) Adding a member variable

Go to the class declaration (in the header file. To the private section, add the lower line:

private:	// User declarations
  String mString;


Now initialize it in the class's constructor. Go to the class definition, and modify the constructor to:

__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
  mString = "Hello World";
}


You could test this by making an OnClick Event calling it:

void __fastcall TForm1::FormClick(TObject *Sender)
{
  ShowMessage(mString);
}


2.2) Adding a const member variable

Nearly the same as above:

Go to the class declaration (in the header file. To the private section, add the lower line:

private:	// User declarations
  const String mString;


Now initialize it in the class's constructor. Go to the class definition, and modify the constructor to:

__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner),
  mString("Hello World")
{
}


You could test this by making an OnClick Event calling it:

void __fastcall TForm1::FormClick(TObject *Sender)
{
  ShowMessage(mString);
}


2.3) Adding a method

Go to the class declaration (in the header file. To the private section, add the lower line:

private:	// User declarations
  void sayHello() const;


Now add the following lines of code in your class definition, preferable somewhere below the constructor:

void TForm1::sayHello() const
{
  ShowMessage("Hello World");
}


You could test this by making an OnClick Event calling it:

void __fastcall TForm1::FormClick(TObject *Sender)
{
  sayHello();
}


last edited (November 11, 2006) by bilderbikkel, Number of views: 2630, Current Rev: 10 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.