Recent

Author Topic: Why Does OnChange not get Triggered on Combobox when itemindex changes ?  (Read 1104 times)

Weitentaaal

  • Full Member
  • ***
  • Posts: 160
Hello :)

When i do a Change on Itemindex in the Code it wont Trigger the OnChange Event.

Thanks :)
« Last Edit: March 08, 2021, 11:21:09 am by Weitentaaal »
Lazarus: 2.0.6 x86_64-win64-win32/win64
FPC-Version: 3.0.4
Compiler Version: 3.2.0

Zoran

  • Hero Member
  • *****
  • Posts: 1665
    • http://wiki.lazarus.freepascal.org/User:Zoran
This is documented: https://lazarus-ccr.sourceforge.io/docs/lcl/stdctrls/tcustomcombobox.onchange.html
Quote
This event covers only changes made by the user, not by code. Note that this differs from how other OnChange events work, for example TEdit.OnChange is triggered by code text changes. This event is also triggered when the selection is changed by using the combo box list.

and Delphi compatible: http://docwiki.embarcadero.com/Libraries/Sydney/en/Vcl.StdCtrls.TCustomCombo.OnChange
Quote
Note: OnChange only occurs in response to user actions. Changing the Text property programmatically does not trigger an OnChange event.


Weitentaaal

  • Full Member
  • ***
  • Posts: 160
Can u Somehow change this, so it gets triggered also when i change it in Code ?
If it doesn't can Some1 Pls Help me write an Event handler wich does this Function ?
« Last Edit: March 08, 2021, 01:06:37 pm by Weitentaaal »
Lazarus: 2.0.6 x86_64-win64-win32/win64
FPC-Version: 3.0.4
Compiler Version: 3.2.0

jamie

  • Hero Member
  • *****
  • Posts: 4376
Can u Somehow change this, so it gets triggered also when i change it in Code ?

No !

That goes against all logic. The onchange is for user actions not yours.

U need to knows the difference between u and the user entering changes.

Call the event directly after code changes.

If assigned(onchanged) then onchange(self)
The only true wisdom is knowing you know nothing

Weitentaaal

  • Full Member
  • ***
  • Posts: 160
is  assigned(onchanged)  True When a Property of the Object Changed  ?

And Thank u Guys :)
Lazarus: 2.0.6 x86_64-win64-win32/win64
FPC-Version: 3.0.4
Compiler Version: 3.2.0

lucamar

  • Hero Member
  • *****
  • Posts: 3790
is  assigned(onchanged)  True When a Property of the Object Changed  ?

Assigned(MyComboBox.OnChange) will be True when you have got an event handler for the box's OnChange event. When you do a change by code you can do something like:

Code: Pascal  [Select][+][-]
  1. MyComboBox.ItemIndex := 0;
  2. if Assigned(MyComboBox.OnChange) then
  3.   MyComboBox.OnChange(MyComboBox);

That ensures that there is a handler assigned to OnChange; otherwise you run the risk, if it isn't, that the call ends up in la-la-land, which would produce a segmentation fault. Of course, if you're absolutely sure that the handler is there (and assigned, e.g. because you set it at design-time) you can omit the test, though I don't recommend it: one never knows what might happen in the future and this ensures that no matter what your code will always work and not jump around picking berries ;)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018