There is nothing wrong. The Form really does resize while it is being created, that is why you get the event. There are many ways you can address this:
use a variable to ignore the first
OnResize
event until the form is ready:private: bool fReady;
void __fastcall TForm1::FormResize(TObject *Sender) { if (!fReady) fReady = true; else Button1->Visible = false; }
use the Form's
OnShow
event to post a custom message to signal the form is ready:private: bool fReady; protected: virtual void __fastcall WndProc(TMessage &Message);
const UINT WM_READY = WM_APP + 100; void __fastcall TForm1::WndProc(TMessage &Message) { if (Message.Msg == WM_READY) fReady = true; else TForm::WndProc(Message); } void __fastcall TForm1::FormShow(TObject *Sender) { PostMessage(Handle, WM_READY, 0, 0); } void __fastcall TForm1::FormResize(TObject *Sender) { if (fReady) Button1->Visible = false; }
use a short timer instead of a custom message:
private: bool fReady;
void __fastcall TForm1::Timer1Timer(TMessage &Message) { Timer1->Enabled = false; fReady = true; } void __fastcall TForm1::FormShow(TObject *Sender) { Timer1->Enabled = true; } void __fastcall TForm1::FormResize(TObject *Sender) { if (fReady) Button1->Visible = false; }
Just to name a few.