views:

135

answers:

5

In my application (Main form is TTntForm, C++Builder 2006):

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  Caption=L"1st caption";        // This works.
  Form1->Caption=L"2nd caption"; // But this doesn't work,
                                 // Caption of the form remains "1st caption".
}

What might be the cause of this problem?

Edited: Thank you all for your answers. I found the bug. There was a twice form creation in project file:

Application->CreateForm(__classid(TForm1), &Form1);
Application->CreateForm(__classid(TForm1), &Form1);
+1  A: 

Are you sure the TForm1 class form you are working with is instantiated as Form1?

birger
+2  A: 

Try

Self.Caption

if this works then Form1 isn´t a instance of TForm1

Or debug it to see the type

DonOctavioDelFlores
+4  A: 

Are you sure that "this" is actually Form1?

if (this != Form1)
    ShowMessage("Whoops. Didn't expect that...");

How is your form being created? Is it in the list if "autocreate" forms in the project options, or are you manually creating an instance of it?

Assuming Form1 is your main form, it's normally created by code in your main project.cpp file, in function WinMain().

Application->CreateForm(__classid(TForm1), &Form1);

This should get written for you automatically by C++Builder, so be wary of changing it manually.

Roddy
Yes this != Form1. How can I find where does it change (It is declared as TForm1 *Form1)?
samir105
It is not declared anywere. You must first callForm1= new TForm1(this);somewere in your code
Riho
A: 

I don't think that TForm1 knows that you have called

TForm1 * Form1=new TForm1(...);

somewere. Is this your first attempt in CBuilder? TForm1 * Form1 that you see generated in top of the file is just declaration. you must also create it. Why don't you like the first, working, solution? There is no need to use Form1 inside the class. Or if you really must then use

this->Caption="...";
Riho
+1  A: 

Thanks you all for your answers. I found the bug. There was a twice form creation in project file:

Application->CreateForm(__classid(TForm1), &Form1);
Application->CreateForm(__classid(TForm1), &Form1);
samir105