-1

I'm having a TEdit, TMemo and a button. When the user presses the button, I want to delete from that memo control a line matching the text entered in the edit box. If no matching line is found, some sort of "line not found" message should be displayed.

I'm new to Delphi and don't know any code for this, but in theory it should work on the principle of searching the TMemo until it finds a line that matches Edit.Text and then deletes that specific line.

Could someone show me how to delete a line found by text from a TMemo control?

TLama
  • 75,147
  • 17
  • 214
  • 392

1 Answers1

1

Use the IndexOf function to find the index of an item by text in a string list. If this function returns value different from -1, the string was found and you can delete it from the list by using the Delete method passing the found index:

var
  Index: Integer;
begin
  Index := Memo.Lines.IndexOf(Edit.Text);
  if Index <> -1 then
    Memo.Lines.Delete(Index)
  else
    ShowMessage('Text not found!');
end;

Note, that the IndexOf function is case insensitive.

TLama
  • 75,147
  • 17
  • 214
  • 392
  • Do note that `IndexOf()` will compare entire lines. If you need to do a substring search instead, you will have to loop through the `Memo.Lines` manually, eg: `Index := -1; S := Edit.Text; for I := 0 to Memo.Lines.Count-1 do begin if Pos(S, Memo.Lines[I]) <> 0 then begin Index := I; Break; end; end; If Index <> -1 then ...` – Remy Lebeau May 14 '15 at 17:13
  • @Remy, there's still quite a few interpretations of this question and I hope my edit did not break it down (review is always welcomed ;-) And feel free to take my post (code, or anything you like) and include it into some canonical answer. – TLama May 15 '15 at 01:05
  • Thank you so much!!!! this helped me and did exactly what i wanted it to!! really big help! – Jean-Claude Pires May 15 '15 at 19:22