How to delete the last character in the table.cell[0,0]?

General TRichView support forum. Please post your questions here
Post Reply
aayaay
Posts: 8
Joined: Sat Jun 11, 2016 3:05 am

How to delete the last character in the table.cell[0,0]?

Post by aayaay »

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

Post by Sergey Tkachenko »

As an editing operation (undoable) ?
aayaay
Posts: 8
Joined: Sat Jun 11, 2016 3:05 am

Post by aayaay »

No, code implementation, not user operation.
Is to deal with an existing text.
Or replace, for example, '? 'replace'!'. Note that only the last one character, not the full text.
aayaay
Posts: 8
Joined: Sat Jun 11, 2016 3:05 am

Post by aayaay »

I have solved..
Sergey Tkachenko
Site Admin
Posts: 17253
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

I am very sorry for the delay.

The task is not as simple as it may seem.
Possible cases:
1) The last item is a text item having more than 1 character.
Action: delete the last character from this item.
2) The last item is a non-text item or a text item containing 1 (or less) characters.
Action: deleting this item. If this item is after a list marker, deleting the marker as well. But if this is the single item in the cell, we replace it with empty text instead.

Code: Select all

procedure DeleteLastCharacter(RVData: TCustomRVData);
var
  ItemNo, ParaNo, TextStyleNo: Integer;
  S: String;
begin
  if RVData.ItemCount = 0 then
    exit;
  ItemNo := RVData.ItemCount - 1;

  if (RVData.GetItemStyle(ItemNo) < 0) or (Length(RVData.GetItemText(ItemNo)) < 2) then
  begin
    if (ItemNo > 0) and not ((ItemNo = 1) and (RVData.GetItemStyle(ItemNo - 1) = rvsListMarker)) then
    begin
      // deleting the last item (non-text or empty or single character)
      RVData.DeleteItems(ItemNo, 1);
      dec(ItemNo);
      // if it was after a list marker, deleting the marker as well
      if (ItemNo >= 0) and (RVData.GetItemStyle(ItemNo) = rvsListMarker) then
        RVData.DeleteItems(ItemNo, 1);
    end
    else if RVData.GetItemStyle(ItemNo) < 0 then
    begin
      // replacing the last non-text item with empty text
      ParaNo := RVData.GetItemPara(ItemNo);
      TextStyleNo := Min(RVData.GetActualTextStyle(RVData.GetItem(ItemNo)), 0);
      RVData.DeleteItems(ItemNo, 1);
      RVData.AddNL('', TextStyleNo, ParaNo);
    end
    else
      // deleting the last character of a single character item
      RVData.SetItemText(ItemNo, '');
  end
  else
  begin
    // deleting the last character from the last text item
    S := RVData.GetItemText(ItemNo);
    Delete(S, Length(S), 1);
    RVData.SetItemText(ItemNo, S);
  end;
end;
Call:

Code: Select all

DeleteLastCharacter(Table.Cells[0,0].GetRVData);
RichView1.Format; // or you can use BeginItemModify/EndItemModify instead of format
PS: I did not test this code properly, if it has bugs, let me know.
Post Reply