none
Set ribbon toggleButton state without clicking the button RRS feed

  • Question

  • I'm working with a template NOT a VS add-in.

    I have a toggleButton on a custom ribbon that calls a macro that checks some properties of the active document and displays it's companion document side by side using the Windows.CompareSideBySideWith command.

    My problem is that when this is done the toggleButton on the companion document's ribbon isn't in the pressed state.

    So, in the macro I have to call the Windows.CompareSideBySideWith command then set the state of the toggle button on the companion document's ribbon. AND, if the user closes either document the toggleButton on the remaining document stays in the pressed state even though the documents are no longer side by side. So, I'd need to set that button back to NOT pressed.

    Is there away to simply set the pressed state of a toggle button? I am setting a global IRibbonUI object in an OnLoad called by the XML but I'm not sure that helps as I'll have to get a reference to the ribbon on a certain document window.

    Many thanks,
    Ken

    Sunday, June 3, 2018 1:51 AM

Answers

  • Is there away to simply set the pressed state of a toggle button? I am setting a global IRibbonUI object in an OnLoad called by the XML but I'm not sure that helps as I'll have to get a reference to the ribbon on a certain document window.

    Your toggle button get the state from your GetPressed callback in your code. So all you have to do is to force the ribbon to call that callback.

    You can do that if you call the InvalidateControl of the IRibbonUI object for that button only, or call Invalidate to force a redraw of all controls of the Ribbon.

    Andreas.

    • Proposed as answer by Terry Xu - MSFT Monday, June 4, 2018 7:58 AM
    • Marked as answer by KenKrugh Wednesday, June 6, 2018 1:04 PM
    Sunday, June 3, 2018 8:30 AM

All replies

  • Is there away to simply set the pressed state of a toggle button? I am setting a global IRibbonUI object in an OnLoad called by the XML but I'm not sure that helps as I'll have to get a reference to the ribbon on a certain document window.

    Your toggle button get the state from your GetPressed callback in your code. So all you have to do is to force the ribbon to call that callback.

    You can do that if you call the InvalidateControl of the IRibbonUI object for that button only, or call Invalidate to force a redraw of all controls of the Ribbon.

    Andreas.

    • Proposed as answer by Terry Xu - MSFT Monday, June 4, 2018 7:58 AM
    • Marked as answer by KenKrugh Wednesday, June 6, 2018 1:04 PM
    Sunday, June 3, 2018 8:30 AM
  • Hello Ken,

    You can customize the Ribbon UI by using callback procedures in COM add-ins. For each of the callbacks the add-in implements, the responses are cached.

    Basically you need to use ribbon callbacks and depending on the active window return a context-based value. The toggleButton control provides the following callback where you can check whether the button should be pressed or not and return appropriate value:

    getPressed

    callback

    Enables you to specify whether the toggle button control is pressed.

    Here is how it should look:

    C#: bool GetPressed(IRibbonControl control)

    VBA: Sub GetPressed(control As IRibbonControl, ByRef returnValue)

    C++: HRESULT GetPressed([in] IRibbonControl *pControl, [out, retval] VARIANT_BOOL *pvarfPressed)

    Visual Basic: Function GetPressed(control As IRibbonControl) As Boolean

    Read more about the ribbon UI (aka Fluent UI) in the following series of articles:

    Customizing the 2007 Office Fluent Ribbon for Developers (Part 1 of 3)

    Customizing the 2007 Office Fluent Ribbon for Developers (Part 2 of 3)

    Customizing the 2007 Office Fluent Ribbon for Developers (Part 3 of 3)

    If you want to update the pressed state you may call the Invalidate or InvalidateControl method of the IRibbonUI interface.  For example, if an add-in writer implements the getImage callback procedure for a button, the function is called once, the image loads, and then if the image needs to be updated, the cached image is used instead of recalling the procedure. This process remains in-place until the add-in signals that the cached values are invalid by using the Invalidate method, at which time, the callback procedure is again called and the return response is cached. The add-in can then force an immediate update of the UI by calling the Refresh method.


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Sunday, June 3, 2018 8:33 AM
  • Thanks for answering guys and thanks, Eugene, for the explanation of the invalidate. I've used it before to reset a drop down list but didn't understand why it was invalidate, makes sense that it's flushing the cache so to speak.

    As it turns out I only needed to use getPressed call back to make the toggle button on the 2nd document pressed. I didn't need to invalidate.

    I THINK this is because the windows are then arranged with the CompareSideBySideWith command, which causes the ribbon to be refreshed. Can someone confirm that?

    The caveat to that is that if there is a third document open the invalidate is needed for THAT document to have the button pressed. Which is OK because you can only have 2 set of compare side by side documents going at the same time.

    No on to trying to RESET to the toggle if the user closes one of the side by side documents. :o)

    Thanks again,
    Ken


    • Edited by KenKrugh Wednesday, June 6, 2018 1:24 PM
    Wednesday, June 6, 2018 1:03 PM