Solved

How to make a Delete in a Memo

Posted on 1998-09-30
10
301 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
loading dll from external path 1 211
Syntax Check Delphi Seattle IOS app without MAC ? 1 91
Strange behavior when a form is closed 6 51
Multiple image collision 13 69
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

920 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now