Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ComboBox OnChange event occurs when changing the ItemIndex property in code

I'm using FMX on Delphi 10.1 Berlin.

I read this (which is the behavior I want):

https://stackoverflow.com/a/42933567/1343976

Changing ItemIndex programmatically does not result in the OnChange event being fired. It fires only in response to user interaction.

Is this true only for VCL?

I'm asking for this because, unfortunately for me, from what I can test, modifying the ItemIndex property in code triggers the OnChange event.

If this is true, how can I achieve the same behaviour as VCL in FireMonkey?

like image 424
orsomannaro Avatar asked Oct 29 '22 04:10

orsomannaro


1 Answers

Is this true only for VCL?

Many things are handled in a different way in FMX.

If this is true, how can I achieve the same behaviour as VCL in FireMonkey?

A simple workaround is to nil the OnChange event property before changing the ItemIndex, and afterwards restore event.

A simple routine to do this would be someting like this (as outlined by @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;  
like image 140
LU RD Avatar answered Nov 13 '22 06:11

LU RD