tags:

views:

47

answers:

3

I wrote some program in C++ Builder 6 (don't ask me why).
I do some actions with on-form components after formResize event occurs.
But it occurs right after I close my program, and looks like all components on form where deleted, so I've got exception and everything crashes.
There is my code:

void __fastcall TForm3::FormResize(TObject *Sender)
{
    Image1->Picture->Graphic->Width = Image1->Width;
    Image1->Picture->Graphic->Height = Image1->Height;    
}

What can I do with that?

+2  A: 

You could check to make sure the objects haven't been deleted:

void __fastcall TForm3::FormResize(TObject *Sender)
{
    if (Image1) {
        Image1->Picture->Graphic->Width = Image1->Width;
        Image1->Picture->Graphic->Height = Image1->Height; 
    }   
}

But that assumes you always set pointers back to NULL when you delete them.

UPDATE:

Or you could do this:

void __fastcall TForm3::FormResize(TObject *Sender)
{
    if (this->Visible) {
        Image1->Picture->Graphic->Width = Image1->Width;
        Image1->Picture->Graphic->Height = Image1->Height;
    } 
}
robinjam
Image1 and other components deleted automatically by builder and it seems that their pointers doesn't set to NULL.
Andrew
Thanks, that works
Andrew
+2  A: 

Well a late addition to the list of possible answers, the components hold a state set, this holds information about the components (drums) current state. When the form is being freed the state set includes the csDestroying state. So in your resize event you could include a check for this.

void __fastcall TForm3::FormResize(TObject *Sender)
{
    if (!ComponentState.Contains(csDestroying)) {
        Image1->Picture->Graphic->Width = Image1->Width;
        Image1->Picture->Graphic->Height = Image1->Height;
    } 
}

This will make sure your resizing code only occurs when the form is not being freed. Whether this is a more appropriate approach than the one suggested by robinjam, is up to you to decide. I like this approach most however, since it seems more like the "correct" way of doing it. What happens if in a future version of the VCL library the visible parameter is not yet set to false.

But it's up to you, I've added it to provide a different alternative anyway.

TommyA
+2  A: 

A cleaner approach is to not resize the underlying Graphic itself at all but to use the TImage's Stretch property to let it simply resize the display of the Graphic instead. But if you must resize the actual Graphic, then you should use the TImage's OnResize event instead of the TForm's OnResize event, and then use the TImage's Anchors property to let the VCL resize the TImage automatically for you.

Remy Lebeau - TeamB