Search code examples
c++c++buildervcl

How to embed a VCL TEdit control in a THeaderSection?


I would like to embed an TEdit control on a THeader control.

UPDATE:

Based on Remy's answer below, I came up with the following code that works nicely.

int secsWidth(0);
for(int i = 0; i < HeaderControl1->Sections->Count; i++)
{
    secsWidth += HeaderControl1->Sections->Items[i]->Width;
}

TPoint p1(HeaderControl1->Left, HeaderControl1->Top);
TPoint p2 = HeaderControl1->ClientToScreen(p1); //<--- ???
mEdit->Left = p1.X + secsWidth;
mEdit->Top = p1.Y;
mEdit->Parent = HeaderControl1;

The above code is executed in a frames constructor. The frame is dynamically created.

For whatever reason, if the line above, marked "??", is commented out, the edit control is not visible. Observe that the line is not really doing anything..!

I guess the call create some necessary internals for the HeaderControl component?


Solution

  • Yes, but not directly.

    THeaderSection is not a UI control of its own, it is just a collection item in memory, so it can't host any child controls. The only UI control is the THeaderControl itself.

    Being that THeaderControl is a TWinControl descendant, at runtime only (not at design-time) you can set the THeaderControl as the Parent for the TEdit, and then you can position the TEdit relative to the desired header section as needed, using THeaderSection.Left, THeaderSection.Width, and THeaderControl.Height properties to calculate the bounding rectangle of the header section within which you want the TEdit to appear.