How to determine state of toolbar togglebutton

This code works fine
Select Case Item.Name
Case “tbarDream”

winWizard.PagePanel1.SelectedPanelIndex = 1
Case “tbarGoal”
//MessageBox(“Goal clicked.”)
winWizard.PagePanel1.SelectedPanelIndex = 2
Case “tbarNextStep”
//messageBox(“Next Step clicked”)
winWizard.PagePanel1.SelectedPanelIndex = 3
Case “tbarOneOff”
//messageBox “One Off”
winWizard.PagePanel1.SelectedPanelIndex = 4
Case “tbarMain”
winTasksMain.Show
Case “tbarSingle”
winSingleTask.Show
End Select

I would like to determine if the first button of the toolbar whose button name is tbarDream, a toggle button, has been pressed , and is now down or has been pressed and is now up

http://documentation.xojo.com/api/deprecated/toolbarbutton.html#toolbarbutton-pressed

You cannot test for it. You cannot write
If toolbarWinstart1.ToolbarButton.Pressed = true Then

winwizard.PagePanel1.SelectedPanelIndex = 1
Else
MessageBox “Dream down”
End

It doesn’t autocomplete - see <https://xojo.com/issue/48116> - but it does work.

Insert a Toolbar (= Toolbar1), and add a ToolbarButton, set the ToolbarButton to Toggle.

Notice that the name of the Toolbutton is ToolItem1 and it IS a Toolbutton - that is imposrtant!

Put a Toolbar1 on the window and rename it to MyToolbar

Add a PushButton to the Window

Put this in the PushButton

Sub Action() Handles Action
  If MyToolBar.ToolItem1.pressed Then
    MessageBox "works"
else
    MessageBox "AAAAAARGH"
  end if
End Sub

If you are using a Xojo version < 2019 R2 then the code is

Sub Action() Handles Action
  If MyToolBar.ToolItem1.pushed Then
    MsgBox "works"
else
    MsgBox "AAAAAARGH"
  end if
End Sub

Btw you have to use the name of the button (eg “ToolItem1”, not the name of the super class “ToolbarButton”. After all YOU might know which button you mean, but the computer doesn’t (it’s like shouting “Human” instead of “Tom” and expecting Tom to come).

P.S. @Anthony_Cyphers: maybe YOU could have helped Lawrence? Just an idea …

Thank you. I think my issue is that the type of toolbar button I’m trying to test is the togglebutton. To me the word toggle means to go from one state to another. So in my mind if the togglebutton is in the up position and is then clicked it will then be in the down position. So then I would like to at some point programatically ascertain the postion/state of the toggle button. I want to find out is it up or down. I don’t want to necessarily trigger the button I just want to know its state. That does not seem to be acceptable to the compiler.

What version of Xojo are you using ?

Add a module and name it “ToolbarExtensions”
Add this method to it

Public Function IsToggleButtonPushed(extends tb as toolbar, toggleNButtonname as string) as boolean
  If tb Is Nil Then 
    Return False
  End If
  
  For i As Integer = 0 To tb.Count - 1
    
    If tb.Item(i) IsA ToolItem Then
      Dim item As ToolItem = tb.Item(i)
      
      If item.Name <> toggleNButtonname Then
        Continue
      End If
      
      If item IsA ToolButton Then
        
        Dim button As ToolButton = toolbutton(item)
        
        Return (button.Style = ToolButton.ToolStyleToggleButton) And (button.Pushed)
      End If
    End If
    
  Next
  
End Function

Then in your code you can do something like


If Toolbar11.IsToggleButtonPushed("tbarDream") Then
  MsgBox "pushed"
Else
  MsgBox "not pushed"
  
End If
1 Like

No, it isn’t. I use a toggle button in my code and test its state from a PushButton.

Which is exactly what my code is doing. So I’m frankly baffled by your response. Did you actually try it?

Thank you, that worked. A very nice solution that taught me about extends and enumerations. I did have to update it for 2019r3.2. which helped me learn some more about the debugger messages. By the way how did you post the code?

Thank you for your suggestions. You suggested the link [https://xojo.com/issue/48116 ](https://xojo.com/issue/48116). How do I access that, as I cannot click on it.

You need to put ``` before and after the code on empty lines

If you visit https://www.xojo.com/download/extras.php you can download the feedback app for your platform.

or [ code ] with a closing [ /code ]
remove the space following the [ and before the ]