Deleting a range

General TRichView support forum. Please post your questions here
Post Reply
Alvaro
Posts: 17
Joined: Wed Apr 14, 2010 4:01 am

Deleting a range

Post by Alvaro »

Hi, in search query I put "delete lines" and I choosed Search for all terms, but nothing helped me.
My question is how can I delete more then one line following this steps:
Suppose a text:

reserved word(i)
text
text
text
reserved word(f)

I would like to select a range starting at reserved word(i) until reserved word(f) and delete all lines in this range.

I tryed this, but returned an error, could you help me, please?

Code: Select all

rve.SearchText('Inicia bloco', [rvseoDown, rvseoWholeWord]).ToInteger;
nPosI := RVGetLinearCaretPos(rve);
rve.SearchText('Termina bloco', [rvseoDown, rvseoWholeWord]).ToInteger;
nPosF := RVGetLinearCaretPos(rve);

rve.DeleteParas(nPosI, nPosF);

or

rve.SetSelectionBounds(nPosI, rve.GetOffsBeforeItem(nPosI), nPosF, rve.GetOffsAfterItem(nPosF));
rve.DeleteSelection;

Sergey Tkachenko
Site Admin
Posts: 17559
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

You must not use RVGetLinearCaretPos here. This function returns a number of characters from the beginning of document to the caret position. But DeleteParas and SetSelectedBounds does not need this value, they need index of item.

The first code could be:

Code: Select all

rve.SearchText('Inicia bloco', [rvseoDown, rvseoWholeWord]);
nPosI := rve.CurItemNo;
rve.SearchText('Termina bloco', [rvseoDown, rvseoWholeWord]);
nPosF := rve.CurItemNo;
rve.DeleteParas(nPosI, nPosF); 
It deletes all paragraphs, starting from paragraphs containing the first word and ending to paragraph containing the last word. Document is reformatted, but this change is not written in undo buffer, so it may become invalid, and you need to call rve.ClearUndo.

Do you need to delete reserved words as well? In this case, the second code could be:

Code: Select all

var ItemNoI, ItemNoF, OffsI, OffsF: Integer;

rve.SearchText('Inicia bloco', [rvseoDown, rvseoWholeWord]); 
rve.GetSelectionBounds(ItemNoI, OffsI, dummy, dummy, True); // storing the position  before selection in ItemNoI, OffsI
rve.SearchText('Termina bloco', [rvseoDown, rvseoWholeWord]); 
rve.GetSelectionBounds(dummy, dummy, ItemNoF, OffsF, True);// storing the position  after selection in ItemNoF, OffsF

rve.SetSelectionBounds(ItemNoI, OffsI, ItemNoF, OffsF);
rve.DeleteSelection;
Note 1: In a real code, you need to check the result of SearchText to see if the word was really found.
Note 2: These code fragments assume that both words are in the root editor, not in table cells. The second code can be modified to support table cells:

Code: Select all

var ItemNoI, ItemNoF, OffsI, OffsF: Integer;
  tle1, tle2: TCustomRichViewEdit;

rve.SearchText('Inicia bloco', [rvseoDown, rvseoWholeWord]); 
tle1 := rve.TopLevelEditor;
tle1.GetSelectionBounds(ItemNoI, OffsI, dummy, dummy, True); // storing the position  before selection in ItemNoI, OffsI
rve.SearchText('Termina bloco', [rvseoDown, rvseoWholeWord]); 
tle2 := rve.TopLevelEditor;
tle2.GetSelectionBounds(dummy, dummy, ItemNoF, OffsF, True);// storing the position  after selection in ItemNoF, OffsF
if tle1=tle2 then begin // otherwise, the selection is in different cells
  tle2.SetSelectionBounds(ItemNoI, OffsI, ItemNoF, OffsF);
  tle2.DeleteSelection;
end;
Alvaro
Posts: 17
Joined: Wed Apr 14, 2010 4:01 am

Post by Alvaro »

Thank you very much Sergey was exactly what I needed, here in Brazil is still night, but have a excelente day my friend.
Post Reply