1

There is this link How do I Invoke a procedure when inside another procedure in Pascal But its not exactly my case.

procedure TForm1.Button1Click(Sender: TObject);
var
   [...]
begin
  // click on button
  [...]
end; 

and I have this procedure

procedure TForm1.CheckListBox2DblClick(Sender: TObject);
begin
  // on double click in flags
  [the same code like above]
end; 

i tryed this but it does not work

procedure TForm1.CheckListBox2DblClick(Sender: TObject);
begin
  TForm1.Button1Click;
end;    

then I tryed this

procedure TForm1.CheckListBox2DblClick(Sender: TObject);
begin
  TForm1.Button1Click(Sender: TObject);
end; 

it also does not work
Can somebody please help me ?

Community
  • 1
  • 1

2 Answers2

3

Just call it directly, using either nil or another component as the Sender:

procedure TForm1.CheckListBox2DblClick(Sender: TObject);
begin
  Button1Click(nil);
end;  

procedure TForm1.CheckListBox2DblClick(Sender: TObject);
begin
  Button1Click(CheckListBox2);
end;      

Note you don't use the classname (or variable name) of the form itself, since you're calling from the current instance of the form. IOW, do not use TForm1 or Form1 inside of a class method; that limits you to a specific instance of the form instead of being available to all instances. If you need to qualify it, use Self, as in Self.Button1Click(nil);.

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • Thank you, It works, in 7 minutes I accept it. but what is nil? –  Feb 19 '14 at 18:17
  • `nil` is an unassigned pointer. It means "pass a pointer to nothing to the procedure, because it requires a parameter". You can test it to see if it's nil using `if Sender <> nil` or `if not Assigned(Sender)`. `Sender` is the control that is "sending" the event (causing it to be invoked); since you're doing it from another control's event handler, it may or may not be appropriate to provide a `Sender` to the event you're manually firing, based on what your code is actually doing. As I don't have that information, I demonstrated both ways. – Ken White Feb 19 '14 at 18:19
0

Try this

procedure TForm1.CheckListBox2DblClick(Sender: TObject);
begin
TForm1.Button1Click(Sender);
end; 
Random
  • 467
  • 1
  • 4
  • 9
  • I get this ERROR : procmaileditor1.pas(145,30) Error: Only class class methods, class properties and class variables can be accessed in class methods –  Feb 19 '14 at 18:14
  • the second version is also not compiling : procmaileditor1.pas(146,30) Error: Only class class methods, class properties and class variables can be accessed in class methods –  Feb 19 '14 at 18:21
  • You may try to assign the function directly TForm1.CheckListBox2DblClick:=@TForm1.Button1Click – Random Feb 19 '14 at 18:39