Go Premium for a chance to win a PS4. Enter to Win

x
Solved

# Get Line/Column from Memo Position and the reverse.

Posted on 2004-09-18
Medium Priority
379 Views
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;

0
Question by:Shellman
• 2

LVL 6

Accepted Solution

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

ID: 12092597

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

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

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

## Featured Post

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
###### Suggested Courses
Course of the Month9 days, 10 hours left to enroll