Recent

Author Topic: [SOLVED]Questions about ComboBox events  (Read 951 times)

tfurnivall

  • Jr. Member
  • **
  • Posts: 87
[SOLVED]Questions about ComboBox events
« on: June 03, 2025, 12:08:50 am »
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][+][-]
  1. procedure TfrmFOpenTool.cboFDomainClick(Sender: TObject);
  2. var
  3.     ix           : integer;
  4.     ic           : integer;
  5.     DomainOption : string;
  6.  
  7. begin
  8.     ic := cboFDomain.items.Count;
  9.     ix := cboFDomain.itemindex;
  10.     DomainOption := cboFDomain.items [ix];
  11.  
  12. end;
  13.  
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
« Last Edit: June 03, 2025, 02:58:51 pm by tfurnivall »

RayoGlauco

  • Full Member
  • ***
  • Posts: 221
  • Beers: 1567
Re: Questions about ComboBox events
« Reply #1 on: June 03, 2025, 12:28:25 am »
The error occurs when you execute this line:
Code: Pascal  [Select][+][-]
  1. DomainOption := cboFDomain.items [ix];
Because ix=-1 (bad index);
You need to avoid this to happen:
Code: Pascal  [Select][+][-]
  1. if ix>=0 then
  2.    DomainOption := cboFDomain.items [ix];
To err is human, but to really mess things up, you need a computer.

tfurnivall

  • Jr. Member
  • **
  • Posts: 87
Re: Questions about ComboBox events
« Reply #2 on: June 03, 2025, 01:24:31 am »
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

  • Jr. Member
  • **
  • Posts: 87
Re: Questions about ComboBox events
« Reply #3 on: June 03, 2025, 02:17:13 am »
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

  • Hero Member
  • *****
  • Posts: 693
Re: Questions about ComboBox events
« Reply #4 on: June 03, 2025, 01:25:28 pm »
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][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.   {TForm1}
  12.   TForm1 = class(TForm)
  13.     ComboBox1: TComboBox;
  14.     Memo1: TMemo;
  15.     procedure ComboBox1Change(Sender: TObject);
  16.     procedure ComboBox1Click(Sender: TObject);
  17.     procedure ComboBox1DropDown(Sender: TObject);
  18.     procedure ComboBox1Select(Sender: TObject);
  19.   end;
  20.  
  21. var
  22.   Form1: TForm1;
  23.  
  24. implementation
  25.  
  26. {$R *.lfm}
  27.  
  28. {TForm1}
  29.  
  30. procedure TForm1.ComboBox1DropDown(Sender: TObject);
  31. begin
  32.   {}
  33.   Memo1.Lines.Clear;
  34.   Memo1.Lines.Add('Internal list of ComboBox was shown!');
  35. end;
  36.  
  37. procedure TForm1.ComboBox1Click(Sender: TObject);
  38. begin
  39.   {}
  40.   Memo1.Lines.Add('Internal button of ComboBox was clicked!');
  41. end;
  42.  
  43. procedure TForm1.ComboBox1Change(Sender: TObject);
  44. begin
  45.   {}
  46.   Memo1.Lines.Add('Visible content in ComboBox was changed!');
  47. end;
  48.  
  49. procedure TForm1.ComboBox1Select(Sender: TObject);
  50. begin
  51.   {}
  52.   Memo1.Lines.Add('Item from internal list of ComboBox was selected!');
  53. end;
  54.  
  55. end.

tfurnivall

  • Jr. Member
  • **
  • Posts: 87
Re: Questions about ComboBox events
« Reply #5 on: June 03, 2025, 02:58:27 pm »
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

  • Hero Member
  • *****
  • Posts: 1496
Re: Questions about ComboBox events
« Reply #6 on: June 03, 2025, 05:00:51 pm »
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

https://lazarus-ccr.sourceforge.io/docs/lcl/stdctrls/tcombobox.html


Preview the next Lazarus documentation release at: https://dsiders.gitlab.io/lazdocsnext

tfurnivall

  • Jr. Member
  • **
  • Posts: 87
Re: [SOLVED]Questions about ComboBox events
« Reply #7 on: June 03, 2025, 06:03:38 pm »
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

  • Hero Member
  • *****
  • Posts: 7405
Re: [SOLVED]Questions about ComboBox events
« Reply #8 on: June 04, 2025, 12:24:58 am »
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
The only true wisdom is knowing you know nothing

RayoGlauco

  • Full Member
  • ***
  • Posts: 221
  • Beers: 1567
Re: [SOLVED]Questions about ComboBox events
« Reply #9 on: June 05, 2025, 12:12:15 pm »
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][+][-]
  1. if ix>=0 then
  2.   DomainOption := cboFDomain.items [ix];
« Last Edit: June 05, 2025, 12:15:30 pm by RayoGlauco »
To err is human, but to really mess things up, you need a computer.

 

TinyPortal © 2005-2018