Solved

How to make a Delete in a Memo

Posted on 1998-09-30
10
309 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…

726 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