Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

TMemo or TRichEdit: Jump to a specific line?

1,266 views
Skip to first unread message

Lars

unread,
Jun 16, 1999, 3:00:00 AM6/16/99
to
Hello,
I try to go to a specific line in a TMemo or TRichEdit but I don't know how
it works. In TRichEdit I can use the FindText-method but I don't know the
text in the line, I only know the number of the line.
Can anybody help me???
I'm using D4 Prof. UpdatePack 3

Thanks.

Gabriel Siemoneit

unread,
Jun 16, 1999, 3:00:00 AM6/16/99
to
Hi Lars

If you want to have the text in a certain line use:

var Buffer: string;
LineNumber: integer;
...
LineNumber:=x;
Buffer:=Memo1/Richedit1.Lines[x-1];
...

Gabriel


Lars <papr...@gkss.de> schrieb in im Newsbeitrag:
7k7qmp$m4...@forums.borland.com...

Grega Stamac

unread,
Jun 16, 1999, 3:00:00 AM6/16/99
to
This is how you get the current position in a memo:

function MemoCursorLine(AnEditControl : TCustomEdit) : Longint;
begin
With AnEdit do
Result := 1+Perform(EM_LINEFROMCHAR, SelStart+SelLength, 0);
end;

function MemoCursorCol(AnEditControl : TCustomEdit) : Longint;
begin
With AnEdit do
Result := 1 + SelStart + SelLength - Perform(EM_LINEINDEX,
Perform(EM_LINEFROMCHAR, SelStart+SelLength, 0), 0);
end;

function MemoCountSelectedLines(AMemo : TMemo) : Longint;
begin
result := Ord(SelLength > 0);
With AMemo do
Result := result + Perform(EM_LINEFROMCHAR, SelStart + SelLength, 0)
- Perform(EM_LINEFROMCHAR, SelStart, 0);
end;

I copied this from somewhere I hope it helps.

Lars

unread,
Jun 16, 1999, 3:00:00 AM6/16/99
to
Thanx, but my problem is that I have(!!!) the correct line number but I
cannot move the cursor to this line.
E.g.: There is a long text in the Memo/RichEdit, the cursor is in the last
line and the required line is not visible. How to move the cursor and make
the invisible line visible???


Gabriel Siemoneit schrieb in Nachricht <7k7rj7$m5...@forums.borland.com>...


>Hi Lars
>
>If you want to have the text in a certain line use:
>
>var Buffer: string;
> LineNumber: integer;
>...
>LineNumber:=x;
>Buffer:=Memo1/Richedit1.Lines[x-1];
>...
>
>Gabriel
>
>
>Lars <papr...@gkss.de> schrieb in im Newsbeitrag:
>7k7qmp$m4...@forums.borland.com...

Lars

unread,
Jun 16, 1999, 3:00:00 AM6/16/99
to
Thanx, but my problem is that I have(!!!) the correct line number but I
cannot move the cursor to this line.
E.g.: There is a long text in the Memo/RichEdit, the cursor is in the last
line and the required line is not visible. How to move the cursor and make
the invisible line visible???


Grega Stamac schrieb in Nachricht <37677589...@sis.si>...


>This is how you get the current position in a memo:
>
>function MemoCursorLine(AnEditControl : TCustomEdit) : Longint;
>begin
> With AnEdit do
> Result := 1+Perform(EM_LINEFROMCHAR, SelStart+SelLength, 0);
>end;
>
>function MemoCursorCol(AnEditControl : TCustomEdit) : Longint;
>begin
> With AnEdit do
> Result := 1 + SelStart + SelLength - Perform(EM_LINEINDEX,
>Perform(EM_LINEFROMCHAR, SelStart+SelLength, 0), 0);
>end;
>
>function MemoCountSelectedLines(AMemo : TMemo) : Longint;
>begin
> result := Ord(SelLength > 0);
> With AMemo do
> Result := result + Perform(EM_LINEFROMCHAR, SelStart + SelLength, 0)
> - Perform(EM_LINEFROMCHAR, SelStart, 0);
>end;
>
>I copied this from somewhere I hope it helps.
>
>Lars wrote:
>

Guido Gybels

unread,
Jun 16, 1999, 3:00:00 AM6/16/99
to
Hi Lars,

Have a look at the EM_CHARFROMPOS and EM_SETSEL messages in the online help.
For TMemo, you could also use the SelStart property.

