The ComboBox OnChange event occurs when the ItemIndex property changes in code

I am using FMX on Delphi 10.1 in Berlin.

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

stack overflow

Changing ItemIndex programmatically does not OnChange event. It only works in response to user interaction.

Is this true for VCL only?

I ask about this because, unfortunately, for me, from what I can check by changing the ItemIndex property in triggers .

If so, how can I achieve the same behavior as VCL in FireMonkey?

+5
source share
2 answers

Is this true for VCL only?

In FMX, much more is handled differently.

If so, how can I achieve the same behavior as VCL in FireMonkey?

A simple workaround is to not have the OnChange event OnChange before changing the ItemIndex event and then restoring it.

A simple procedure for this would be like this (as @Remy pointed out):

 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; 
+2
source

The correct way to solve this problem is to first find out where the OnChange handler is OnChange . This is done in the TCustomComboBox.DoChange() method.

So you need to do the following:

  • override the default DoChange() method so as not to fire the OnChange event OnChange .

  • override the ItemIndex property setter to use other logic that does not call the DoChange() method.

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

0
source

Source: https://habr.com/ru/post/1270404/


All Articles