How to activate the button of a DesktopToolbar?

Hi,
How to activate, by code, the button of a (Desktop)Toolbar?
I tried without success:

Toolbar1.MyButtonName_Btn.Pressed = true

Thanks

Don’t try.

Make the button call a method: eg DoStuff1
And instead of trying to ‘press’ the button in code, just call the method in code instead.

2 Likes

Thanks @Jeff_Tullin , but I prefer the method with a command like “Pressed” (or equivalent), which would save me from making too many changes in my code.

Curiously, the command Toolbar1.myBtnName.Pressed = true, does select the button but does not trigger the Toolbar1.pressed event!!!

You are confusing events vs state.

It’s unfortunate that someone at Xojo chose to keep the property named Pressed in the API 2 version of the button as it just causes confusion. The Pressed property that you are setting is just about appearance and has nothing to do with calling any code. Events cannot be “called” the way methods can, so @Jeff_Tullin got it right, if you want to be able to “press” a toolbar button from code, you will need to put that code into a method.

That’s how VB did it as I recall.

But it ties your code to the UI / window on which the button exists
Using a common method allows you to use the same code in many areas of your app without having to duplicate the code.
For instance, a menu, or a completely different window.

You want Button_clicked and something that calls button_clicked.
I say Both Button_clicked and something should both call the DoTheThing method.
They have one line of code each, and the method is the same as the original button_clicked code.

But if you don’t want to do that, I won’t try to convince you.

1 Like