Solved

How to make a Delete in a Memo

Posted on 1998-09-30
10
307 Views
Last Modified: 2010-04-06
How to make a Delete in a Memo, I mean the same thing as when you press the Del key.
0
Comment
Question by:elbaid
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 5

Accepted Solution

by:
scrapdog earned 100 total points
ID: 1341270
Include these functions in your code
------------------------------------

procedure DeleteFromMemo(Memo :TMemo; Line, Column :Integer);
var x :string;
begin
  x := Memo.Lines[Line];
  x := Copy(x,1,Column) + Copy(x,Column+2,Length(x));
  Memo.Lines[Line] := x;
end;

procedure DeleteAtCursor(Memo :TMemo);
var Lpos, CPos, LineLength :word;
begin
  Lpos := SendMessage(memo.Handle,EM_LINEFROMCHAR,Memo.SelStart,0);
  Cpos := SendMessage(memo.Handle,EM_LINEINDEX,Lpos,0);
  LineLength := SendMessage(memo.handle, EM_LINELENGTH, Cpos, 0);
  CPos := Memo.SelStart-CPos;
  DeleteFromMemo(Memo, LPos, CPos);
end;

--------------------------

how to call:
DeleteFromMemo(Memo, Line, Column)
DeleteAtCursor(Memo)

Calling DeleteFromMemo will delete the character at the position indicated at Line and Column.

--or--

Calling DeleteAtCursor will delete the character where the cursor is located in the memo.

Make sure that Windows is in your uses clauses in the unit you declare these in.


Scrapdog
0
 
LVL 4

Expert Comment

by:erajoj
ID: 1341271
Hi,
Now you're complicating things, the Borland guys have already thought of that.
It's much easier, one line of code, like this:

procedure TForm1.Button3Click(Sender: TObject);
begin
  Memo1.SelText := '';
  Memo1.SetFocus; // if you want to re-set focus to the memo
end;

/// John
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1341272
If you want to delete the last character you can do like this...

SetLength(Memo1.Text, Length(Memo1.Text) - 1);//replace the 1 with the number of characters to delete...you can't undo that though,.....

You can also do something like this if you want to delete from wihin the text somewhere then use something like this,,,,,

Let's say you want to delete the word BEGIN from the first place that it is met....Here is the code....

var
  nPos : Integer;
begin
  nPos := Pos('begin', Memo1.Text);
  if nPos > 0 then
    Delete(Memo1.Text, nPos, Length('begin'));//just to show you th LENGTH() if you dont know how long the word would be....
end;

Haven't tested this example but it should work....

Regards,
Viktor Ivanov
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 10

Expert Comment

by:viktornet
ID: 1341273
Hello.....Now as I read your question again I know what ya talking about....Here is how this can be done.....

procedure TForm1.Button1Click(Sender: TObject);
begin
    with Memo1 do begin
        SelLength := 1;
        ClearSelection;
        SetFocus;//Set the focus if you have done this by pressing a button...
    end;
end;

Regards,
Viktor Ivanov
0
 

Author Comment

by:elbaid
ID: 1341274
Sorry but thats not the answer i was looking for.

with your solution, the cursor return to the end of the line, and when you press a del key in a word processor, for exemple, it doesn't do that.

I'll give you the half of the points promised at least for your
effort.

but thanks anyway!
0
 
LVL 4

Expert Comment

by:erajoj
ID: 1341275
Viktor: You beat me this time (ClearSelection)! Damn, you're learning fast!

/// John, vice chairman of the mutual admiration society
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1341276
10x ;->
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1341277
This one works too...

procedure TForm1.Button1Click(Sender: TObject);
     begin
         with Memo1 do begin
             SelLength := 1;
             SelText := '';//as John said... ;->
             SetFocus;//Set the focus if you have done this by pressing a button...
         end;
     end;
0
 

Author Comment

by:elbaid
ID: 1341278
Thanks viktor for your answer, it's the best I got, but if you tried that code in a simple program, did you notice when you delete a line what's appearing?  three cute sqares as caracters. It's the real bug I have got.
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1341279
I never said it's the prefect one.... It will give you an idea with a lot shorter answer than scrapdog on how to accomplish this....Here is something else you might wanna try...

How about this???

  if Memo1.SelStart <> Length(Memo1.Text)-2 then
  begin
    Memo1.SelLength := 1;
    Memo1.ClearSelection;
  end;
  Memo1.SetFocus;

Regards,
Viktor Ivanov
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Path  to current project in Delphi. 2 92
Making delphi communicate with a c# service 16 111
Firemonkey DbLookupComboBox equivalent ? 2 55
Graphics32 under Delphi 10.1 Berlin 2 102
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

839 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