trichview.com

trichview.support




Re: how to make tables add a new row when pressing tab


Return to index


Author

Message

Sergey Tkachenko

Posted: 12/14/2004 15:29:34


May be in future I'll add this option.

For now, you can use the code below.

Unfortunately, the current version does not allow to process Tab when it

used to move between cells.

So this example uses Shift+Ctrl+Enter.


procedure TForm3.RichViewEdit1KeyDown(Sender: TObject; var Key: Word;

  Shift: TShiftState);

var rve,rvetop: TCustomRichViewEdit;

    table: TRVTableItemInfo;

    r,c: Integer;

begin

   if (Key=VK_RETURN) and (Shift=[ssCtrl, ssShift]) then

   begin

     if RichViewEdit1.SelectionExists then

       exit;

     if not RichViewEdit1.GetCurrentItemEx(TRVTableItemInfo, rve,

       TCustomRVItemInfo(table)) then

       exit;

     rvetop := table.GetEditedCell(r,c);

     // no edited cell?

     if rvetop=nil then

       exit;

     // last cell?

     if (r+table.Cells[r,c].RowSpan<table.Rows.Count) or

        (c+table.Cells[r,c].ColSpan<table.Rows[r].Count) then

       exit;

     // caret at the end of cell?

     if (rvetop.CurItemNo<rvetop.ItemCount-1) or

        (rvetop.OffsetInCurItem<rvetop.GetOffsAfterItem(rvetop.CurItemNo))

     then

       exit;

     // we cannot add new row right here, because it will destroy

     // Sender editor. So using PostMessage

     PostMessage(Handle, WM_RVADDROW, 0, 0);

     Key := 0;

  end;

end;


// Constant > WM_USER

const WM_RVADDROW = WM_USER+5;


// In form declaration

    procedure WMRVAddRow(var Msg: TMessage); message WM_RVADDROW;


procedure TForm3.WMRVAddRow(var Msg: TMessage);

var rve: TCustomRichViewEdit;

    table: TRVTableItemInfo;

    ItemNo, Data: Integer;

    r,c: Integer;

begin

  if not RichViewEdit1.GetCurrentItemEx(TRVTableItemInfo, rve,

    TCustomRVItemInfo(table)) then

    exit;

  ItemNo := table.GetMyItemNo;

  rve.BeginItemModify(ItemNo, Data);

  table.InsertRows(table.Rows.Count, 1, table.Rows.Count-1, False);

  rve.EndItemModify(ItemNo, Data);

  rve.Change;

  table.Rows.GetMainCell(table.Rows.Count-1, 0, r, c);

  table.EditCell(r,c);

end;


Note 1: this code does not allow adding new paragraph at the end of the last

cell - it adds new row instead. May be it's better to use Ctrl+Enter?

Note 2: yes, if Tab is used to move to another cell, it is not passed to

OnKeyDown. May be it will be changed in future versions.




> If I am in the bottom right cell of a table, I would like tab to add a

> new row, just like MS Word. I tried looking for an option or something

> but I could not seem to find anything.

>

> Is there an easy way to do this?

>

> Thanks,

> Dave





Powered by ABC Amber Outlook Express Converter