1

I want to add an string into specified place of a Memo Edit in delphi, how can I do this? I mean I want to know where the mouse cursor is within TMemo, and then add a string to this position. Is that possible?

NGLN
  • 43,011
  • 8
  • 105
  • 200
Rojin
  • 109
  • 3
  • 6
  • 2
    This is really very, very basic stuff you should be able to find on most tutorials. Something like MemoEdit1.Lines.Add('xxx') or MemoEdit1.Lines.Text:=MemoEdit1.Lines.Text+'xxx'. You should some more research before posting. – Frazz May 17 '14 at 07:15
  • 2
    Thank you for your help. But I think you miss understanding my question, of course I know using your help suggestion like memoEdit1.Lines.Add('XXX') or the other one, but the thing that I want to do is like this: Adding a string into a line, but not a specified line! but some where the mouse points, is it possible? could you help me? I can use OnMouseUp or OnMouseMove events too. – Rojin May 17 '14 at 07:35
  • Then you should explain the problem better and provide examples of what you've tried. – Frazz May 17 '14 at 07:37
  • Do you want to insert at the cursor position or the caret position? Please edit your question so that we know what you want. – David Heffernan May 17 '14 at 07:48
  • Sounds like drag'n'drop – Sir Rufo May 17 '14 at 08:32
  • 2
    I want to insert at the cursor position. – Rojin May 17 '14 at 09:15
  • 1
    Please edit the question, instead of using comments to add information not provided in the question itself. This may be helpful to others in the future... but not as is. – Frazz May 17 '14 at 11:06

4 Answers4

9

You can use EM_CHARFROMPOS to determine the character position of where the mouse cursor points:

var
  Pt: TPoint;
  Pos: Integer;
begin
 Pt := Memo1.ScreenToClient(Mouse.CursorPos);
 if (Pt.X >= 0) and (Pt.Y >= 0) then begin
   Pos := LoWord(Memo1.Perform(EM_CHARFROMPOS, 0, MakeLong(Pt.x, Pt.Y)));
   Memo1.SelLength := 0;
   Memo1.SelStart := Pos;
   Memo1.SelText := '.. insert here ..';
 end;
end;
Sertac Akyuz
  • 54,131
  • 4
  • 102
  • 169
4

If you want to place your string at the caret position of your memo you can use the following code:

procedure TForm1.InsertStringAtcaret(MyMemo: TMemo; const MyString: string);
begin
  MyMemo.Text :=
  // copy the text before the caret
    Copy(MyMemo.Text, 1, MyMemo.SelStart) +
  // then add your string
    MyString +
  // and now ad the text from the memo that cames after the caret
  // Note: if you did have selected text, this text will be replaced, in other words, we copy here the text from the memo that cames after the selection
    Copy(MyMemo.Text, MyMemo.SelStart + MyMemo.SelLength + 1, length(MyMemo.Text));

  // clear text selection
  MyMemo.SelLength := 0;
  // set the caret after the inserted string
  MyMemo.SelStart := MyMemo.SelStart + length(MyString) + 1;
end;
  • 1
    Just `MyMemo.SelText := MyString` is enough. Possibly with `MyMemo.SelLength := 0` in front, depending on OP's wishes. – NGLN May 17 '14 at 09:42
  • 3
    But it's not what you asked! You asked for mouse cursor position, not caret position. Sure, a mouse click at the mouse position will result in the "same" caret position, but why then not asking about the caret position? – NGLN May 17 '14 at 09:45
0

You can add a string to the bottom of the ".Lines" member: MyMemo.Lines.add('MyString').

You can also replace a string in any (already existing) position in "Lines" you want: MyMemo.Lines[2] := 'MyString'.

Finally, you can insert anywhere you want: MyMemo.Lines.Insert(2, 'MyString')

FoggyDay
  • 11,962
  • 4
  • 34
  • 48
0

It's very simple (NGLN's answer):

MyMemo.SelText := MyString;