In article <37446464.2D0E6...@AdvancedRelay.com>, David Tiktin
Quote
<tik...@AdvancedRelay.com> writes:
>I use a TMemo to display a dynamically updated session log. I would
>like
>the text to scroll so that the latest entry is always visible. In
>Visual
>Basic I use a TextBox like this with the code:
> SessionLog.text = SessionLog.text & str & vbCrLf
> SessionLog.SelStart = Len(SessionLog.text)
>Setting SelStart to the end of the text scrolls the last line into view.
>In Delphi, I tried the code:
> SessionLog.Text := SessionLogMemo.Text + str + #13#10;
> SessionLog.SelStart := Length(SessionLogMemo.Text);
>The new text is added but the TMemo does not scroll. I consulted
>various
>FAQs and Delphi Tip sheets which suggested using PostMessage() with
>WM_VSCROLL, so I added:
> PostMessage(SessionLog.Handle, WM_VSCROLL, SB_BOTTOM, 0);
>This does nothing! Then I tried SB_PAGEDOWN, which works, but only for
>a
>while. Eventually, the scrolling fails to keep up and the last line
>moves
>further and further out of the window. I tried several other SB_XXXXXX
>parameters without success. If I stop the session, the scroll bars work
>properly.
This is a note I produced on TMemo scrolling. Note that some of the methods
work only if the TMemo has focus.
.
==========================
TMemo and Scrolling
If you just add lines to the TMemo (without BeginUpdate / EndUpdate), the
viewport scrolls to the last line added. However if you use BeginUpdate / add
lines / EndUpdate, then the viewport does not scroll.
If you want to position the viewport, you can then use the windows messages in
a Perform method to move the viewport. Some of the messages (with their
parameters) available are :-
(EM_Scroll, nScroll, 0) scrolls an amount defined by the second parameter
constant, the third parameter must be zero.
where nScroll is one of the constants SB_LINEDOWN, SB_LINEUP, SB_PAGEDOWN, or
SB_PAGEUP. The move is limited to those available, ie it does not scroll so
that the first or the last line is out of the viewport.
(EM_LineScroll, cxScroll, cyScroll)
where cxScroll and cyScroll are integers (+ve or -ve) to scroll characters
(cxScroll) or lines (cyScroll). The line move is limited to those available.
(EM_ScrollCaret, 0, 0) moves the viewport so that the caret is in the viewport.
where the second and third parameters must be zero.
Memo1.Perform(EM_SetSel, 0, 0); {move caret to top}
Memo1.Perform(EM_LineScroll, 0, Memo1.Lines.Count * -1); {move to top}
{or Memo1.Perform(EM_ScrollCaret, 0, 0); scroll caret into view}
Memo1.SetFocus;
Memo1.Perform(EM_SetSel, Memo1.GetTextLen, Memo1.GetTextLen); {move caret to
bottom}
Memo1.Perform(EM_LineScroll, 0, Memo1.Lines.Count); {move to bottom}
{or Memo1.Perform(EM_ScrollCaret, 0, 0); scroll caret into view}
Memo1.SetFocus;
==========================
Alan Lloyd
alangll...@aol.com