Mirror

How to adjust the width of THeaderControl sections automatically, when the first section is resized (Views: 100)


Problem/Question/Abstract:

I have THeaderControl with 4 sections. When I resize the first section, I need the remaining 3 sections to resize proportionally to their size to ensure that they don't go out of visibility (I mean that the last section end position always remains the same). I tried to use OnSectionResize or OnSectionTrack events, but without success. Can anyone help?

Answer:

procedure TForm1.ResizeMyHeader();
const
  MIN_COL_WIDTH = 10;
var
  iColWidth: Integer;
  iColExtra: Integer;
  iSection: Integer;
begin
  if HeaderControl1.Sections.Count > 1 then
  begin
    iColWidth := HeaderControl1.Width;
    Dec(iColWidth, HeaderControl1.Sections[0].Width);
    iColWidth := (iColWidth div (HeaderControl1.Sections.Count - 1));
    iColExtra := HeaderControl1.Width - (HeaderControl1.Sections[0].Width +
      (HeaderControl1.Sections.Count - 1) * iColWidth);
    {resize the 2nd to the last columns}
    if iColWidth >= MIN_COL_WIDTH then
    begin
      for iSection := 1 to HeaderControl1.Sections.Count - 2 do
      begin
        HeaderControl1.Sections[iSection].Width := iColWidth;
      end;
      HeaderControl1.Sections[HeaderControl1.Sections.Count - 1].Width := iColWidth + iColExtra;
    end;
  end;
end;

procedure TForm1.HeaderControl1_OnSectionResize(HeaderControl: THeaderControl;
  Section: THeaderSection);
begin
  Self.ResizeMyHeader;
end;

procedure TForm1.Form_OnResize(Sender: TObject);
begin
  Self.ResizeMyHeader;
end;

<< Back to main page