0

I'm using Delphi 10 Seattle to build a multi device project with firemonkey.

My project has a ListBox, and I fill it runtime with ListBoxItems. I want to add the LongTap gesture to the ListBoxItems.

I have already tried this:

gestureManager := TGestureManager.Create(nil);
listBoxItem.Touch.GestureManager := gestureManager;
listBoxItem.Touch.InteractiveGestures := [TInteractiveGesture.LongTap];
listBoxItem.OnGesture := ListBoxItemGesture;

But the onGesture method doesn't get called. If I add the gestureManager to the Form in the designer and call the same onGesture method it does get called.

Remi
  • 1,289
  • 1
  • 18
  • 52
  • try create gestureManager with parent, `gestureManager := TGestureManager.Create(myForm);` – kami Mar 11 '16 at 05:56
  • @kami you mean owner, but I've already tried with a gestureManager that I put on the form instead of creating the gestureManager runtime. I'll try it by setting the owner the listBoxItem. – Remi Mar 11 '16 at 07:58
  • 1
    Gestures dont work with controls inside ScrollBox and descendants (I don't know, why). You should use `ListBox.Touch`, `ListBox.OnGesture` and analyze `Selected` property. – kami Mar 11 '16 at 09:03
  • @kami thanks! You are correct. If you post it as an answer i'll except it! – Remi Mar 11 '16 at 10:18

2 Answers2

3

Gestures don't work with controls inside ScrollBox and descendants (I don't know, why). You should use ListBox.Touch, ListBox.OnGesture and analyze Selected property:

  ListBox1.Touch.GestureManager := FmyGestureManager;
  ListBox1.Touch.InteractiveGestures := [TInteractiveGesture.LongTap];
  ListBox1.OnGesture := ListBox1Gesture;



procedure THeaderFooterForm.ListBox1Gesture(Sender: TObject; const EventInfo: TGestureEventInfo; var Handled: Boolean);
begin
  if (Sender = ListBox1) and Assigned(ListBox1.Selected) then
    begin
      lblMenuToolBar.Text := 'Handled' + ListBox1.Selected.Text;
      Handled := True;
    end;
end;

Or, more complex method - find item by gesture location:

procedure THeaderFooterForm.ListBox1Gesture(Sender: TObject; const EventInfo: TGestureEventInfo; var Handled: Boolean);
var
  c: IControl;
  ListBox: TListBox;
  lbxPoint: TPointF;
  ListBoxItem: TListBoxItem;
begin
  c := ObjectAtPoint(EventInfo.Location);
  if Assigned(c) then
    if Assigned(c.GetObject) then
      if c.GetObject is TListBox then
        begin
          ListBox := TListBox(c.GetObject);
          lbxPoint := ListBox.AbsoluteToLocal(EventInfo.Location);

          ListBoxItem := ListBox.ItemByPoint(lbxPoint.X, lbxPoint.Y);
          if Assigned(ListBoxItem) then
            lblMenuToolBar.Text := 'Handled ' + ListBoxItem.Text;
          Handled := True;
        end;
end;
kami
  • 1,438
  • 1
  • 16
  • 23
0

The right solution is much more banal:

for i:=0 to pred(ListBox1.items.count)do
begin
  ListBox1.ItemByIndex(i).Touch.GestureManager:=GestureManager1;
  ListBox1.ItemByIndex(i).Touch.InteractiveGestures :=
  [TInteractiveGesture.LongTap, TInteractiveGesture.DoubleTap];
  ListBox1.ItemByIndex(i).OnGesture := ListBoxitemGesture;
  ListBox1.ItemByIndex(i).HitTest:=true;
end;
cuebit
  • 1
  • 1