1
votes

I want to set the caption to all controls (Tlabel, Tbutton, Teditlabel, Tbitbtn, TGroupBox, etc) and all components (TMenuItems, TActions) that have a caption from a language file.

My problem is that Caption is not public in TComponent, TControl or even TWinControl. Even more, some 'common' controls like TLabel/TBitBtn are not even derived from TWinControl.

Example:

void SetCaptionAll(TComponent *container)
{
   for (int i = 0; i < container->ComponentCount; i++)
      {
      TComponent *child = container->Components[i];
      child->Caption = ReadFromFile;    <-- This won't work. Caption is private
      }
}

Most important: I don't want to use a macro (I think this is what is called) like:

#define GetCtrlCaption(p)\
{ code here }

because this is not debugable.

I need C++ Builder example, but Delphi is accepted also.

1
Isn't it easier to just add language version ? (Project->Languages->Add)Vancalar
@Vancalar - Yes, might be. But I write code for a large/old application. It is already like this. I cannot use Project->LanguagesServer Overflow

1 Answers

3
votes

Works for all TControl descendants:

 for i := 0 to ControlCount - 1  do
    Controls[i].SetTextBuf('CommonText');

To walk through all controls including ones at childs like panels, you can use recursive traversal:

procedure SetControlText(Site: TWinControl; const s: string);
var
  i: Integer;
begin
  for i := 0 to Site.ControlCount - 1  do begin
     Site.Controls[i].SetTextBuf(PWideChar(s));
     if Site.Controls[i] is TWinControl then
       SetControlText(TWinControl(Site.Controls[i]), s);
  end;
end;

begin
   SetControlText(Self, 'CommonText');

For components like TMenuItems you can use RTTI - check whether component has property like Caption, Text etc and set new string.

Delphi RTTI example using old-style approach (new RTTI is available since D2010). Not sure that it works for Builder

 uses... TypInfo

 if IsPublishedProp(Site.Controls[i], 'Caption') then
   SetStrProp(Site.Controls[i], 'Caption', 'Cap');