delphi uses RichView control to select from the table

Select from the TRichView table

introduce

Select type

Table type selected in RichView:

  • Not selected
  • Full selection (as part of multi project selection)
  • Partial selection (multi cell selection)
  • Some part of the contents of a cell (in a cell or in a Inplace Editor )Selected.

RichView does not support selection that starts with other items and completes in the middle of the table. A selection that contains more than one item can only contain a table completely (or no table).

Any method that handles the selection must be called only when the document is formatted.

Table in multi item selection

To determine whether the table is fully selected, as part of a multi item selection, use RichView.GetSelectionBounds (and compare its results with the table's GetItemNo).

Multi cell selection

You can partially select a table: the selection can start in one cell of the table and end in another cell.

You can use the TRVTableItemInfo.GetSelectionBounds or TRVTableItemInfo.GetNormalizedSelectionBounds methods (the latter is more convenient) to determine whether a table is partially selected.

You can use TRVTableItemInfo.Select to select some cells in the table and TRVTableItemInfo.Deselect to delete the selection.

Some rows or columns can be completely selected: TRVTableItemInfo.SelectRows and TRVTableItemInfo.SelectCols.

You can use the TRVTableItemInfo.IsCellSelected method to determine whether a given cell is selected.

Selection within cells

Available Inplace Editor And the location of the currently edited cell: TRVTableItemInfo.GetEditedCell.

Before you can make a selection in a cell, you need to activate it Inplace Editor (TRichView doesn't) Inplace Editor But the same code must be invoked in TRichView, not just in TRichViewEdit.

Examples selected in cell table.Cells[0,0]

table.EditCell(0,0);
table.Cells[0,0].GetRVData.SelectAll;

method

Select the cell TRVTableItemInfo.Select

procedure Select(StartRow, StartCol, RowOffs, ColOffs: Integer);

This method selects the cell range (rectangle, if there is no cell merging) in the table, starting with cell Cells[StartRow, StartCol].

parameter

StartRow and StartCol start to select the cell index.

RowOffs, ColOffs row offsets, and column offsets.

Cells[StartRow, StartCol] is the cell to start selecting. Select to always include this cell.

Cells[StartRow+RowOffs, StartCol+ColOffs] is the cell to end the selection. Select to always include this cell.

If RowOffs > 0, select include row RowOffs below. If RowOffs < 0, select to include the abs(RowOffs) line above. So is ColOffs.

This method cannot be used to deselect because Select(StartRow, StartCol, 0,0) selects a cell and deselect TRVTableItemInfo.Deselect.

This method must be called when formatting a document.

After selecting cells, you cannot use the method of inserting symbol position (such as InsertText, InsertPicture, etc.) in the RichViewEdit editor. You need to set the inserting symbol position TCustomRichView.SetSelectionBounds

Select the row TRVTableItemInfo.SelectRows

Select the Count row starting from StartRow (down)

procedure SelectRows(StartRow, Count: Integer);

parameter

StartRow is the index of the first row to be selected. The range is 0..RowCount-1.

Count the number of rows to select.

This method must be called when formatting a document.

Select the column TRVTableItemInfo.SelectCols

Select the Count column starting from StartCol (right)

procedure SelectCols(StartCol, Count: Integer);

parameter

StartCol the index of the first column to be selected, in the range of 0..ColCount-1.

Count the number of columns to select.

This method must be called when formatting a document.

Deselect TRVTableItemInfo.Deselect

Deselect the cells in the table.

procedure Deselect;

All selected cells become unselected.

This method must be called when formatting a document.

TRVTableItemInfo.GetSelectionBounds

Returns the selection in the table.

function GetSelectionBounds(out StartRow, StartCol, RowOffs, ColOffs: Integer): Boolean;

parameter

StartRow and StartCol start to select the cell index.

RowOffs, ColOffs row offsets, and column offsets.

Cells[StartRow, StartCol] is the cell to start selecting. Select to always include this cell.

Cells[StartRow+RowOffs, StartCol+ColOffs] is the cell to end the selection. Select to always include this cell.

If RowOffs > 0, select include row RowOffs below. If RowOffs < 0, select to include the abs(RowOffs) line above. So is ColOffs.

Return value: True selection exists, False selection does not exist.

This method must be called when formatting a document.

Because of the merge, the selection may not have a rectangular shape. Some operations apply only to rectangular selections (for example, cell merging). You can use CanMergeSelectedCells to check.

This method returns the selected internal storage information. For practical use, GetNormalizedSelectionBounds is more convenient.

For example, the return value (3,3,0,0) indicates that only one cell is selected: Cells[3,3].

Due to cell merging, the selected cells may be nil.

Whether TRVTableItemInfo.IsCellSelected is selected in the cell

Returns whether the specified cell is selected.

function IsCellSelected(Row, Col: Integer): Boolean;

parameter

Row, Col specify the location of the cell.

The return value True cell is selected, and the False cell is not selected.

This method must be called when formatting a document.

Example of traversing all selected cells

for r := 0 to table.RowCount-1 do
for c := 0 to table.ColCount-1 do
 if (table.Cells[r,c]<>nil) and
    table.IsCellSelected(r,c) then ...

Get the standardized selection range TRVTableItemInfo.GetNormalizedSelectionBounds

function GetNormalizedSelectionBounds(IncludeEditedCell: Boolean;
  out TopRow, LeftCol, ColSpan, RowSpan: Integer): Boolean;

Returns the selection range in the table in a convenient form.

parameter

IncludeEditedCell if True, the function returns the location of the currently edited cell (if there is no selection and some cells are currently being edited).

TopRow, TopCol output the upper left corner of the selection (because of merging, the selection is not always rectangular)

ColSpan and RowSpan output select how many columns and rows to include. Both values > = 1.

Return value: True selection exists, False selection does not exist.

Some operations apply only to rectangular selections (for example, cell merging). You can use CanMergeSelectedCells to check.

This method must be called when formatting a document.

Error example of traversing all selected cells

for r := TopRow to TopRow + RowSpan - 1 do
for c := LeftCol to TopCol + ColSpan - 1 do
 if table.Cells[r,c]<>nil then ...

This loop may miss some selected cells because there may be some cells to the left of LeftCol and the top of TopRow, which are also selected due to merge spans.

Returns the currently edited cell TRVTableItemInfo.GetEditedCell

Returns the of the currently edited cell Inplace Editor , if there is no such cell, nil is returned.

function GetEditedCell(out Row,Col: Integer): TCustomRichViewEdit;

parameter

Row, Col receive the location of the cell being edited.

This method must be called when formatting a document.

example

Select cell

uses RVTable, RVItem;

procedure TForm1.Button1Click(Sender: TObject);
var
  Table: TRVTableItemInfo;
  Row, Col: Integer;
begin
  //Create a table with 5 rows and 4 columns
  Table := TRVTableItemInfo.CreateEx(5, 4, RichViewEdit1.RVData);

  //Set table border and background color
  Table.Color := clNone;
  Table.BorderStyle := rvtbColor;
  Table.CellBorderStyle := rvtbColor;
  Table.BorderWidth := 1;
  Table.CellBorderWidth := 1;

  //Add table data
  for Row := 0 to Table.RowCount - 1 do
    for Col := 0 to Table.ColCount - 1 do
    begin
      Table.Cells[Row, Col].BestWidth := 60;
      Table.Cells[Row, Col].BestHeight := 20;
      Table.Cells[Row, Col].Color := clCream;
      Table.Cells[Row, Col].Clear;
      Table.Cells[Row, Col].AddFmt('%d,%d', [Row, Col], 0, 0);
    end;

  //Merge cells, merge [2,2] and [2,3]
  Table.MergeCells(2, 2, 1, 2, True);

  //Add table to document
  RichViewEdit1.InsertItem('', Table);

  //Select the rectangle formed by [2,1] to [1,2]
  //Because [2,2] is merged with [2,3], the selection is not rectangular
  Table.Select(2, 1, -1, 1);
end;

Sets the color of the selected cell

uses RVTable, RVItem;

procedure TForm1.Button2Click(Sender: TObject);
var
  Item: TCustomRVItemInfo;
  Table: TRVTableItemInfo;
  Data: Integer;
  Rve: TCustomRichViewEdit;
  ItemNo: Integer;
  Row, Col: Integer;
begin
  //Gets the currently selected table
  if not RichViewEdit1.CanChange or
    not RichViewEdit1.GetCurrentItemEx(TRVTableItemInfo, Rve, Item) then
    Exit;
  Table := TRVTableItemInfo(Item);
  ItemNo := Rve.GetItemNo(Table);

  Rve.BeginItemModify(ItemNo, Data);

  //Traverse selected cells
  for Row := 0 to Table.RowCount - 1 do
    for Col := 0 to Table.ColCount - 1 do
      if (Table.Cells[Row, Col] <> nil) and
        Table.IsCellSelected(Row, Col) then
      begin
        Table.SetCellColor(clRed, Row, Col);
      end;

  Rve.EndItemModify(ItemNo, Data);
  Rve.Change;
end;

Get selection information

uses RVTable, RVItem;

procedure TForm1.Button3Click(Sender: TObject);
var
  Item: TCustomRVItemInfo;
  Table: TRVTableItemInfo;
  Rve: TCustomRichViewEdit;
  ItemNo: Integer;
  StartRow1, StartCol1, RowOffs1, ColOffs1: Integer;
  StartRow2, StartCol2, RowOffs2, ColOffs2: Integer;
begin
  //Gets the currently selected table
  if not RichViewEdit1.CanChange or
    not RichViewEdit1.GetCurrentItemEx(TRVTableItemInfo, Rve, Item) then
    Exit;
  Table := TRVTableItemInfo(Item);
  ItemNo := Rve.GetItemNo(Table);

  //Get selection information
  Table.GetSelectionBounds(StartRow1, StartCol1, RowOffs1, ColOffs1);
  //Get selection range
  Table.GetNormalizedSelectionBounds(False, StartRow2, StartCol2, RowOffs2, ColOffs2);

  //Deselect table
  Table.Deselect;
  //After moving the insertion point to the table
  RichViewEdit1.SetSelectionBounds(ItemNo, 1, ItemNo, 1);

  //Output selection information and selection range
  RichViewEdit1.InsertText('Select information StartRow=' + IntToStr(StartRow1) +
    ' StartCol=' + IntToStr(StartCol1) + ' RowOffs=' + IntToStr(RowOffs1) + ' ColOffs=' + IntToStr(ColOffs1));
  RichViewEdit1.InsertText(#13#10);
  RichViewEdit1.InsertText('Selection range StartRow=' + IntToStr(StartRow2) +
    ' StartCol=' + IntToStr(StartCol2) + ' RowOffs=' + IntToStr(RowOffs2) + ' ColOffs=' + IntToStr(ColOffs2));
end;

Posted by guanche on Sat, 06 Nov 2021 04:06:42 -0700