delphi - Firemonkey TListBox.OnClick - 单击了哪个项目?

标签 delphi firemonkey tlistbox

德尔福 10.2.2 移动版

从一个空白的移动项目开始,我在表单上放置一个 TListBox。我添加了两个 TListBoxItem。

procedure TForm1.ListBox1Click(Sender: TObject);
begin
  ShowMessage('ListBoxItem.itemindex = ' + ListBox1.ItemIndex.ToString);
end;

当我单击 Windows 和 Macintosh 中的第一个项目时,OnClick() 正确报告项目索引 0 已被单击。

当我单击移动设备(iOS 和 Android)中的第一个项目时,OnClick() 会将项目索引报告为 -1(而不是应有的 0)。然后它继续突出显示第一项。

如果我单击移动设备中的第二个项目,OnClick() 会将项目索引报告为 0(而不是应有的 1)。然后它继续突出显示第二项。

在移动设备上单击 TListBox 时,如何在 OnClick() 中获取正确的项目?

最佳答案

显然,OnClick 事件是在 ItemIndex 更新之前触发的。因此,您必须延迟处理,直到 ItemIndex 有机会首先更新。您可以:

  • 使用TThread.ForceQueue()(仅限 10.2 Tokyo+):

    procedure TForm1.ListBox1Click(Sender: TObject);
    begin
      TThread.ForceQueue(nil,
        procedure
        begin
          ShowMessage('ListBoxItem.itemindex = ' + ListBox1.ItemIndex.ToString);
        end
      );
    end;
    
  • 使用TThread.Queue():

    procedure TForm1.ListBox1Click(Sender: TObject);
    begin
      TThread.CreateAnonymousThread(
        procedure
        begin
          TThread.Queue(nil,
            procedure
            begin
              ShowMessage('ListBoxItem.itemindex = ' + ListBox1.ItemIndex.ToString);
            end
          );
        end
      ).Start;
    end;
    
  • 使用短计时器:

    procedure TForm1.ListBox1Click(Sender: TObject);
    begin
      Timer1.Enabled := True;
    end;
    
    procedure TForm1.Timer1Timer(Sender: TObject);
    begin
      Timer1.Enabled := False;
      ShowMessage('ListBoxItem.itemindex = ' + ListBox1.ItemIndex.ToString);
    end;
    

关于delphi - Firemonkey TListBox.OnClick - 单击了哪个项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48813666/

相关文章:

c++ - 使用指针将代码翻译成 Pascal 中的程序集 - Delphi

delphi - 如何创建一个类似对话框的组件,允许在其中放置其他控件?

delphi - 使用实时绑定(bind)将多个字段值分配给 FMX MetropolisUI TListBox Item.Text

delphi - 在 FireMonkey 中动画添加字符串到列表框

Delphi:首次单击后获取VirtualStringTree的编辑模式

delphi - 判断字符串是否不包含数字

Delphi 字典和排序数据

delphi - 如何从 FireMonkey TGrid 中的特定列/行获取单元格值

android - FMXBroadcastReceiver (Android) 在 Delphi 10 Seattle 中损坏了吗?自 XE7 以来发生了什么变化?

delphi - 我可以强制 Delphi 6 TImageList 位图将其透明像素绘制为某种颜色吗?