Creating components at run time, reassingning event handler. lazarus / 8-Other ---------------------------------------------------------------------- SlackBrane · 2008-11-14 16:03 I've done this, probably incorrectly, in TurboDelphi under Windows, and tried the same in Lazarus... Code Snippet. Type TMyStringGrid  = class(TStringGrid)   procedure MSGOnClick(Sender: TObject); end; var MyStringGrid: TMyStringGrid; procedure CreateEdit(); begin   MyStringGrid := TMyStringGrid.Create(nil);   With MyStringGrid do   begin     Visible         := False;     Parent          := PanelForm.Panel2;     Top             := 1;     Left            := 0;     Width           := 449;     DefaultColWidth := 148;     Options         := Options + [goEditing];     ColCount        := 0;     ScrollBars      := ssNone;     Columns.Add;     Columns.Add;     Columns.Add;     Columns[0].Title.Caption := 'Item';     Columns[1].Title.Caption := 'Value';     Columns[2].Title.Caption := 'Filter';     RowCount   := 5;     Height     := (DefaultRowHeight+1) * RowCount;     Cells[0,1] := 'LocalName';     Cells[0,2] := 'PercentLeft';     Cells[0,3] := 'PercentWidth';     Cells[0,4] := 'Padding';     Visible    :=  True;     OnClick    :=  MSGOnClick; //Fails Here   end; end; procedure TMyStringGrid.MSGOnClick(Sender: TObject); begin end; It raises an error when I try and reassign the Onlclick event handler with... Error: Wrong number of parameters specified for call to "MSGOnClick" Hint:   Found declaration: TMyStringGrid.MSGOnClick(TObject) If I change things to... OnClick    :=  MSGOnClick(Sender: TObject); Then I get.. Error: Identifier not found "Sender" Which, if I were to guess, might mean my syntax for the procedure in the Type statement might have problems unless... well, I don't know really so rather than thrashing about with permutations and combinations, or referring to the manual I thought I would be lazy and ask here. Oh, if I comment out the offending line then things compile, link and run. Just in case Version # 0.9.27 beta Date 2008-10-05 FPC Version 2.2.2 SVN Revision 16886 i386-linux-gtk-2 (beta) Thanks for any help Keith ---------------------------------------------------------------------- Phil · 2008-11-14 22:37 Quote from: "SlackBrane"It raises an error when I try and reassign the Onlclick event handler with... Error: Wrong number of parameters specified for call to "MSGOnClick" Hint:   Found declaration: TMyStringGrid.MSGOnClick(TObject) Try this: OnClick := @MSGOnClick; The compiler thinks you're trying to call the MSGOnClick procedure and haven't included the TObject params. By including the @ it knows you want to assign the address of the procedure. Also, check if you're compiing in objfpc mode or Delphi mode. Probably in Delphi mode you won't need the "@". Note that I never, ever use the objfpc mode since I want my code to be Delphi compatible. Thanks. -Phil ---------------------------------------------------------------------- SlackBrane · 2008-11-15 02:26 Thank you very much Mr Phil. I was just about to award myself a Mr Stupid badge and tried... OnClick := MSGOcClick(MyStringGrid); Thinking it was because I was not passing the procedure anything to play with and... Incompatible types: got "untyped" expected "" So, I was still Mr Stupid. However the @ suggestion sorted it and the Delphi mode suggestion, which is the one I will use (compatibility sounds good), also works. Just in case I 'forced' Delphi mode by changing.. {$mode objfpc}{$H+} to {$mode delphi}{$H+} at the top of the unit(s). Is there something in a setup option somewhere where I can set that as the default? Thanks again Keith ---------------------------------------------------------------------- Phil · 2008-11-15 13:19 Quote from: "SlackBrane"Is there something in a setup option somewhere where I can set that as the default? If you take out any $mode directives from the top of your unit files, you can then select Delphi under Syntax mode on the Parsing tab of the Compiler Options dialog. FPC will then compile the files of your project in Delphi mode. Remember though that a $mode directive overrides this for an individual file. Thanks. -Phil ---------------------------------------------------------------------- SlackBrane · 2008-11-16 13:02 OK, probably best to leave the directives in there for clarity should someone else have to play with my mess. Thanks Keith