GUIDO GYBELS
Programmer (GDG GROEP BELGIUM)

Lars heeft geschreven in bericht <7k812v$m5...@forums.borland.com>...

Henry Clarius

unread,
Jun 16, 1999, 3:00:00 AM6/16/99
to
Code Snippet from some code I use to take me to a given line in a richedit.
I am not very good at explaining, but this works for me.

I have a listview and a richedit.
When I click on line in the Listview, it gets the line number (First_Rec which
I have already determined) of the richedit and takes the user at the beginning
of the line.

RAWDATA is my richedit ListView1 is my Listview

type
Record_Unit=record
Rec_Unit:String[9];
First_Rec:Integer;
Count:Integer;
end;
MyRC=array[1..200] of Record_Unit;
var
RCUN:^MyRC;


procedure TRecCount.ListView1Click(Sender: TObject);
var
r,i:Integer;
z:LongInt;
s:String;
begin
if ListView1.ItemFocused=nil then
MessageDlg('No Selection',MtInformation,[mbOK],0)
else
begin
r:=ListView1.Items.IndexOf(ListView1.ItemFocused)+1;{r is the index of
an array I use to get the line number in my richedit}
RawData.SetFocus;
s:=RawData.Lines[RCUN^[r].First_Rec-1]; {s is the line number in my
richedit}
z:=RawData.GetTextLen;
i:=RawData.FindText(s,0,z,[stMatchcase]);
if i=-1 then MessageDlg('New Record',MtInformation,[mbOK],0)
else RawData.SelStart:=i;
end;
end;

Peter Below (TeamB)

unread,
Jun 16, 1999, 3:00:00 AM6/16/99
to
> I try to go to a specific line in a TMemo or TRichEdit but I don't know how
> it works. In TRichEdit I can use the FindText-method but I don't know the
> text in the line, I only know the number of the line.
> Can anybody help me???
> I'm using D4 Prof. UpdatePack 3
>
Incredible, 3 answers and none of them read the question before posting <g>.

with richedit1 do begin
selstart := perform( em_lineindex, linenumber, 0 );
perform( em_scrollcaret, 0, 0 );
end;

The EM_LINEINDEX message returns the character index of the first character on
a given line, assigning that to selstart moves the caret to that position. The
control will only automatically scroll the caret into view if it has the focus,
thus the EM_SCROLLCARET.


Peter Below (TeamB) 10011...@compuserve.com)
No e-mail responses, please, unless explicitly requested!


John McTaggart

unread,
Jun 17, 1999, 3:00:00 AM6/17/99
to
On Wed, 16 Jun 1999 12:50:24 +0200, "Lars" <papr...@gkss.de> wrote:

>Thanx, but my problem is that I have(!!!) the correct line number but I
>cannot move the cursor to this line.
>E.g.: There is a long text in the Memo/RichEdit, the cursor is in the last
>line and the required line is not visible. How to move the cursor and make
>the invisible line visible???

Try this...

RichEdit.SelStart := Perform(EM_LINEINDEX, CurrentLine, 0);
Perform(EM_SCROLLCARET, 0, 0);

I hope it works... <g>

If not, it's a simple fix.

John McTaggart

Lars

unread,
Jun 17, 1999, 3:00:00 AM6/17/99
to
Thanks Henry,

it works. But have a look at John McTaggart's and Peter Below's advices.
It's better ;)


Henry Clarius schrieb in Nachricht <3767DB9C...@epa.gov>...

>> I try to go to a specific line in a TMemo or TRichEdit but I don't know
how
>> it works. In TRichEdit I can use the FindText-method but I don't know the
>> text in the line, I only know the number of the line.
>> Can anybody help me???
>> I'm using D4 Prof. UpdatePack 3
>>

>> Thanks.
>

Lars

unread,
Jun 17, 1999, 3:00:00 AM6/17/99
to
Thanks Peter,

but one question: why do you use the second Perform(EM_SCROLLCARET, 0, 0)? I
tried it without and it works great.

Lars

Lars

unread,
Jun 17, 1999, 3:00:00 AM6/17/99
to
OK, forget it, I see why you do this (perhaps I was a little bit too
fast...) ;)

Lars

Lars schrieb in Nachricht <7ka8b5$on...@forums.borland.com>...

0 new messages