It is not legal to do that, no. You should never 'delete' a component from
its own event handlers. The AV is coming from the VCL trying to access the
button again after the event handler returns. You deleted the memory, so
the memory cannot be accessed anymore, so the AV occurs.
> Basically I have a delete button, a TUpDown object and a label...
> All have the same Top position...when the user clicks the delete
> button I want to delete that row of objects and then reposition
> the rest.
For something like that, I would suggest placing the controls onto either a
TPanel or a TFrame to keep them grouped together, and then when you click
the Delete button, post a custom message to the form containing a pointer to
the TPanel or TFrame and let the form delete it when nothing else is
happening with them. For example:
#define APPWM_DELETE_PANEL (WM_APP + 100)
void __fastcall TfrmFileDockSite::WndProc(TMessage &Message)
{
if( Message.Msg == APPWM_DELETE_PANEL )
{
delete reinterpret_cast<TPanel*>(Message.LParam);
Message.Result = TRUE;
}
else
TForm::WndProc(Message);
}
void __fastcall TfrmFileDockSite::btnDeleteClick(TObject *Sender)
{
TButton* btn = dynamic_cast<TButton*>(Sender);
if( btn )
{
TPanel *pnl = dynamic_cast<TPanel*>(btn->Parent);
if( pnl )
PostMessage(Handle, APPWM_DELETE_PANEL, 0,
reinterpret_cast<LPARAM>(pnl));
}
}
Gambit
void __fastcall TfrmFileDockSite::btnDeleteClick(TObject *Sender)
{
TButton* btn = dynamic_cast<TButton*>(Sender);
//delete some labels and other stuff
//...
delete btn; //is this ok to do?
It doesn't really matter. Technically, it is declared as 'protected' in
TWinControl and most descendants, but it is not required to be so. Some
people declare it as 'public' and some declare it as 'private'. It works
either way. Personally, I would suggest keeping it 'protected'.
Gambit