Discussion:
Pass thru OnClick...
(too old to reply)
Gene Buckle
2008-07-23 16:17:51 UTC
Permalink
I've got a TLabel on a custom control that I need to be able to have
it generate the OnClick event for the parent that it's living on.

Right now I've got it set up like this:

CustomControl->OnClick = ClickHandler;
CustomControl->Label->OnClick = ClickHandler;

Now what ends up happening is that if you click the label instead of the
surrounding panel, the Label OnClick event is fired. Unfortunately
this makes the Sender a TLabel and not the CustomControl that it must be.

How can I manipulate it in such a way that if the label is clicked, it
will pass the correct Sender?

I've tried:

x = ((TLabel*)Sender)->Tag;
if (x == 2112) {
// we're coming from the label on the TFunctionPanel...
workPanel = ((TFunctionPanel*)Sender)->Parent;
}

However, the compiler errors with "Cannot Convert 'TWinControl* const' to
'TFunctionPanel *'

Help! :)

g.
--
Proud owner of F-15C 80-0007
http://www.f15sim.com - The only one of its kind.
Remy Lebeau (TeamB)
2008-07-23 18:11:39 UTC
Permalink
Post by Gene Buckle
I've got a TLabel on a custom control that I need to be able to have
it generate the OnClick event for the parent that it's living on.
CustomControl->OnClick = ClickHandler;
CustomControl->Label->OnClick = ClickHandler;
Now what ends up happening is that if you click the label instead of the
surrounding panel, the Label OnClick event is fired. Unfortunately
this makes the Sender a TLabel and not the CustomControl that it must be.
Assign a different handler to the TLabel, and have that handler call the
CustomControl's Click() method. That will change the Sender, ie:

CustomControl->OnClick = CustomControlClickHandler;
CustomControl->Label->OnClick = LabelClickHandler;

// Click() is protected at the TControl level, promote it manually
class TControlAccess : public TControl
{
public
DYNAMIC void __fastcall Click() { TControl::Click(); }
};

void __fastcall TWhatever::LabelClickHandler(TObject *Sender)
{
TLAbel *Lbl = static_cast<TLabel*>;
((TControlAccess*) Label->Parent)->Click();
}

void __fastcall TWhatever::CustomControlClickHandler(TObject *Sender)
{
//...
}

On the other hand, if the CustomControl is actually creating the TLabel
internally, then it can assign a private handler to it, ie:

class TMyCustomControl : public TWhatever
{
private:
TLabel *FLabel;
void __fastcall LabelClicked(TObject *Sender);
//...
public:
__fastcall TMyCustomControl(TComponent *Owner);
//...
};

__fastcall TMyCustomControl::TMyCustomControl(TComponent *Owner)
{
FLabel = new TLabel(this);
//...
FLabel->OnClick = LabelClicked;
}

void __fastcall TMyCustomControl::LabelClicked(TObject *Sender)
{
Click();
}


Gambit
Gene Buckle
2008-07-23 19:58:40 UTC
Permalink
Post by Remy Lebeau (TeamB)
On the other hand, if the CustomControl is actually creating the TLabel
class TMyCustomControl : public TWhatever
{
TLabel *FLabel;
void __fastcall LabelClicked(TObject *Sender);
//...
__fastcall TMyCustomControl(TComponent *Owner);
//...
};
__fastcall TMyCustomControl::TMyCustomControl(TComponent *Owner)
{
FLabel = new TLabel(this);
//...
FLabel->OnClick = LabelClicked;
}
void __fastcall TMyCustomControl::LabelClicked(TObject *Sender)
{
Click();
}
...and Gambit comes through yet again with a win. *laughs*

Thanks!

g.
--
Proud owner of F-15C 80-0007
http://www.f15sim.com - The only one of its kind.
Tom420
2008-07-27 05:16:19 UTC
Permalink
Post by Gene Buckle
...and Gambit comes through yet again with a win. *laughs*
Thanks!
g.
I would change his name as it appears in the From field to:

Remy Lebeau (MasterB)

Loading...