"noobcoder" <
XXXX@XXXXX.COM >wrote in message
Quote
Currently I'm doing it this way (just found out of this method
after I posted my original message):
That is not handling any nested components, and it does not handle any
descendant classes, either. If you are going to implement such a
manual loop, then you should implement it as a recursive function, ie:
void __fastcall SetMultiLabelCaptionIndexes(TWinControl
*ParentControl, int Index)
{
for(int i = 0; i < ParentControl->ControlCount; ++i)
{
TControl *pControl = ParentControl->Controls[i];
TMultiLabel *label = dynamic_cast<TMultiLabel*>(pControl);
if( label )
label->CaptionIndex = Index;
TWinControl *pWC = dynamic_cast<TWinControl*>(pControl);
if( pWC )
SetMultiLabelCaptionIndexes(pWC, Index);
}
}
Then you can call it like this:
void __fastcall TForm1::DoSomething()
{
SetMultiLabelCaptionIndexes(this, myIndex);
}
Depending on how many controls you actually have on your form, though,
this can become very inefficient. I still think your best approach is
the singleton approach. You can implement it inside your component
package directly. For example (untested):
--- MultiLabel.h ---
class TMultiLabel : public TLabel
{
private:
TStrings* FCaptionList;
int FCaptionIndex;
void __fastcall CaptionListChanged(TObject *Sender);
void __fastcall SetCaptionList(TStrings *Value);
void __fastcall SetCaptionIndex(int Value);
public:
__fastcall TMultiLabel(TComponent *Owner);
__fastcall ~TMultiLabel();
__published:
__property TStrings* CaptionList = {read=FCaptionList,
write=SetCaptionList};
__property int CaptionIndex = {read=FCaptionIndex,
write=SetCaptionIndex, default=-1};
};
--- MultiLabel.cpp ---
#include "MultiLabel.h"
class TMultiLabelRegistrar
{
private:
TList *FList;
bool FBroadcasting;
public:
TMultiLabelRegistrar();
~TMultiLabelRegistrar();
void CaptionIndexChanged(TMultiLabel *Label, int Value);
void Register(TMultiLabel *Label);
void Unregister(TMultiLabel *Label);
};
TMultiLabelRegistrar *Registrar = NULL:
TMultiLabelRegistrar::TMultiLabelRegistrar()
{
FList = new TList;
}
TMultiLabelRegistrar::~TMultiLabelRegistrar()
{
delete FList;
}
void TMultiLabelRegistrar::CaptionIndexChanged(TMultiLabel *Label,
int Value)
{
if( Broadcasting ) return;
Broadcasting = true;
try
{
for(int i = 0; i < FList->Count; ++i)
{
TMultiLabel *pLabel =
static_cast<TMultiLabel*>(FList->Items[i]);
if( pLabel != Label )
pLabel->CaptionIndex = Value;
}
}
__finally
{
Broadcasting = false;
}
}
void TMultiLabelRegistrar::Register(TMultiLabel *Label)
{
FList->Add(Label);
}
void TMultiLabelRegistrar::Unregister(TMultiLabel *Label)
{
FList->Remove(Label);
if( FList->Count == 0 )
{
delete Registrar;
Registrar = NULL;
}
}
__fastcall TMultiLabel::TMultiLabel(TComponent *Owner)
: TLabel(Owner)
{
FCaptionIndex = -1;
FCaptionList = new TStringList;
static_cast<TStringList*>(FCaptionList)->OnChange =
CaptionListChanged;
if( !Registrar )
Registrar = new TMultiLabelRegistrar;
Registrar->Register(this);
}
__fastcall ~TMultiLabel()
{
delete FCaptionList;
if( Registrar )
Registrar->Unregister(this);
}
void __fastcall TMultiLabel::CaptionListChanged(TObject *Sender)
{
if( (FCaptionIndex>= 0) && (FCaptionIndex <
FCaptionList->Count) )
Caption = FCaptionList->Strings[FCaptionIndex];
else
SetCaptionIndex(-1);
}
void __fastcall TMultiLabel::SetCaptionList(TStrings *Value)
{
FCaptionList->Assign(Value);
}
void __fastcall TMultiLabel::SetCaptionIndex(int Value)
{
if( Value < -1 )
Value = -1;
else if( Value>= FCaptionList->Count )
Value = (FCaptionList->Count - 1);
if( FCaptionIndex != Value )
{
FCaptionIndex = Value;
if( (FCaptionIndex>= 0) && (FCaptionIndex <
FCaptionList->Count) )
Caption := FCaptionList->Strings[FCaptionIndex];
else
Caption := "";
Registrar->CaptionIndexChanged(this, FCaptionIndex);
}
}
Gambit