Solved

How to make a Delete in a Memo

Posted on 1998-09-30
10
303 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

785 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