Borland C++ Builder 2007: Problems with events
August 6th, 2016
If I make a pointer e.g. on a Button: TButton *Button1;
and further I deklare it with void __fastcall TForm1::FormCreate(TObject *Sender)
{
Button1=new TButton(Form1); //Form1…Owner
Button1->Parent=Form1;
}
Now I would like to define what happens if you click on the Button:
void __fastcall Button1Click(TObject *Sender)
{
exit(1);
}
//This is exactly the same what the Builder makes if you click on events->OnClick //in the ObjectInspector
But now if I click this Button nothing happens. I’ve already watched in the help of Borland, there stands as an example:
Sorry for the German comments!!!
F�r dieses Beispiel werden ein TPageControl-Objekt und ein TCheckBox-Objekt ben�tigt. Die Ereignisbehandlungsroutine f�r OnCreate des Formulars f�llt jede Registerkarte mit einem Eingabefeld, das an einer Zufallsposition innerhalb des Client-Bereichs plaziert wird.
Die Deaktivierung des Kontrollfeldes bewirkt, da� der Client-Bereich (nicht der Registerbereich) von TabControl unsichtbar wird.
void __fastcall TForm1::PageControl1Change(TObject *Sender)
{
CheckBox1->Checked = PageControl1->ActivePage->Visible;
}
void __fastcall TForm1::CheckBox1Click(TObject *Sender)
{
PageControl1->ActivePage->Visible = CheckBox1->Checked;
}
void __fastcall TForm1::FormCreate(TObject *Sender)
{
for (int i = 0; i < 10; i++)
{
TTabSheet *pPage = new TTabSheet(this);
pPage->PageControl = PageControl1;
pPage->Caption = AnsiString("Page") + IntToStr(i);
TEdit *pEdit = new TEdit(this);
pEdit->Parent = pPage;
pEdit->Left = random(pPage->ClientWidth - pEdit->Width);
pEdit->Top = random(pPage->ClientHeight - pEdit->Height);
}
PageControl1Change(Sender);
}
So I’ve also tried to type the bold letters into the OnCreate event of the form:
Button1Click(Sender); //stands in Form1::FormCreate
But if now when I compile this programm and run it, it automatically starts the Button1Click prozedure (what is logical).
And here the question:
What could I do, to make this programm run? (So that the OnClick Event is only startet when I click on it) I have no idea any more, and so I hope somebody out there can help me to make it run. (I’ve already searched in google for it, but I’ve nothing found.
Thank you!!!!!! Merry X-Mas and a happy new year!
Here the answer, I have it from a different forum:
Thanks to tutorials.de, great work guys:
void __fastcall TForm1::Button1Click(TObject Sender)
{
TButton *MyButton = new TButton(this);
MyButton->Parent = this;
MyButton->Name = "MyButton";
MyButton->OnClick = ClickMyButton;
}
void __fastcall TForm1::ClickMyButton(TObject *Sender)
{
ShowMessage("Button has been clicked");
}
Naturally I have to declare ClickMyButton as a procedure in Form1, but that’s all!