C++builder 窗体之间变量传送问题
首先在unit1.h的public中定义一个string
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TLabel *Label1;
TLabel *Label2;
TLabel *Label3;
void __fastcall Button1Click(TObject *Sender);
private: // User declarations
public: // User declarations
AnsiString string;//在form1的public里定义string
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
然后在unit1.cpp中加入如下语句:
string="ABC";
Label3->Caption=string; //此处在form1的Label3处能显示ABC;
Form2->Show();
在unit2.cpp中已经写了#include "Unit1.h"
Form中有label1和Label2
还有如下语句:
Label1->Caption=Form1->string;
Label2->Caption="123";
编译没显示错误,运行,跳出Form2后 Label2处显示123,但label1处确是个空白
疑问:为什么在unit1.h的public中定义的变量string不能将其值传送到Form2中,当然在unit2.cpp中已经包含了unit1.h了
而string在Form1中能正常使用