0

I'm a Matric Student in South Africa. I have to create a PAT project for assessment. I have created a dynamic form with a dynamic button and edit on it. But I need to fire the on click event for the button when it is clicked. I'm at a loss right now. They taught us to access properties of dynamic objects like the cells property of a String Grid, but not how to fire the events for dynamic objects.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Can you post the code that you have so far? The dynamic form and dynamic button would be a good place to start. – wolffer-east Aug 28 '14 at 19:02
  • 2
    I don't know how far you want to get with your project, but as proper way I would consider using actions. You would create a `TAction`, write a code for its `OnExecute` event and assign it to the created button through the `Action` property. Then you would `Execute` the action instead of triggering the click event. – TLama Aug 28 '14 at 19:25
  • Why do you want to fire that event. Can't you just call a method directly? – David Heffernan Aug 28 '14 at 19:49

1 Answers1

6

try this

procedure TForm1.btnNewClick(Sender: TObject);
begin
   // do something...
end;    

procedure TForm1.FormCreate(Sender: TObject);
begin
   btnNew := TButton.Create(Self);
   btnNew.Parent := Self;
   btnNew.OnClick := btnNewClick;
   // set other properties as needed ...
end;

If you need to "click" the button in code, you can do this:

btnNew.Click;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Passella
  • 640
  • 1
  • 8
  • 23
  • 1
    This answers the question. But one wonders whether it misses an opportunity to advise. If `btnNewClick` were to call a method and do more more then `btNew.Click` could be replaced with a call to that method. One wonders why go to the trouble of clicking on a button to indirectly get something done. – David Heffernan Aug 28 '14 at 20:15