 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Mike Guest
|
Posted: Fri Dec 26, 2003 5:56 pm Post subject: delete the parent component |
|
|
Hi,
I have written my own componenten, derive from TPanel. That component, has
serval comboboxes and one button. My aim is to delete the full instanz of my
component, after click the button.
Following code would only delete the button, but not the whole componente.
void __fastcall TMyComp::ButtonClick(TObject *Sender)
{
PostMessage(Handle, APPWM_DELETE_COMP, 0, reinterpret_cast<int>(Sender));
}
//--------------------------------------------------------------------------
-
void __fastcall TForm::WndProc(TMessage &Message)
{
if(Message.Msg == APPWM_DELETE_COMP)
{
delete reinterpret_cast<TMyComp*>(Message.LParam);
Message.Result = TRUE;
}
else
TForm::WndProc(Message);
}
//--------------------------------------------------------------------------
-
But if i write Sender->Parent or Sender->Owner, this does not help. SO i
need anyhow to get the owner of the button, to delete the component. any
ideas how to do?
mike
|
|
| Back to top |
|
 |
Santa Guest
|
Posted: Fri Dec 26, 2003 5:58 pm Post subject: Re: delete the parent component |
|
|
"Mike" <mikeh (AT) gmx (DOT) de> wrote:
| Quote: | [...] Following code would only delete the button, but not
the whole componente.
|
The Sender - as it is in the ButtonClick event - is the button that was clicked. Since you want to delete the Parent of the Sender, You must either PostMessage with the Sender's Parent:
void __fastcall TMyComp::ButtonClick(TObject *Sender)
{
TButton* pButton = static_cast<TButton*>( Sender );
TMyComp* pMyComp = dynamic_cast<TMyComp*>( pButton->Parent );
if( pMyComp ) PostMessage(Handle, APPWM_DELETE_COMP, 0, reinterpret_cast<int>(Sender));
else ShowMessage( "Unable to delete Component" );
}
//-------------------------------------------------------------
void __fastcall TForm::WndProc(TMessage &Message)
{
if(Message.Msg == APPWM_DELETE_COMP)
{
delete reinterpret_cast<TMyComp*>(Message.LParam);
Message.Result = TRUE;
}
else
TForm::WndProc(Message);
}
OR you need to handle all of it in the WndProc:
void __fastcall TMyComp::ButtonClick(TObject *Sender)
{
PostMessage(Handle, APPWM_DELETE_COMP, 0, reinterpret_cast<int>(Sender));
}
//-------------------------------------------------------------
void __fastcall TForm::WndProc(TMessage &Message)
{
if(Message.Msg == APPWM_DELETE_COMP)
{
TButton* pButton = reinterpret_cast<TButton*>(Message.LParam);
TMyComp* pMyComp = dynamic_cast<TMyComp*>( pButton->Parent );
if( pMyComp )
{
delete pMyComp;
Message.Result = TRUE;
}
else ShowMessage( "Delete MYComp failed" );
}
else
TForm::WndProc(Message);
}
Happy Holidays
Santa
|
|
| Back to top |
|
 |
nicolasr Guest
|
Posted: Fri Dec 26, 2003 6:44 pm Post subject: Re: delete the parent component |
|
|
Hi,
| Quote: | Following code would only delete the button, but not the whole componente.
void __fastcall TMyComp::ButtonClick(TObject *Sender)
{
PostMessage(Handle, APPWM_DELETE_COMP, 0,
reinterpret_cast<int>(Sender));
}
//--------------------------------------------------------------------------
-
|
How do you create the button in your component? Does the button have an
Owner?
Or do you create it with new TButton(NULL)? I assume the Parent is your
panel
component? Does something like this work:
....
TButton* b = dynamic_cast<TButton*>(Sender);
PostMessage(Handle, APPWM_DELETE_COMP, 0, reinterpret_cast<int>(b->Parent));
....
Nick
|
|
| Back to top |
|
 |
