On my form there's a ScrollBox. I populate it with 2 dynamically created Memos like this:
TotalHeight := 0;
NewMemo := TMemo.Create(nil);
NewMemo.Parent := ScrollBox1;
NewMemo.Left := 0;
NewMemo.Top := TotalHeight;
NewMemo.Lines.Text := Memo1.Lines.Text;
NewMemo.FitHeightToContent; //SEE BELOW !!!
TotalHeight := TotalHeight + NewMemo.Height;
NewMemo := TMemo.Create(nil);
NewMemo.Parent := ScrollBox1;
NewMemo.Left := 0;
NewMemo.Top := TotalHeight;
NewMemo.Lines.Text := Memo1.Lines.Text;
NewMemo.FitHeightToContent;
TotalHeight := TotalHeight + NewMemo.Height;
ScrollBox1.VertScrollBar.Range := TotalHeight;
FitHeightToContent procedure code (taken from SO):
procedure TMemoHelper.FitHeightToContent;
var
DC: HDC;
SaveFont : HFont;
Metrics : TTextMetric;
LineHeight: Integer;
Increase: Integer;
LC: Integer;
begin
DC := GetDC(Handle);
SaveFont := SelectObject(DC, Font.Handle);
GetTextMetrics(DC, Metrics);
SelectObject(DC, SaveFont);
ReleaseDC(Handle, DC);
LineHeight := Metrics.tmHeight;
Increase := Height;
LC := Lines.Count;
if LC < 1 then
LC := 1;
Height := LC * LineHeight + 8;
Increase := Height - Increase;
end;
Memos have their word-wrap as false. Their height is such that if one memo is fully visible, the other's got only about 1 line of text visible.
THE PROBLEM: when I click on NOT fully visible memo, the scrollbox gets scrolled up or down to show the clicked-upon memo in full.
I experimented with memo sizes and problem persists. With different memo sizes this strange scrolling is different and not always fully exposes clicked-upon memos.