[SOLVED]Questions about ComboBox events lazarus / 31-LCL ---------------------------------------------------------------------- tfurnivall · 2025-06-02 22:08 The attached project is a small tool I'm writing to convert text parameters of a File Open into numeric equivalents. At the moment, I'm trying to get the selected item in the Combo Box list, and then convert that first to a numerical value, and second to the equivalent parameters on a command line command. (No resemblance whatever to any of the platforms where Lazarus runs, but in case anyone wonders, it's the old HP3000 OS - MPE/iX). My question is the way the app behaves when I click on the drop-down icon of the Domain Value combobox (the first one on the form). An exception is raised even before I can click on one of the items. There are a few more combo boxes - some of which have lists, and some of which are empty. Clicking on the drop-down arrow for those combo boxes causes no problems. The exception comes only on the combo box to which I have added an event handler. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TfrmFOpenTool.cboFDomainClick(Sender: TObject);var    ix           : integer;    ic           : integer;    DomainOption : string; begin  ic := cboFDomain.items.Count;    ix := cboFDomain.itemindex;    DomainOption := cboFDomain.items [ix]; end; The immediate questions are Why? and How to fix it? Any other pointers to good examples of Combo Box (or other control) event handlers is welcome, too! Meantime, I'll se there are any other events that might be worth exploring... Thanks, Tony ---------------------------------------------------------------------- RayoGlauco · 2025-06-02 22:28 The error occurs when you execute this line: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i DomainOption := cboFDomain.items [ix];Because ix=-1 (bad index); You need to avoid this to happen: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i if ix>=0 then   DomainOption := cboFDomain.items [ix]; ---------------------------------------------------------------------- tfurnivall · 2025-06-02 23:24 Yes. But. I haven't clicked on the list yet! If there is a separate event for DropDown (which there is) and I had the code for Click in that procedure, I'd agree. But I simply click on the down arrow and wait - shonuf the exception is raised. That is what I want to try to understand! Tony ---------------------------------------------------------------------- tfurnivall · 2025-06-03 00:17 Follow up, I added ShowMessage () notifications for each event (OnCLick, and OnDropDown). When both notifications were enabled, the DropDown message was raised. No sign of the Click message, but I was stubborn and waited a few seconds before clearing the box;-) When DropDown was commented out, and with no clicking except on the DropDown button, the Click event was raised. Personally I think that's wrong, but my guess is that it is now 'legacy' and has to be maintained in that way. I would have expected Click to be the result of clicking on the list, not just anywhere in the control. Oh well, che sera, sera ---------------------------------------------------------------------- VisualLab · 2025-06-03 11:25 I made a small test program to check the order of occurrence of several events. Details in the attachment. The test shows that OnDropDown appears first (of the 4 listed) and OnClick right after it. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls; type  {TForm1}  TForm1 = class(TForm)    ComboBox1: TComboBox;    Memo1: TMemo;    procedure ComboBox1Change(Sender: TObject);    procedure ComboBox1Click(Sender: TObject);    procedure ComboBox1DropDown(Sender: TObject);    procedure ComboBox1Select(Sender: TObject);  end; var  Form1: TForm1; implementation {$R *.lfm} {TForm1} procedure TForm1.ComboBox1DropDown(Sender: TObject);begin  {}  Memo1.Lines.Clear;  Memo1.Lines.Add('Internal list of ComboBox was shown!');end; procedure TForm1.ComboBox1Click(Sender: TObject);begin  {}  Memo1.Lines.Add('Internal button of ComboBox was clicked!');end; procedure TForm1.ComboBox1Change(Sender: TObject);begin  {}  Memo1.Lines.Add('Visible content in ComboBox was changed!');end; procedure TForm1.ComboBox1Select(Sender: TObject);begin  {}  Memo1.Lines.Add('Item from internal list of ComboBox was selected!');end; end. ---------------------------------------------------------------------- tfurnivall · 2025-06-03 12:58 Eureka! I assumed (yes - point taken) that the Click event was the event I was looking for. Then I noticed OnSelect. That is what I was looking for. There is an apposite quote from the documentation: Lazarus offers various events that you can use to enter your own procedures to handle things that happen in your application (e.g. a user clicks a button). There are rather a lot of possible events to cater for a lot of different scenarios. Somebody who does not know Lazarus or Delphi could well pick the wrong event. The VBA environment away from which I am migrating doesn't have a Select event, sos it never occurred to me to look for it. Thanks, VisualLab, and all the other helpers on this board! Tony ---------------------------------------------------------------------- dsiders · 2025-06-03 15:00 Quote from: tfurnivall on June 03, 2025, 02:58:27 pmEureka! I assumed (yes - point taken) that the Click event was the event I was looking for. Then I noticed OnSelect. That is what I was looking for. There is an apposite quote from the documentation: Lazarus offers various events that you can use to enter your own procedures to handle things that happen in your application (e.g. a user clicks a button). There are rather a lot of possible events to cater for a lot of different scenarios. Somebody who does not know Lazarus or Delphi could well pick the wrong event. The VBA environment away from which I am migrating doesn't have a Select event, sos it never occurred to me to look for it. Thanks, VisualLab, and all the other helpers on this board! Tony https://lazarus-ccr.sourceforge.io/docs/lcl/stdctrls/tcombobox.html ---------------------------------------------------------------------- tfurnivall · 2025-06-03 16:03 Thanks for the pointer, dsiders. That's a mighty long list and a 'quick' (ie very inefficient) read gave me OnClick before OnSelect. Then my VBA history kicked in and I looked no further. My bad - and a good learning moment! Tony ---------------------------------------------------------------------- jamie · 2025-06-03 22:24 You can still use the OnClick event,  you need to test for ItemIdex for the value of -1 a -1 indicates that nothing is selected so never use that itemindex to look at the list if its -1 Jamie ---------------------------------------------------------------------- RayoGlauco · 2025-06-05 10:12 I agree with Jamie. My opinion is that it's always better to prevent unexpected errors. That's why it's a good idea to check the combo index before using it, even if you think it will never be -1. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i if ix>=0 then  DomainOption := cboFDomain.items [ix];