Question

I just tried write small program in C++ Builder 6(don't ask me why, its just a homework in institute). So, my program must hide button1 when i resizing form. But resize event raises after window created, its mean that after i start program button1 is already invisible.

void __fastcall TForm1::FormResize(TObject *Sender)
{
  Button1->Visible = false;
}

I tried use different resize events, but it don't works too. What I'm doing wrong?

PS. Sorry for my bad English.

Was it helpful?

Solution

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:

  1. 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;
    }
    
  2. 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;
    }
    
  3. 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top