Delphi calling FlipChildren on THeadercontrol fails

I just stumbled upon a behavior in Delphi that seems like a mistake to me.

In Delphi, simply release the THeaderControl on the form and assign it at least one section. If you call FlipChildren (true) in this current form, an "index index of bounds" error occurs. Looks like the problem is in the FlipChildren TCustomHeaderControl procedure.

Since the same behavior is reproduced in different versions of Delphi (I tried Delphi 6 and Delphi 2010), I am a little reluctant to classify this as an error. Has anyone else encountered this problem before?

+4
source share
1 answer

. , Delphi 1, THeaderSections , . , , !

:

procedure TCustomHeaderControl.FlipChildren(AllLevels: Boolean);
var
  Loop, FirstWidth, LastWidth: Integer;
  ASectionsList: THeaderSections;
begin
  if HandleAllocated and
     (Sections.Count > 0) then
  begin
    { Get the true width of the last section }
    LastWidth := ClientWidth;
    FirstWidth := Sections[0].Width;
    for Loop := 0 to Sections.Count - 2 do Dec(LastWidth, Sections[Loop].Width);
    { Flip 'em }
    ASectionsList := THeaderSections.Create(Self);
    try
      for Loop := 0 to Sections.Count - 1 do with ASectionsList.Add do
        Assign(Self.Sections[Loop]);
      for Loop := 0 to Sections.Count - 1 do
        Sections[Loop].Assign(ASectionsList[Sections.Count - Loop - 1]);
    finally
      ASectionsList.Free;
    end;
    { Set the width of the last Section }
    if Sections.Count > 1 then
    begin
      Sections[Sections.Count-1].Width := FirstWidth;
      Sections[0].Width := LastWidth;
    end;
    UpdateSections;
  end;
end;

, , . , . .

, . , . THeaderSections , THeaderSections - . , ASectionsList.Add SectionsList!

,

for Loop := 0 to Sections.Count - 1 do with ASectionsList.Add do
  Assign(Self.Sections[Loop]);

, Sections.Count , ASectionsList.Count - . ,

for Loop := 0 to Sections.Count - 1 do
  Sections[Loop].Assign(ASectionsList[Sections.Count - Loop - 1]);

ASectionsList[Sections.Count - Loop - 1] .

. . , , , . , :

type
  THeaderControl = class(Vcl.ComCtrls.THeaderControl)
  public
    procedure FlipChildren(AllLevels: Boolean); override;
  end;

procedure THeaderControl.FlipChildren(AllLevels: Boolean);
var
  Index, Count: Integer;
  Widths: TArray<Integer>;
begin
  Count := Sections.Count;
  if Count>1 then
  begin
    SetLength(Widths, Count);
    for Index := 0 to Count-2 do
      Widths[Index] := Sections[Index].Width;
    Widths[Count-1] := ClientWidth;
    for Index := 0 to Count-2 do
      dec(Widths[Count-1], Widths[Index]);
    Sections.BeginUpdate;
    try
      for Index := 0 to Sections.Count-1 do
        Sections[Index].Width := Widths[Count-Index-1];
    finally
      Sections.EndUpdate;
    end;
  end;
end;

.

+2

Source: https://habr.com/ru/post/1525206/


All Articles