Gms300
asked on
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?
ASKER
I couldn't get that to work. I don't know if I was doing something wrong or what.
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.Hand le, 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.Hand le, WM_VSCroll, SB_LINEDOWN,0);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Edit1.Text := '1';
end;
end.
Regards, Zif.
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.Hand
end;
procedure TForm1.bDownClick(Sender: TObject);
var i : integer;
begin
for i:= 1 to StrToInt(Edit1.text) do
sendmessage(richedit1.Hand
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Edit1.Text := '1';
end;
end.
Regards, Zif.
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
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
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.hand le, 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.hand le,VM_VScr oll,-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.hand le,VM_VScr oll,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.
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.hand
SendMessage(richedit1.hand
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.hand
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.
ASKER
Thanks for the help. viktornet, you were right. Sorry about rejecting you answer the first time. The points are yours, just answer again, please. :)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Strange, does this rely works on your pc's?
SendMessage(richedit1.hand le, VM_VScroll,-10, 0)
I can't get this at work.
Which versions of Delphi are you using and which OS.
Zif.
SendMessage(richedit1.hand
I can't get this at work.
Which versions of Delphi are you using and which OS.
Zif.
D4 / Win95
Vik
Vik
and you gms300. I've D3 and Win98 on this PC.
Win98 is f****** buggy, so is Delphi4...
Vik
Vik
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.han dle, 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_LINE SCROLL,X,Y );
for scrolling down :
SendMessage(richedit1.Hand le,EM_LINE SCROLL,0,1 0);
Sorry for all this, but I just wants to know why your code works too.
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.han
thus I don't understand above code works.
What can work, like scrolling a X position up or down, should be :
SendMessage(Handle,EM_LINE
for scrolling down :
SendMessage(richedit1.Hand
Sorry for all this, but I just wants to know why your code works too.
the above should be nbScrollcode instead of nbScrollBar
f*** nScrollCode
I have no idea why it works :-|
SendMessage(RichEdit1.Hand
Regards,
Viktor Ivanov