Scrolling down a TRichEdit

I have this terminal program for telnet that reads in text from the net and displays it in a TRichEdit.  I want the incoming text to be visible when it comes in.  Right now, I have it set up to the SelStart to the end of the text and then I set the focus to the TRichEdit.  This works fine, except that on occasion, if I'm working in another application, focus will jump to my terminal program.  Is there a way do this without setting the focus to the RichEdit at all?
Gms300Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
viktornetConnect With a Mentor Commented:
10x
0
 
viktornetCommented:
SendMessage(RichEdit1.Handle, WM_VSCROLL, 10, 0); //Scroll down by 10
SendMessage(RichEdit1.Handle, WM_VSCROLL, -10, 0); //Scroll up by 10

Regards,
Viktor Ivanov
0
 
Gms300Author Commented:
I couldn't get that to work.  I don't know if I was doing something wrong or what.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
ZifNabCommented:
Hi Gms300,

this should work :

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    RichEdit1: TRichEdit;
    bFill: TButton;
    bUp: TButton;
    bDown: TButton;
    Edit1: TEdit;
    procedure bFillClick(Sender: TObject);
    procedure bUpClick(Sender: TObject);
    procedure bDownClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.bFillClick(Sender: TObject);
var i: integer;
begin
 for i:= 1 to 20 do begin
  richedit1.Lines.Add('This is line : '+intToStr(i));
 end;
end;

procedure TForm1.bUpClick(Sender: TObject);
var i : integer;
begin
for i:= 1 to StrToInt(Edit1.text) do
 SendMessage(RichEdit1.Handle, WM_VSCROLL, SB_LINEUP, 0);
end;

procedure TForm1.bDownClick(Sender: TObject);
var i : integer;
begin
for i:= 1 to StrToInt(Edit1.text) do
 sendmessage(richedit1.Handle, WM_VSCroll, SB_LINEDOWN,0);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 Edit1.Text := '1';
end;

end.

Regards, Zif.
0
 
viktornetCommented:
The problem is I guess that you didn't have more that 10 lines in your RichEdit component .... Simply try replacing the 10 with a 1 or 2 and try again.. I've been using that a few times and it always works....

btw- Zif, No offence but I think that your code is no different than mine...As a matter of fact if he uses your code it will take a while to scroll down 10 units... If he uses the way I told 'im to then the RichEdit will scroll right at the current plus ten, and yours it will just jump rfom line to line until it gets to the number in edit.text

Cheers,
Viktor
0
 
ZifNabCommented:
Viktornet,

 I must say that the answer you gave is partially correct, so by all means, you may have the points, but let me say this :

SendMessage(richedit1.handle, VM_VScroll,-10, 0) will not work, even if you would 've added more then 10 lines. Normally if you browse further then you have, it should stop at the last item.

SendMessage(richedit1.handle,VM_VScroll,-1,0) doesn't work either.

I tried these myself several times, even now again, because you gave them as an answer. I just can't get this scroll down to work.

What does work for scrolling down is this (this I found by trial and error):

SendMessage(richedit1.handle,VM_VScroll,0,1) does work for browsing down.

But, it is much better, to use windows constants itself!

So to scroll up, use parameter SB_LINEUP, do scroll down us SB_LINEDOWN.

To scroll to a given amount, use SB_THUMBPOSITION. (Scrolls to the absolute position. The current position is specified by the nPos parameter.)      

I agree about calling these 10 times isn't a great way of programming, but do you have a better one? (also for scrolling down?). Yes, perhaps, SB_THUMBPOSITION is better for moving fast. ps. there are other constants available too.

Regards, Zif.

0
 
Gms300Author Commented:
Thanks for the help.  viktornet, you were right.  Sorry about rejecting you answer the first time.  The points are yours, just answer again, please.  :)
0
 
ZifNabCommented:
Strange, does this rely works on your pc's?

SendMessage(richedit1.handle, VM_VScroll,-10, 0)

I can't get this at work.

Which versions of Delphi are you using and which OS.

Zif.
0
 
viktornetCommented:
D4 / Win95

Vik
0
 
ZifNabCommented:
and you gms300. I've D3 and Win98 on this PC.
0
 
viktornetCommented:
Win98 is f****** buggy, so is Delphi4...

Vik
0
 
ZifNabCommented:
well, I don't understand that it actually works.

Look at this :

WM_VSCROLL

nScrollCode = (int) LOWORD(wParam); // scroll bar value
nPos = (short int) HIWORD(wParam);  // scroll box position
hwndScrollBar = (HWND) lParam;      // handle of scroll bar

then

Parameters

nScrollCode

Value of the low-order word of wParam. Specifies a scroll bar value that indicates the user's scrolling request. This parameter can be one of the following values:

Value      Meaning
SB_BOTTOM      Scrolls to the lower right.
SB_ENDSCROLL      Ends scroll.
SB_LINEDOWN      Scrolls one line down.
SB_LINEUP      Scrolls one line up.
SB_PAGEDOWN      Scrolls one page down.
SB_PAGEUP      Scrolls one page up.
SB_THUMBPOSITION      Scrolls to the absolute position. The current position is specified by the nPos parameter.
SB_THUMBTRACK      Drags scroll box to the specified position. The current position is specified by the nPos parameter.
SB_TOP      Scrolls to the upper left.
nPos

Value of the high-order word of wParam. Specifies the current position of the scroll box if the nScrollCode parameter is SB_THUMBPOSITION or SB_THUMBTRACK; otherwise, nPos is not used.

hwndScrollBar

Value of lParam. Identifies the control if WM_VSCROLL is sent by a scroll bar control. If WM_VSCROLL is sent by a window's standard scroll bar, hwndScrollBar is not used.

Return Value

If an application processes this message, it should return zero.

so nbScrollbar can't be higher or equal to 10

(SendMessage(richedit1.handle, VM_VScroll,-10, 0))

thus I don't understand above code works.

What can work, like scrolling a X position up or down, should be :


SendMessage(Handle,EM_LINESCROLL,X,Y);

for scrolling down :

SendMessage(richedit1.Handle,EM_LINESCROLL,0,10);

Sorry for all this, but I just wants to know why your code works too.



0
 
ZifNabCommented:
the above should be nbScrollcode instead of nbScrollBar
0
 
ZifNabCommented:
f*** nScrollCode
0
 
viktornetCommented:
I have no idea why it works :-|
0
All Courses

From novice to tech pro — start learning today.