change label property of Microsoft.Office.Tools.Ribbon.RibbonTab in Microsoft.Office.Tools.Ribbon.OfficeRibbon Load event not working

Answered change label property of Microsoft.Office.Tools.Ribbon.RibbonTab in Microsoft.Office.Tools.Ribbon.OfficeRibbon Load event not working

  • 3. září 2011 14:35
     
     

    Hi,

    I need to change the label property (the text the end-user sees on the tab) of a custom ribbon (Microsoft.Office.Tools.Ribbon.RibbonTab) when the VSTO Excel Document Addin loads.  In the Microsoft.Office.Tools.Ribbon.OfficeRibbon.Load method, I execute the following line of code:

    tab1.Label = "Procuct X";

    but this has no effect (it still renders the text that is set in the Microsoft.Office.Tools.Ribbon.RibbonBase.InitializeComponent() which gets called before the Microsoft.Office.Tools.Ribbon.OfficeRibbon.Load method).  I am using VS2010, Excel 2010, C# and am using the Ribbon Designer in VS (i.e. not an XML Ribbon).  Any help would be greatly appreciated. 


    Anthony LaMark

Všechny reakce

  • 3. září 2011 16:25
    Moderátor
     
     Odpovědět

    Hi Anthony

    It helps a bit if you understand how the Ribbon XML - that the Ribbon Designer is wrapping for you - works. At Load time it runs the "callbacks" to get the initial values. If you've assigned a value at Design time, that's what's going to be loaded.

    This isn't something I've ever tried, but I think you might be able to make the assignment when the Ribbon class is initialized, in its constructor?

    Yes, it works for me. I put it directly after InitializeComponent(). (Note: on my system the constructor was located in the Ribbon1.Designer file).

    But if that doesn't work for you, then you'd need to assign the label after the Ribbon has already been loaded, on the WorkbookOpen event, for example.

    If there's a short time where the user might access or see the button with the wrong label, set it to be disabled or invisible by default and change that setting at the same time you change the label.


    Cindy Meister, VSTO/Word MVP
    • Označen jako odpověď ALaMark 3. září 2011 18:37
    •  
  • 3. září 2011 18:38
     
     
    Perfect!  Thank you very much!!!
    Anthony LaMark
  • 13. srpna 2012 21:35
     
     

    Hi,

    I'm working on a similar task - please could you share the code how to rename a tab text/label.

    Thanks


    Fleming

  • 13. srpna 2012 23:11
     
     

    Hi,

    It has been a while since I wrote this code but Cindy's answer worked for me.  You have to view and edit the code that gets generated for the Ribbon.  To do this, in the visual studio solution explorer view, under your ribbon (ex: XRibbon.cs), you will see XRibbon.Designer.cs.  Right mouse click it and then select "View Code".  I implemented Cindy's suggestion in the constructor:

        partial class XRibbon : Microsoft.Office.Tools.Ribbon.RibbonBase
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;

            public XRibbon()
                : base(Globals.Factory.GetRibbonFactory())
            {
                InitializeComponent();
                tabX.Label = XExcelSheet.m_stringProductName;
                for (int i = 0; i < tabX.Groups.Count; i++)
                {
                    if (tabX.Groups[i].Name == "groupExcelAddIn")
                    {
                        tabX.Groups[i].Label =
                        XExcelSheet.m_stringProductName +
                        " Excel AddIn";
                        break;
                    }
                }
            }

    Hope that helps some.


    Anthony LaMark