Todd Brylski Guest
|
Posted: Fri Dec 26, 2003 7:08 pm Post subject: Re: delete the parent component |
|
|
"Mike" <mikeh (AT) gmx (DOT) de> wrote
| Quote: | Hi,
I have written my own componenten, derive from TPanel. That component, has
serval comboboxes and one button. My aim is to delete the full instanz of my
component, after click the button.
Following code would only delete the button, but not the whole componente.
void __fastcall TMyComp::ButtonClick(TObject *Sender)
{
PostMessage(Handle, APPWM_DELETE_COMP, 0, reinterpret_cast<int>(Sender));
}
|
You are posting the message to your component, it should go to the Form->Handle.
Also you are sending the 'Sender' as your last parameter, that is going to be
the Button, you should send a pointer to your component:
::PostMessage( Form1->Handle, APPWM_DELETE_COMP, 0, reinterpret_cast<int>(this);
or
::PostMessage( GetParentForm(this)->Handle, APPWM_DELETE_COMP, 0, reinterpret_cast<int>(this);
Todd
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Fri Dec 26, 2003 8:54 pm Post subject: Re: delete the parent component |
|
|
"Mike" <mikeh (AT) gmx (DOT) de> wrote
| Quote: | Following code would only delete the button, but not the whole componente.
|
That is because that code was specifically written to do just that. If you
want the component to delete itself, then just call 'delete this' in the
message handler instead of passing a component pointer as a message
parameter.
However, with that said, I strongly suggest that you *not* have the
component delete itself at all to begin with. You should leave that up to
the form to handle instead. You should never delete a component from within
its own message handlers, because you have no way of knowing at that time
whether the VCL still needs to work with your component after the message
handler returns, or if the form or other component is going to try to
reference the component later on as a result of another operation. You have
a potential of causing access violations and such be having the component
delete itself.
The easiest way to support what you are asking for is to instead have the
component expose an event that is triggered when the component's button is
clicked, and then the form can do whatever is needed to delete the component
when it is actually safe to do so. For example:
#define APPWM_DELETE_COMP (WM_APP + 100)
class TMyComp : public TPanel
{
private:
TButton *FButton;
TNotifyEvent FOnButtonClick;
void __fastcall ButtonClicked(TObject *Sender);
//...
public:
__fastcall TMyComp(TComponent *Owner);
__fastcall ~TMyComp();
//...
__published:
__property TNotifyEvent OnButtonClick = {read=FOnButtonClick,
write=FOnButtonClick};
//...
};
__fastcall TMyComp::TMyComp(TComponent *Owner)
: TPanel(Owner)
{
FButton = new TButton(this);
FButton->Parent = this;
FButton->OnClick = ButtonClicked;
//...
}
__fastcall TMyComp::~TMyComp()
{
//...
delete FButton;
}
void __fastcall TMyComp::ButtonClicked(TObject *Sender)
{
if( FOnButtonClick )
FOnButtonClick(this);
}
Then you can do the following:
void __fastcall TForm1::MyComp1ButtonClick(TObject *Sender)
{
PostMessage(Handle, APPWM_DELETE_COMP, 0,
reinterpret_cast<int>(Sender));
}
void __fastcall TForm1::WndProc(TMessage &Message)
{
if(Message.Msg == APPWM_DELETE_COMP)
{
delete reinterpret_cast<TMyComp*>(Message.LParam);
Message.Result = TRUE;
}
else
TForm::WndProc(Message);
}
Gambit
|
|
| Back to top |
|
 |
JD Guest
|
Posted: Sat Dec 27, 2003 3:46 pm Post subject: Re: delete the parent component |
|
|
"Remy Lebeau (TeamB)" <gambit47.no.spam (AT) no (DOT) spam.yahoo.com> wrote:
Please clearify for me how the
TNotifyEvent FOnButtonClick;
and the
__property TNotifyEvent OnButtonClick = {read=FOnButtonClick, write=FOnButtonClick};
declarations work together in regards to
if( FOnButtonClick ) FOnButtonClick( this );
~ JD
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Sat Dec 27, 2003 9:48 pm Post subject: Re: delete the parent component |
|
|
"JD" <nospam (AT) nospam (DOT) com> wrote
| Quote: | Please clearify for me how the
snip
declarations work together in regards to
if( FOnButtonClick ) FOnButtonClick( this );
|
TNotifyEvent is a typedef for a function pointer:
typedef void __fastcall (__closure *TNotifyEvent)(System::TObject*
Sender);
Thus FOnButtonClick is a simple variable that is a pointer to a class
method. Then the code is testing to see if that pointer has been assigned,
and if so then it calls the class method that is assigned to it.
Gambit
|
|
| Back to top |
|
 |
JD Guest
|
Posted: Sun Dec 28, 2003 7:20 pm Post subject: Re: delete the parent component |
|
|
"Remy Lebeau (TeamB)" <gambit47.no.spam (AT) no (DOT) spam.yahoo.com> wrote:
| Quote: | [...] Then the code is testing to see if that pointer has
been assigned, and if so then it calls the class method that
is assigned to it.
|
OK, but I don't get how and when it gets assigned.
~ JD
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Sun Dec 28, 2003 10:57 pm Post subject: Re: delete the parent component |
|
|
"JD" <nospam (AT) nospam (DOT) com> wrote
| Quote: | OK, but I don't get how and when it gets assigned.
|
If you look back at my earlier code, you will see that FOnButtonClick is
accessed via a property. It is the calling code's responsibility to assign
the property when it creates the actual component instance, ie:
TMyComp *comp = new TMyComp(this);
comp->OnButtonClick = MyComp1ButtonClick;
Gambit
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|