Push a button programmatically

How to push a button programmatically? I see that once a event existed Pushbutton1.push… but not anymore…

Subclass the pushbutton and put the action code into a method. Then call that method?

No need to subclass the pushbutton.

Have the Button Action call… PushMyButton

In a Module… create a Method called PushMyButton … add all the code you need t here.

To “simulate” just call PushMyButton

It it bad form/practice to attempt to call control events directly

Of course it works, when you call code in global namespace … but talking about ‘bad’ form/practice, then I would count not using encapsulation as belonging into that field as well.

Personally, I prefer to keep things together and use subclasses of controls, and then put any method, property, etc., OF the class INTO the class.

of course we can agree to disagree… and that is fine…

but my point is more towards… if you need to call the code OUTSIDE of the class (which “simulating” a button press is)… then the code belongs OUTSIDE the class

my opinion… my practice…my preference

Ok… Uhhmmm I think that David’s solution is easier to implement, without touching fundamental discussions about architecture…:slight_smile:

Architecture/Implementation discussions aside, Pushbutton1.Push still works and is still in the documentation.

[quote=17559:@Dave S]of course we can agree to disagree… and that is fine…

but my point is more towards… if you need to call the code OUTSIDE of the class (which “simulating” a button press is)… then the code belongs OUTSIDE the class

my opinion… my practice…my preference[/quote]

In this case, I guess both answers are fine depending on the context.

If I’m making a button whose function is very specific to the control then subclassing the pushbutton, adding the method to it and calling it from the event or directly make the most sense.

I did a similar functionality once. An app had to load a default file when a button was pressed but I was asked to have “hidden” functionality where if a file in the same format in a different location was dropped on top of the button, it would load as well. I put the loading code in a method and pressing the button would trigger the method with a pre-defined default folderitem, but dropping the file would parse the dropped object and if it found a readable folderitem with the proper extension it would call the same method with that folderitem.

In this case it was added functionality to an existing design, so modifying the button to move the method away from it but keeping it in the window made sense. If, on the other hand, I had wanted to abstract all functionality from the presentation layer, I would have the method and all related properties in a module and the window would just be a front-end for that module.

Both approaches are valid, they depend on the direction you want to take in the future, the time available for the changes and the origin of the need.

Separating functionality from presentation, though, always makes sense. It’s more work at the beginning but since you always end up doing it as the project grows larger it makes sense to begin there already, with the side benefit of ending up with sets of libraries, classes and modules you can recycle in other projects.

The PushButton.Push method is still available in 2013 R1 (Mac version).

The me.push is not available to a bevel button. Why is that ? It is easy to have the code in the Action event and then to have a key down with me.push … If the user hits enter/ return.

No idea. It’s always bugged me! It’s only available in the PushButton class and trips me up sometimes when I use a Bevel instead.

I don’t know that I’d use “push” for a “real” project as it does seem architecturally questionable, but for quick-and-dirty projects, it is much quicker and easier than creating a separate method or subclassing a button.

I asked essentially this same question on the RB-NUG many moons ago and was pointed to this (fantastically thought out) discussion:

Why is it bad practice to call an event handler from code?

The TAction is specific to Delphi though, isn’t it?

Unfortunately yes, though much of the rest of the discussion is applicable. It’s not the “prettiest” solution, but I’ve tried hard to separate my event handlers from the actual code that gets executed.