Solved

Get Line/Column from Memo Position and the reverse.

Posted on 2004-09-18
4
298 Views
Last Modified: 2010-04-05
I've got a memo, some text in it, and I need to set the cursor to a specific line and a specific column. So how do I get the value I need for SelStart?
The reverse operation would also be interesting. What I basically need is:
function MemoPositionToLineColumn(M: TMemo; Position: Integer): TPoint;
function LineColumntToMemoPosition(M: TMemo; LineColumn: TPoint): Integer;

Thanks for your answers.
0
Comment
Question by:Shellman
  • 2
4 Comments
 
LVL 6

Accepted Solution

by:
vadim_ti earned 100 total points
ID: 12092341
function MemoPositionToLineColumn(M: TMemo; Position: Integer): TPoint;
var
  pp:TPoint;
  i, Line, Col: Integer;
begin
  i:=0; Line := 0; Col := 0;
  while position >= Length(M.Lines[i])+2 do begin
    inc(Line);
    Dec(position, Length(M.Lines[i])+2);
    Inc(i);
  end;
  col := position;
  pp.X := Line; pp.Y := Col;
  result := pp;
end;

function LineColumntToMemoPosition(M: TMemo; LineColumn: TPoint): Integer;
var
  i, Line, Col: Integer;
begin
  Line := LineColumn.X;
  Col := LineColumn.Y;
  i:=0; result := Col;
  while i < Line do begin
    Inc(result, Length(M.Lines[i])+2);
    Inc(i);
  end;
end;

0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 12092597
just from head

function MemoPositionToLineColumn(M: TMemo; Position: Integer): TPoint;
begin
  Result := Point(-1,-1);
  If Assgined(M) then
    M.Perform(EM_POSFROMCHAR,integer(result),m.SelStart);
end;


function LineColumntToMemoPosition(M: TMemo; LineColumn: TPoint): Integer;
begin
  result := -1;
  If Assgined(M) then
    result := m.Perform(EM_CHARFROMPOS,0,Integer(LineColumn ));
end;

just from head, not tested, maybe the conversions are not right

meikl ;-)
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 12123234
Here's some code I wrote to get the row, column in a Memo, use it to your delight:

function RichRow(m: TCustomMemo): Longint;
begin
  Result := m.PerForm(EM_LINEFROMCHAR,m.SelStart,0);
end;

function RichCol(m: TCustomMemo; Row: Integer): Longint;
begin
  Result := m.SelStart-m.Perform(EM_LINEINDEX,Row,0);
end;

procedure TForm1.Memo1KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
  Memo: TMemo;
  Row: Integer;
  Col: Integer;
begin
  Memo := TMemo(Sender);
  Row := RichRow(Memo);
  Col := RichCol(Memo, Row);
  Caption := Format('%d : %d', [Row, Col]);
  StatusBar1.Panels.Items[sbpPosition].Text := Format('%d : %d',
           [Row, Col]);
end;

procedure TForm1.Memo1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  Memo: TMemo;
  Row: Integer;
  Col: Integer;
begin
  Memo := TMemo(Sender);
  Row := RichRow(Memo);
  Col := RichCol(Memo, Row);
  Caption := Format('%d : %d', [Row, Col]);
  StatusBar1.Panels.Items[sbpPosition].Text := Format('%d : %d',
           [Row, Col]);
end;
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 12123245
Oh, sbpPosition is a constant that tells me which Panel to place the text in a multi-panel statusbar.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to use Hashing sha1 in Delphi 2010 4 206
Delphi application Soap connection 5 96
Making delphi communicate with a c# service 16 91
Graphics32 under Delphi 10.1 Berlin 2 40
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now