[RESOLVED]Event handler seems not to allow local variables lazarus / 31-LCL ---------------------------------------------------------------------- tfurnivall · 2025-06-02 20:35 I have an OnClick handler defined as follows: 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;begin    ic :=TfrmFOpenTool.cboFDomain.items.Count;    ix :=TfrmFOpenTool.cboFDomain.itemindex;  end;  I'm trying to grab the values of the ItemsCount and the ItemIndex.  I have seen other handlers which declare local variables, and everything seems to work. However, my own attempt fails - miserably! 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 Only class methods, class properties and class variables can be referred with class referencesOnly class methods, class properties and class variables can be accessed in class methods    Seems pretty clear - but the highlighted portion of the offending line is around the control name. The full class definition is: 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     TfrmFOpenTool = class(TForm)        cboFName: TComboBox;        cboFName1: TComboBox;        cboFName2: TComboBox;        cmdExit: TButton;        cmdBrowse: TButton;        cboFDomain: TComboBox;      lblDeviceValue: TLabel;        lblName: TLabel;        lblStructure: TLabel;        lblOrganization: TLabel;        lblNameValue: TLabel;      lblFOptions1: TLabel;        lblStructureValue: TLabel;        lblOrganizationValue: TLabel;        lblPriority: TLabel;        lblPriorityValue: TLabel;        lblIndexCount: TLabel;        lblIndexCountValue: TLabel;        lblDomain: TLabel;        lblRecSize: TLabel;        lblDomainValue: TLabel;        lblAccess: TLabel;        lblAccessValue: TLabel;        lblDevice: TLabel;      lblRecSizeValue: TLabel;        lblDOptions: TLabel;        lblAOptions: TLabel;        lblROptions: TLabel;        lblFOptions: TLabel;        txtFileName: TEdit;        txtFileEquation: TEdit;      lblFileName: TLabel;        lblFEQ: TLabel;        PageControl1: TPageControl;        pgFOptions: TTabSheet;        pgROptions: TTabSheet;        pgAOptions: TTabSheet;        pgDOptions: TTabSheet;        pgSpoolFile: TTabSheet;        pgIndexing: TTabSheet;        procedure cboFDomainChange(Sender: TObject);        procedure cboFDomainClick(Sender: TObject);        procedure cmdExitClick(Sender: TObject);        procedure FormCreate(Sender: TObject);        procedure pgFOptionsContextPopup(Sender: TObject; MousePos: TPoint;            var Handled: Boolean);        procedure pgROptionsContextPopup(Sender: TObject; MousePos: TPoint;            var Handled: Boolean); Can anyone explain why I'm getting these errors. ---------------------------------------------------------------------- Martin_fr · 2025-06-02 20:45 Don't prefix the field name with the classname QuoteCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     ic :=TfrmFOpenTool.cboFDomain.items.Count;    ix :=TfrmFOpenTool.cboFDomain.itemindex; should be 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     ic :=cboFDomain.items.Count;    ix :=cboFDomain.itemindex; If you have a global var 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 var frmFOpenTool1: TfrmFOpenTool And if that is initialized (e.g. by the IDE generated code) then you can do 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     ic :=frmFOpenTool1.cboFDomain.items.Count;    ix :=frmFOpenTool1.cboFDomain.itemindex; Note: the variable, not the class. But you do not need that in any method of the class. You would need that in procedures that don't have the "TfrmFOpenTool." prefix in their name. ---------------------------------------------------------------------- tfurnivall · 2025-06-02 21:50 Thanks, Martin_fr, Yes - that made sense (and the compiler approves)! The error was actually pointing past the cboFDomain bit, so I assumed it was the next symbol that was causing the error. Is there any Me construct in Lazarus (analagous to the Me in VBA)? And is there any good reference for LCL controls? It's almost as if the developers can't believe that anybody wouldn't know that stuff! I've grumbled about Microsoft documentation, and Lazarus is - in general - better and more detailed (even if occasionally not fully comprehensible). However the controls are not a good example Thanks for the guidance - I'm certain that there will be more requests! Tony ---------------------------------------------------------------------- Martin_fr · 2025-06-02 22:55 Quote from: tfurnivall on June 02, 2025, 11:50:23 pmYes - that made sense (and the compiler approves)! The error was actually pointing past the cboFDomain bit, so I assumed it was the next symbol that was causing the error. Is there any Me construct in Lazarus (analagous to the Me in VBA)? Not familiar with VBA, but I guess that is "self"    self.SomeField Docs: - https://lazarus-ccr.sourceforge.io/docs/lcl/index.html - Our wiki (link in the menu on the left) - various tutorials on Pascal/Object Pascal, like https://www.delphibasics.co.uk/index.html ---------------------------------------------------------------------- n7800 · 2025-06-02 22:58 Quote from: tfurnivall on June 02, 2025, 11:50:23 pmIs there any Me construct in Lazarus (analagous to the Me in VBA)? There is Self, see docs and docs. Quote from: tfurnivall on June 02, 2025, 11:50:23 pmAnd is there any good reference for LCL controls? It's almost as if the developers can't believe that anybody wouldn't know that stuff! I've grumbled about Microsoft documentation, and Lazarus is - in general - better and more detailed (even if occasionally not fully comprehensible). However the controls are not a good example The easiest way is to select a property in the Object Inspector (property grid) and press [F1]. The link will open in a browser (site). There is also an additional package for viewing documentation offline (see wiki). A little late with the answer )) ---------------------------------------------------------------------- tfurnivall · 2025-06-02 23:21 Ah, Self! (That would be Me ;-) Thanks, Martin-fr, and n7800. These forums are worth their weight in platinum! Tony ---------------------------------------------------------------------- Zvoni · 2025-06-03 06:20 Quote from: tfurnivall on June 03, 2025, 01:21:25 amAh, Self! (That would be Me ;-) Thanks, Martin-fr, and n7800. These forums are worth their weight in platinum! Tony Addition: There is the "Sender"-Argument in the Event-Handler, which represents the Object causing the Event to be fired, in your case the Combobox itself. You could even go like this: 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;begin    ic :=Sender.items.Count;    ix :=Sender.itemindex; end;Comments of the others not withstanding (No Sanity-Check on ItemIndex etc.) ---------------------------------------------------------------------- Thaddy · 2025-06-03 07:36 The latter code won't work without a hard or soft cast, because TObject does not have those properties.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, ic : integer;begin    ic :=(Sender as TComboBox).items.Count;    ix :=(Sender as TComboBox).itemindex;end;In this case you have my permission to use a with statement. alternative: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;begin    if Sender is TComboBox then with TCombobox(Sender) do    begin       ic :=items.Count;       ix :=itemindex;    end;end; ---------------------------------------------------------------------- Zvoni · 2025-06-03 09:22 Quote from: Thaddy on June 03, 2025, 09:36:48 amThe latter code won't work without a hard or soft cast, because TObject does not have those properties. Ah yes...... Correct. Always forget that, and then the compiler reminds me of it again.. ---------------------------------------------------------------------- n7800 · 2025-06-03 12:19 You can use "absolute" if you are sure of the "Sender" type: 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    lCombo: TComboBox absolute Sender;    ix, ic : integer;begin    ic :=lCombo.Items.Count;    ix :=lCombo.ItemIndex;end; ---------------------------------------------------------------------- Thaddy · 2025-06-03 12:44 That works too, although absolute has absolutely disadvantages. But not in this case. Actually, here it is quite neat! One remark: there is no verification that sender is really a combobox...... ......and will get you into trouble if you connect the event to other components too.... Just a warning.