User5590
User5590

Reputation: 1435

Outlook 2013 Addin: Change Ribbon Button Image on click of button using c#

I have created Ribbon button for Outlook 2013 using c#.

And i have also set image for the ribbon.

Now on click of Ribbon button i want to change Ribbon Image.

Is it possible to achieve this using c#.?

Upvotes: 1

Views: 850

Answers (2)

Erevodifosin
Erevodifosin

Reputation: 31

This question is 3 yo but helped me to go further and I want to share this with you. First, I accomplished this in VB.net so my code will be in VB.net. There are some online tools to convert the code into C#. Second, I used a Toggle button instead of a Simple button. Third, I used OnOff as a project setting in order to save the state of the Toggle button.

Step 1: Ribbon.xml file, code to place the toggle button on the ribbon. Assuming you already have set up the tab and group tags in the file.

<toggleButton id="onoffTBTN" label="ON/OFF" showImage="true" onAction="OnOffToggle" getImage="OnOffImage"/>

Step 2: Ribbon.vb file, code to change the OnOff setting based on the Toggle button status(pressed or not) and forces to invalidate the custom control

Public Sub OnOffToggle(ByVal control As Office.IRibbonControl, ByVal pressed As Boolean)
    My.Settings.OnOff = pressed
    My.Settings.Save()
    myRibbon.InvalidateControl("onoffTBTN")
End Sub

Step 3: Ribbon.vb file, reads the OnOff setting and changes the image accordingly. Have in mind that your images must have been added to your project resources in order to use them at My.Resources.*. I used png files that support transparent pixels. This function is called in two occasions, first when the Outlook starts and second when the toggle button is pressed and specifically with the command myRibbon.InvalidateControl("onoffTBTN").

Public Function OnOffImage(ByVal control As Office.IRibbonControl) As Drawing.Bitmap
    Dim onoff As Boolean = My.Settings.OnOff
    Select Case control.Id
        Case "onoffTBTN"
            If onoff = True Then
                Return New Drawing.Bitmap(My.Resources._on)
            Else
                Return New Drawing.Bitmap(My.Resources.off)
            End If
    End Select
End Function

The only weird behaviour is when the OnOff setting has been set to TRUE. The correct image is displayed but the Toggle button looks unpressed. You have to click twice in order to set the OnOff setting to False.

Upvotes: 0

Buggy
Buggy

Reputation: 135

Not sure how exactly you want it to work, but this could do the trick.

bool callback {get;set}

public Bitmap GetImage(IRibbonControl control)
    {
        switch (control.Id)
        {

            case "FooButtonId":
            {
                if(callback== true){
                    callback = false;
                    return new Bitmap(Properties.Resources.someimage1);
                }else 
                    callback =true;
                    return new Bitmap(Properties.Resources.someimage2);
                }
            }
        }
    }

Upvotes: 2

Related Questions