title: Setting the OnClick procedure of a Delphi button at runtime
body: <p>I have a program in which I need to update a database table with information entered into edit boxes, with a button at the end to do the updating. However, the form is created at runtime and all the elements including the button are also created in the same way. I figured a way to allow the database arguments would be to define a procedure to update the database such as:</p>
  
  <pre><code>procedure UpdateDatabase(Field1,Field2,Field3:string);
  begin
  //update database here...
  end;
  </code></pre>
  
  <p>Then assign the OnClick event of my button to this procedure with the parameters pre filled like:</p>
  
  <pre><code>Button1.OnClick := UpdateDatabase(Edit1.text,Edit2.Text,Edit3.Text);
  </code></pre>
  
  <p>However the types are incompatible as it requires a different data type. I also noticed that parameters can't usually be passed into a OnClick function. Is there actually a way of achieving what I have proposed?</p>
  
  <p>This is my current create button code:</p>
  
  <pre><code>function buttonCreate(onClickEvent: TProcedure; 
    left: integer; top: integer; width: integer; height: integer; 
    anchors: TAnchors; caption: string; parent: TWinControl; form: TForm;): TButton;
  var
    theButton: TButton;
  begin
    theButton := TButton.Create(form);
    theButton.width := width;
    theButton.height := height;
    theButton.left := left;
    theButton.top := top;
    theButton.parent := parent;
    theButton.anchors := anchors;
    //theButton.OnClick := onClickEvent;
    theButton.Caption := caption;
    result := theButton;
  end;
  </code></pre>
  
  <p>Any and all help appreciated!</p>