Page 1 of 1

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

Posted: Fri Jan 06, 2017 8:25 am
by aayaay
:(

Posted: Fri Jan 06, 2017 1:31 pm
by Sergey Tkachenko
As an editing operation (undoable) ?

Posted: Fri Jan 06, 2017 9:36 pm
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.

Posted: Tue Jan 10, 2017 8:14 am
by aayaay
I have solved..

Posted: Tue Jan 10, 2017 9:11 am
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.