# Push a button programmatically

**URL:** <https://forum.xojo.com/t/push-a-button-programmatically/9226>\
**Category:** General\
**Created:** [June 30, 2013, 12:13pm UTC](https://forum.xojo.com/t/push-a-button-programmatically/9226 "2013-06-30T12:13:00Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexander\_van\_der\_Linden](https://forum.xojo.com/user_avatar/forum.xojo.com/alexander_van_der_linden/32/323_2.png) [@Alexander\_van\_der\_Linden](https://forum.xojo.com/u/Alexander_van_der_Linden)\
**Post date:** [June 30, 2013, 12:13pm UTC](https://forum.xojo.com/t/push-a-button-programmatically/9226/1 "2013-06-30T12:13:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Oliver\_Osswald](https://forum.xojo.com/user_avatar/forum.xojo.com/oliver_osswald/32/6972_2.png) [@Oliver\_Osswald](https://forum.xojo.com/u/Oliver_Osswald)\
**Post date:** [June 30, 2013, 12:19pm UTC](https://forum.xojo.com/t/push-a-button-programmatically/9226/2 "2013-06-30T12:19:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![DaveS](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/848f3c/32.png) [@DaveS](https://forum.xojo.com/u/DaveS)\
**Post date:** [June 30, 2013, 3:24pm UTC](https://forum.xojo.com/t/push-a-button-programmatically/9226/3 "2013-06-30T15:24:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Oliver\_Osswald](https://forum.xojo.com/user_avatar/forum.xojo.com/oliver_osswald/32/6972_2.png) [@Oliver\_Osswald](https://forum.xojo.com/u/Oliver_Osswald)\
**Post date:** [June 30, 2013, 5:44pm UTC](https://forum.xojo.com/t/push-a-button-programmatically/9226/4 "2013-06-30T17:44:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![DaveS](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/848f3c/32.png) [@DaveS](https://forum.xojo.com/u/DaveS)\
**Post date:** [June 30, 2013, 5:50pm UTC](https://forum.xojo.com/t/push-a-button-programmatically/9226/5 "2013-06-30T17:50:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Alexander\_van\_der\_Linden](https://forum.xojo.com/user_avatar/forum.xojo.com/alexander_van_der_linden/32/323_2.png) [@Alexander\_van\_der\_Linden](https://forum.xojo.com/u/Alexander_van_der_Linden)\
**Post date:** [June 30, 2013, 6:31pm UTC](https://forum.xojo.com/t/push-a-button-programmatically/9226/6 "2013-06-30T18:31:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [June 30, 2013, 7:04pm UTC](https://forum.xojo.com/t/push-a-button-programmatically/9226/7 "2013-06-30T19:04:14Z")

</div>

> [@17503:@Alexander van der Linden](#):
>
> How to push a button programmatically? I see that once a event existed Pushbutton1.push… but not anymore…

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

---

<div class="post-metadata">

**Author:** ![Eduardo\_Gutierrez\_de\_Oliveira](https://forum.xojo.com/user_avatar/forum.xojo.com/eduardo_gutierrez_de_oliveira/32/264_2.png) [@Eduardo\_Gutierrez\_de\_Oliveira](https://forum.xojo.com/u/Eduardo_Gutierrez_de_Oliveira)\
**Post date:** [July 2, 2013, 7:37am UTC](https://forum.xojo.com/t/push-a-button-programmatically/9226/8 "2013-07-02T07:37:05Z")

</div>

[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.

---

<div class="post-metadata">

**Author:** ![Richard\_Corlett](https://forum.xojo.com/letter_avatar_proxy/v4/letter/r/74df32/32.png) [@Richard\_Corlett](https://forum.xojo.com/u/Richard_Corlett)\
**Post date:** [July 2, 2013, 1:40pm UTC](https://forum.xojo.com/t/push-a-button-programmatically/9226/9 "2013-07-02T13:40:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![EDGARD\_LANDWEHRKAMP](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/3e96dc/32.png) [@EDGARD\_LANDWEHRKAMP](https://forum.xojo.com/u/EDGARD_LANDWEHRKAMP)\
**Post date:** [August 18, 2013, 1:35pm UTC](https://forum.xojo.com/t/push-a-button-programmatically/9226/10 "2013-08-18T13:35:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Marc\_Zeedar](https://forum.xojo.com/user_avatar/forum.xojo.com/marc_zeedar/32/1007_2.png) [@Marc\_Zeedar](https://forum.xojo.com/u/Marc_Zeedar)\
**Post date:** [August 18, 2013, 3:35pm UTC](https://forum.xojo.com/t/push-a-button-programmatically/9226/11 "2013-08-18T15:35:45Z")

</div>

> [@28053:@EDGARD LANDWEHRKAMP](#):
>
> he me.push is not available to a bevel button. Why is that ?

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.

---

<div class="post-metadata">

**Author:** ![William\_Koperwhats](https://forum.xojo.com/user_avatar/forum.xojo.com/william_koperwhats/32/230_2.png) [@William\_Koperwhats](https://forum.xojo.com/u/William_Koperwhats)\
**Post date:** [August 19, 2013, 3:03pm UTC](https://forum.xojo.com/t/push-a-button-programmatically/9226/13 "2013-08-19T15:03:33Z")

</div>

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?](http://stackoverflow.com/questions/956255/why-is-it-bad-practice-to-call-an-eventhandler-from-code)

---

<div class="post-metadata">

**Author:** ![Markus\_Winter](https://forum.xojo.com/user_avatar/forum.xojo.com/markus_winter/32/144_2.png) [@Markus\_Winter](https://forum.xojo.com/u/Markus_Winter)\
**Post date:** [August 19, 2013, 5:59pm UTC](https://forum.xojo.com/t/push-a-button-programmatically/9226/14 "2013-08-19T17:59:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![William\_Koperwhats](https://forum.xojo.com/user_avatar/forum.xojo.com/william_koperwhats/32/230_2.png) [@William\_Koperwhats](https://forum.xojo.com/u/William_Koperwhats)\
**Post date:** [August 19, 2013, 6:03pm UTC](https://forum.xojo.com/t/push-a-button-programmatically/9226/15 "2013-08-19T18:03:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 4:43pm UTC](https://forum.xojo.com/t/push-a-button-programmatically/9226/16 "2020-10-29T16:43:46Z")

</div>


