Search code examples
c#revit-api

Button with image and empty string in Revit API


I am creating a new tab with some stacked buttons in revit. I need to place a button without text such as the attached image: image of buttons without text.

I am using the following code:

RibbonPanel panelMenu6 = application.CreateRibbonPanel(tabName, panelNameMenu6);

//create large buttons
PushButtonData Button23 = new PushButtonData("Button 23", "Button 23", directoryName + @"\pyRevit.dll", "pyRevit.TheCommand");
PushButton button23 = panelMenu6.AddItem(Button23) as PushButton;
button23.LargeImage = new BitmapImage(new Uri(directoryName + "\\Icons Resources\\Button 17 -bulleted-list-16 (2).png"));

//create small buttons
PushButtonData Button24 = new PushButtonData("Button 24", "Button 24", directoryName + @"\pyRevit.dll", "pyRevit.TheCommand");
Button24.Image = new BitmapImage(new Uri(directoryName + "\\Icons Resources\\Button 23 -address-16 (2).png"));

PushButtonData Button25 = new PushButtonData("Button 25", "Button 25", directoryName + @"\pyRevit.dll", "pyRevit.TheCommand");
Button25.Image = new BitmapImage(new Uri(directoryName + "\\Icons Resources\\Button 24 -paint-palette-16 (2).png"));
panelMenu6.AddStackedItems(Button24, Button25);

I tried to send an empty string instead of "button 25" but I receive an exception "The value cannot be empty. Parameter name: text".

That's the code when I receive the error. For instance, Button 24 without text:

PushButtonData Button24 = new PushButtonData("Button 24", **""**, directoryName + @"\pyRevit.dll", "pyRevit.TheCommand");

I try to create a pushbutton such as those in the attached pic 1. Image only and no text


Solution

  • Thanks to this blog I was able to create buttons without text. here is the code

    using AW = Autodesk.Windows;
    /// <summary>
        /// Each item shall be converted into Autodesk windows item
        /// </summary>
        /// <param name="tabName"></param>
        /// <param name="panelName"></param>
        /// <param name="itemName"></param>
        /// <returns></returns>
        public AW.RibbonItem GetButton(string tabName, string panelName, string itemName)
        {
            AW.RibbonControl ribbon = AW.ComponentManager.Ribbon;
            foreach (AW.RibbonTab tab in ribbon.Tabs)
            {
                if (tab.Name == tabName)
                {
                    foreach (AW.RibbonPanel panel in tab.Panels)
                    {
                        if (panel.Source.Title == panelName)
                        {
                            return panel.FindItem("CustomCtrl_%CustomCtrl_%"
                              + tabName + "%" + panelName + "%" + itemName,
                              true) as AW.RibbonItem;
                        }
                    }
                }
            }
            return null;
        }
        /// <summary>
        /// A List contains items which shall be without text
        /// after converting each item to autodesk windows item using getbutton method
        /// set the text display to false
        /// and set the size to large
        /// </summary>
        /// <param name="ribbonItem"></param>
        /// <param name="tabName"></param>
        /// <param name="panelName"></param>
        public void editNoTextButtons(IList<RibbonItem> ribbonItem, string tabName, string panelName)
        {
            foreach (var item in ribbonItem)
            {
                var adwinbutton = GetButton(tabName, panelName, item.Name);
                adwinbutton.ShowText = false;
                adwinbutton.Size = Autodesk.Windows.RibbonItemSize.Large;
    
            }
        }
    

    And that's how it looks like