Getting item index of selected control

General TRichView support forum. Please post your questions here
Post Reply
pyoung@wddc.com
Posts: 5
Joined: Wed Mar 31, 2021 8:56 pm

Getting item index of selected control

Post 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?
Sergey Tkachenko
Site Admin
Posts: 17842
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: Getting item index of selected control

Post 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).
Sergey Tkachenko
Site Admin
Posts: 17842
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: Getting item index of selected control

Post 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;
Post Reply