No improvements to this issue after trying several different ways to get at it. However, I am able to isolate more precisely when this happens.
First some code to show what I tried (not all my attempts):
// TDiscssionPanel is an extension of TPanel and is in my Discussion unit.
constructor TDiscussionPanel.Create(AOwner: TComponent; DText: string; DTop: integer);
begin
inherited Create(AOwner);
Self.Parent := AOwner as TWinControl;
Self.Top := DTop;
// added this if block after posting about the problem in this forum but no change in results.
if Parent.ControlCount > 1 then
begin
Self.AnchorSide[akTop].Control := Parent.Controls[Parent.ControlCount-2]; //assign sibling
Self.AnchorSide[akTop].Side := asrBottom; //anchor top side to bottom of sibling
end;
Self.Align := alTop;
// some settings (Panel background colors etc.) removed for conciseness.
end:
The DiscussionPanel Constructor method is called from the main form unit:
procedure TForm1.CreateTxtDisplay(AText: string);
var
newDiscussionPanel:TDiscussionPanel;
newTop: Integer;
begin
if Scrollbox1.ControlCount = 0 then newTop := 0
else newTop := Scrollbox1.Controls[Scrollbox1.ControlCount-1].Top +
Scrollbox1.Controls[Scrollbox1.ControlCount-1].Height;
try
// Create a new Discussion Panel
newDiscussionPanel := TDiscussionPanel.Create(Scrollbox1, Atext, newTop);
except
on E: Exception do
begin
newDiscussionPanel.Free;
raise;
end;
end;
end;
Other attempts included adding a "wait" flag to the TScrollbox in my (already existing) helper unit and setting it on when a call to create a
TDisucssionPanel is made and not calling another create method until it is off. ...but, when do you turn it off? Turning it off at the end of the
CreateTxtDisplay procedure didn't change anything. Turning it off at the end of the
TDiscussionPanel.Create method itself, didn't change anything. Turning it off in the
OnPaint method of the
TDiscussionPanel created an infinite loop.
With all of that said, this problem ONLY occurs on loading a session that has more than 4 panels AND when they do not fit in the
ScrollBox display window. If I make the Form full screen, I can get more panels fitting in the
ScrollBox. If they all fit this issue does not occur. If I shrink the Form and reload, the problem reccurs. If the Panels load fine, no problems occur on redraws of the
ScrollBox from resizing, changing visibility etc. Adding Panels during a session doesn't create any problems (but they are added one at a time so not surprising).
Edit: In my previous response I indicated that I didn't set any alignment. Although I calculate the Top of the panel myself, I did already set the alignment. Removing my calculation makes the situation worse--they never load in order (Top to bottom, but some mishmash of bottom to top).