Consulting

Results 1 to 15 of 15

Thread: Ribbon - toggle button problems

  1. #1
    VBAX Regular
    Joined
    May 2012
    Posts
    15
    Location

    Ribbon - toggle button problems

    Hi guys,

    Iím not sure if this can be done, but I am trying to create two toggle buttons in a custom ribbon tab and make it such that:

    1. When toggle button 1 is pressed, toggle button 2 is switched off
    2. When toggle button 2 is pressed toggle button 1 button is switched off and so forth

    My code is below and im using invalidatecontrol method to reset a button to the off state when the other is pressed.

    Is there a simple way of getting this to work?

    [vba]

    <customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui">
    <ribbon startFromScratch="false">
    <tabs>
    <tab id="customTab" label="Custom Tab">
    <group id="customGroup" label="Custom Group">
    <toggleButton id="customButton1" label="Face 1" imageMso="HappyFace" size="large" onAction="Callback1" />
    <toggleButton id="customButton2" label="Face 2" imageMso="HappyFace" size="large" onAction="Callback2" />

    </group>
    </tab>
    </tabs>
    </ribbon>
    </customUI>
    [/vba]

    [vba]Dim PressedState As Boolean
    Public gobjRibbon As IRibbonUI

    Public Sub OnRibbonLoad(objRibbon As IRibbonUI)

    Set gobjRibbon = objRibbon

    End Sub

    Sub callback1(control As IRibbonControl, pressed As Boolean)

    MsgBox "Face 1 on - Face 2 off"
    gobjRibbon.InvalidateControl ("customButton2")


    End Sub

    Sub callback2(control As IRibbonControl, pressed As Boolean)

    MsgBox "Face 2 on - Face 1 off"
    gobjRibbon.InvalidateControl ("customButton1")

    End Sub[/vba]
    Is there a simple way of getting this to work?

    Thanks.

  2. #2
    VBAX Wizard
    Joined
    Apr 2007
    Posts
    6,251
    Location
    You just need to use the getPressed callback and to include onLoad in your xmlns


    [vba]
    Option Explicit
    Public gobjRibbon As IRibbonUI
    Public B1 As Boolean, B2 As Boolean

    Public Sub OnRibbonLoad(objRibbon As IRibbonUI)

    Set gobjRibbon = objRibbon

    B1 = False
    B2 = False

    End Sub

    'Callback for customButton1 onAction
    Sub Pushed(control As IRibbonControl, pressed As Boolean)

    Select Case control.ID
    Case "customButton1"
    B1 = True
    B2 = False


    Case "customButton2"
    B1 = False
    B2 = True

    End Select

    gobjRibbon.Invalidate

    End Sub
    'Callback for customButton1 getPressed
    Sub UpOrDown(control As IRibbonControl, ByRef returnedVal)
    Select Case control.ID
    Case "customButton1"
    returnedVal = B1

    Case "customButton2"
    returnedVal = B2
    End Select
    End Sub

    [/vba]



    [vba]
    <customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui"onLoad="OnRibbonLoad" >
    <
    ribbon startFromScratch="false">
    <
    tabs>
    <
    tab id="customTab"label="Custom Tab">
    <
    group id="customGroup"label="Custom Group">
    <
    toggleButton id="customButton1"label="Face 1"imageMso="HappyFace"size="large"onAction="Pushed"getPressed="UpOrDown" />
    <
    toggleButton id="customButton2"label="Face 2"imageMso="HappyFace"size="large"onAction="Pushed"getPressed="UpOrDown" />
    </
    group>
    </
    tab>
    </
    tabs>
    </
    ribbon>
    </
    customUI>
    [/vba]

    Paul
    Attached Files Attached Files

  3. #3
    VBAX Regular
    Joined
    May 2012
    Posts
    15
    Location
    Works really well, thanks Paul for your help.

  4. #4
    VBAX Regular
    Joined
    May 2012
    Posts
    6
    Location

    Talking Paul - loads of kudos to you !!!!

    I had this working in Excel 2003 using commandbar coding but could not figure out how to do it with a ribbon (until I came across your solution).

    Many thanks again

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master xld's Avatar
    Joined
    Apr 2005
    Posts
    24,907
    Location
    Quote Originally Posted by alex878
    I am trying to create two toggle buttons in a custom ribbon tab and make it such that:

    1. When toggle button 1 is pressed, toggle button 2 is switched off
    2. When toggle button 2 is pressed toggle button 1 button is switched off and so forth
    Isn't a toggle button supposed to have two states itself, that is using one to control another is a tad redundant?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    VBAX Expert TrippyTom's Avatar
    Joined
    Jul 2005
    Location
    New York, NY (USA)
    Posts
    556
    Location
    XLD: I think an example of where you might want to do this is when you need 2 distinct choices, but they're not necessarily on/off. Like "inches" vs. "centimeters"?

    Or would that still be a good option for a toggle button?
    Office 2010, Windows 7
    goal: to learn the most efficient way

  7. #7
    VBAX Wizard
    Joined
    Apr 2007
    Posts
    6,251
    Location
    If it were "Inches" XOR "Centimeters" and only that ,then I'd agree with XLD about the single toggle button, probably changing the getLable results. That was the OP's origional question


    If the logic were more complicated, I colud see multiple toggle buttons with a really ugly truth table


    For ex TB1, TB2, TB3 (main options), TB4, TB5 (sub-options)

    If TB1 = ON, then force TB4 = OFF, TB5=ON, and TB2 and TB3=OFF
    If TB1 = OFF, then fore TB4 = OFF, TB5=OFF, and TB2 and TB3=OFF

    If TB2 = ON, then forceTB4 = ON, TB5=OFF, and TB1 and TB3=OFF
    If TB2 = OFF, then force TB4 = OFF, TB5=OFF, and TB1 and TB3=OFF

    If TB3 = ON, then force TB4 = OFF, TB5=OFF, and TB1 and TB2=OFF
    If TB3 = OFF, then forceTB4 = OFF, TB5=OFF, and TB1 and TB2=OFF

    TB4 and TB5 still operate independently to control sub-options


    So you could have TB1=ON, and then click and have TB4=ON also


    Paul

  8. #8
    VBAX Newbie
    Joined
    Jun 2013
    Posts
    1
    Location
    You need to use the getPressed callback and return True or False accordingly.

  9. #9
    VBAX Regular
    Joined
    Jul 2014
    Posts
    14
    Location
    getPressed callback and return True or False solve the issue.

  10. #10
    Hi Paul,
    This may be outdated but just came across your solution for toggle buttons in ribbon. while it works for 2 buttons i would like it to work for 3 buttons i.e. one of the 3 button should be in pressed state. I am not a pro at vba but i tried to copy the code as in the attached file but does not work. Can you help me make it work please. I am using excel 2007.
    basically i want one toggle button to choose either one of the three currencies i.e. USD, EURO, GBP
    I have to use toggle button as i believe we cannot use option (radio) button in excel 2007 ribbon, which i would have loved to use instead.

    Thanks in advance
    Attached Files Attached Files

  11. #11
    Distinguished Lord of VBAX VBAX Grand Master xld's Avatar
    Joined
    Apr 2005
    Posts
    24,907
    Location
    Quote Originally Posted by nischalshety View Post
    Hi Paul,
    This may be outdated but just came across your solution for toggle buttons in ribbon. while it works for 2 buttons i would like it to work for 3 buttons i.e. one of the 3 button should be in pressed state. I am not a pro at vba but i tried to copy the code as in the attached file but does not work. Can you help me make it work please. I am using excel 2007.
    basically i want one toggle button to choose either one of the three currencies i.e. USD, EURO, GBP
    I have to use toggle button as i believe we cannot use option (radio) button in excel 2007 ribbon, which i would have loved to use instead.
    I have a query that uses those same 3 currency options; I use a dropdown.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  12. #12
    VBAX Wizard
    Joined
    Apr 2007
    Posts
    6,251
    Location
    Quote Originally Posted by nischalshety View Post
    Hi Paul,
    This may be outdated but just came across your solution for toggle buttons in ribbon. while it works for 2 buttons i would like it to work for 3 buttons i.e. one of the 3 button should be in pressed state. I am not a pro at vba but i tried to copy the code as in the attached file but does not work. Can you help me make it work please. I am using excel 2007.
    basically i want one toggle button to choose either one of the three currencies i.e. USD, EURO, GBP
    I have to use toggle button as i believe we cannot use option (radio) button in excel 2007 ribbon, which i would have loved to use instead.

    Thanks in advance
    One way - you can see the XML and the Callback in the xlsm

    Capture.JPG
    Attached Files Attached Files
    Paul

    ------------------------------------------------------------------------------------------------------------------------
    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s)
    (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  13. #13
    Thanks xld for your post, yes drop down can also be done, but i will be using the three toggle buttons also for other codes apart from currencies, like measurements or maybe to get data for different locations. here the dropdown can work but large toggle buttons if feel can be more useful for my purpose.

    Apreciate your feed back though...

  14. #14
    Paul, thats what i wanted, as i said i am really new to coding in VBA or XMLs i will surely study your code and try to make necessary changes to suit my requirement.
    Appreciate such a quick response buddy
    Stay healthy and merry xmas to you

  15. #15
    Please i would like to put a color on the ToggleButton of a ribbon when pressed.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •