Ribbon bar chaning icon during runtime.

Oct 11, 2012 at 6:37 PM

I'll be honest I don't even know where to start to be able to do this.  I have read about it, and havn't been able to follow the helpful links that lead to microsofts walkthroughs.  So what I have is a buttonthat i need it to flip between two different icons.  A login and logout icon and/or label.  In my code now I have

public class MyRibbon : ExcelRibbon
    {

        //Excel.Application xlApp = (Excel.Application)ExcelDnaUtil.Application;
        public void RefreshCmd(IRibbonControl control)
        {
            Years.Instance.Year(2012).Set_Trial_Balance_Data();
            MessageBox.Show("Refresh Done!");
        }
        
        public void LoginCmd(IRibbonControl control)
        {
            User.Instance.LogIn();
            //Here is where I want to change the label and/or icon to logout
        }

        public void LogoutCmd(IRibbonControl control)
        {
            User.Instance.LogOut();
            //Here is where I want to change the label and/or icon to login
        }

        public void OnLoad(IRibbonUI ribbon)
        {
            //ribbon.Invalidate();
            //ribbon.InvalidateControl("LoginCmd");
        }
    }

and here is my dna file

<DnaLibrary Name="GL Add-In" RuntimeVersion="v4.0">
  <ExternalLibrary Path="MyLibrary.dll" Pack="true" />
  <CustomUI>
    <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
      <ribbon>
        <tabs>
          <tab id="customTab" label="General Ledger" insertAfterMso="TabHome">
            <group id="Refresh" label="Refresh">
              <button id="RefreshCmd" label="Reload Data" size="large" imageMso="DataRefreshAll" onAction="RefreshCmd" />
            </group>
            <group id="Detail" label="Detail">
              <button id="TrialBalanceCmd" label="Trial Balance" size="large" imageMso="DataRefreshAll" onAction="TrialBalanceCmd" />
              <button id="DetailCmd" label="Transaction Detail" size="large" imageMso="QueryMakeTable" onAction="DetailCmd" />
            </group>
            <group id="Logon" label="Logon">
              <button id="LoginCmd" label="Login" size="large" imageMso="Lock" onAction="LoginCmd" />
              <button id="LogoutCmd" label="Logout" size="large" imageMso="UnLock" onAction="LogoutCmd" /> <!--UnLock doesn't exists I'll make my own Icon-->
            </group>
          </tab>
        </tabs>
      </ribbon>
    </customUI>
  </CustomUI>
</DnaLibrary>

Coordinator
Oct 11, 2012 at 7:10 PM

Hi Glen,

It all works with callbacks. So suppose you want to change the label at runtime. Then instead of having a label="..." attribute in the xml, you set up a getLabel="..." callback. Then when the callback is called, you return the label for the current state. Same with images. The signatures for getLabel and getImage are in the Microsoft docs I linked before. For images you can directly return any kind of .NET bitmap, no need for some of the tricks you see online sometimes.

One issue is how to make the ribbon refresh - and call your callbacks - when your state changes. For this you need to call the ribbon.Invalidate(...) stuff. You wouldn't call this in onLoad, but rather when the internal state has changed and needs to be shown in the ribbon.

-Govert

Oct 11, 2012 at 8:07 PM

How do I call the ribbon.Invalidate("LoginCmd") with the  onAction="LoginCmd" it only passes the IRibbonControl not the IRibbonUI object. 

 

public void LoginCmd(IRibbonControl control)
        {
            if (User.Instance.Authenticated == false)
            {
                User.Instance.LogIn();
            }
            else
            {
                User.Instance.LogOut();
            }
            

            //Here is where I want to change the label and/or icon to logout
            
        }

 

Coordinator
Oct 11, 2012 at 8:42 PM

Maybe you need to grab and hold the IRibbonUI when you get it in onLoad.?

-Govert

Oct 11, 2012 at 8:58 PM

YEs got it, I set it to a private variable on the onLoad.  So I could use it later.  Thanks again!