Hi I am unable to update the status bar's text property in the form by another form's button click. It is successfully compiled and able to run until I clicked The error I got is "Access violation at address: XXXXXXXX....."
C++ Builder XE7 in Win 7 is used.
Form1:
#include "Main.h"
#include "minor.h"
....
TForm1 *Form1;
TForm2 *Form2;
....
Form2:
#include "minor.h"
#include "Main.h"
....
TForm2 *Form2;
TForm1 *Form1;
....
__fastcall TForm2::TForm2(TComponent* Owner)
: TForm(Owner)
{
TButton* btnTest2 = new TButton(this);
btnTest2->Height = 50;
btnTest2->Width = 200;
btnTest2->Left = 220;
btnTest2->Top = 50;
btnTest2->Caption = "Updated Statusbar Button";
btnTest2->Visible = true;
btnTest2->Enabled = true;
btnTest2->Parent = this;
btnTest2->OnClick = &ButtonClicked2; // Create your own event here manually!
}
//---------------------------------------------------------------------------
void __fastcall TForm2::ButtonClicked2(TObject *Sender)
{
Form1->statusbarMain->Panels->Items[0]->Text = "Hello2"; // PROBLEM!!!
}
Any idea why it got problems? Please advise.. Thanks
Your Form2 unit is declaring its own
Form1variable that is separate from theForm1variable in the Form1 unit, and that second variable is not being initialized to anything meaningful. That is why your code is crashing - you are accessing the wrongForm1variable.You need to get rid of the
Form1variable in the Form2 unit and#includeForm1's header file instead (it should already have anexterndeclaration for Form1'sForm1variable). This is also necessary because Form2 needs the full declaration of theTForm1class in order to access its members, such asstatusbarMain.Same thing with the
Form2variable that you are declaring in the Form1 unit. You need to remove it and use#include "Form2.hpp"instead (which should have a suitableexterndeclaration).Form1.h:
Form1.cpp:
Form2.h:
Form2.cpp: