Page 1 of 1

Getting item index of selected control

Posted: Thu Jun 05, 2025 4:15 pm
by pyoung@wddc.com
Hi There.

In a richviewedit, if there is a Delphi control such as a TEdit and the user directly selects the TEdit using the mouse in order to perform some input, the CurItemNo does not get updated. It does if the user selects other item types but it appears that TControls intercede . Is there a way to get the index of the item the user has selected and is working in if it is a TControl?

Re: Getting item index of selected control

Posted: Fri Jun 06, 2025 11:14 am
by Sergey Tkachenko
The simplest solution is selecting the control on clicking on it: process OnMouseDown of controls, and call RichViewEdit.SelectControl() in it.
See the example in viewtopic.php?t=157 (how to resize and drag&drop controls in the editor).

Re: Getting item index of selected control

Posted: Fri Jun 06, 2025 11:23 am
by Sergey Tkachenko
If you want to find the location of the control, you can use this function:

Code: Select all

procedure FindControlLocation(var RVData: TCustomRVData; out ItemNo: Integer);
var 
  table: TRVTableItemInfo;
  r,c: Integer;
begin
  ItemNo := RVData.FindControlItemNo(TControl(Sender));
  if ItemNo<0 then 
  begin
    RVData := nil;
    exit;
  end;
  if RVData.GetItem(ItemNo) is TRVTableItemInfo then
  begin
      table := TRVTableItemInfo(RVData.GetItem(ItemNo));
      table.GetCellWhichOwnsControl(TControl(Sender), r, c, ItemNo);
      RVData := table.Cells[r,c].GetRVData;
      FindControlLocation(RVData, ItemNo);
  end;
end;
On input, RVData must be TRichView.RVData.
On output, RVData is a document containing the control (one of: TRichView.RVData, cell, or RVData of cell inplace editor), or nil if the control is not found.

Here is how you can use this function to select this control (i.e., doing the same work as SelectControl):

Code: Select all

var 
  RVData: TCustomRVData;
    ItemNo : Integer;
begin
  RVData := RichViewEdit1.RVData;
  FindControlLocation(RVData, ItemNo);
  if RVData = nil then 
    exitl
  RVData := RVData.Edit;
  TCustomRVFormattedData(RVData).SetSelectionBounds(ItemNo, 0, ItemNo, 1); // or (ItemNo, 1, ItemNo, 1) to move the caret after the control
end;