orsomannaro
orsomannaro

Reputation: 51

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?

Upvotes: 5

Views: 4697

Answers (2)

LU RD
LU RD

Reputation: 34919

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;  

Upvotes: 2

SilverWarior
SilverWarior

Reputation: 8386

The proper way of dealing with this problem is to first find out where the OnChange handler is being called from. This is being done in the TCustomComboBox.DoChange() method.

So, what you need to do is either:

  1. override the default DoChange() method to not fire the OnChange event method.

  2. override the ItemIndex property setter to use different logic which won't call the DoChange() method.

Both of these approaches require you to create a new class for your modified ComboBox.

Upvotes: 0

Related Questions