我在 Delphi 10.1 Berlin 上使用 FMX。
我读到了这个(这是我想要的行为):
https://stackoverflow.com/a/42933567/1343976
Changing
ItemIndex
programmatically does not result in theOnChange
event being fired. It fires only in response to user interaction.
这仅适用于 VCL 吗?
我之所以提出这个要求,是因为不幸的是,根据我的测试,修改代码中的 ItemIndex
属性会触发 OnChange
事件。
如果这是真的,我怎样才能在 FireMonkey 中实现与 VCL 相同的行为?
最佳答案
Is this true only for VCL?
FMX 中许多事情都以不同的方式处理。
If this is true, how can I achieve the same behaviour as VCL in FireMonkey?
一个简单的解决方法是在更改 ItemIndex
之前清空 OnChange
事件属性,然后恢复事件。
执行此操作的简单例程如下(如@Remy 所述):
procedure SetItemIndex(ix : Integer; cb: TComboBox);
var
original : TNotifyEvent;
begin
original := cb.OnChange;
cb.OnChange := nil;
try
cb.ItemIndex := ix;
finally
cb.OnChange := original;
end;
end;
关于delphi - 更改代码中的 ItemIndex 属性时会发生 ComboBox OnChange 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45423581/