Solved

How to make a Delete in a Memo

Posted on 1998-09-30
10
300 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 4

Expert Comment

by:erajoj
Comment Utility
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
Comment Utility
10x ;->
0
 
LVL 10

Expert Comment

by:viktornet
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

6 Experts available now in Live!

Get 1:1 Help Now