Revit Ribbon ComboBox is a ribbon container which can hold some ComboBoxMember items. The ComboBox ribbon items can be hosted by a RibbonPanel as demonstrated before with both C# and VB.NET code. They can be stacked into a group as well to indicate they are brothers or sisters, related to each other and looking similar.
This is achieved through the AddStackedItems() call. It has two signatures one of which accepts two ribbon item data object arguments and the other three. That implies that only two or three ribbon items can be stacked together. Moreover, the stack group only supports four types of RibbonItem, PulldownButton, PushButton, ComboBox, and TextBox. In this article, we focus on creating stacked ComboBox items using VB.NET.
In fact, another post has been there for long time discussing the Revit stacked group in detail, good or bad.
Ribbon of Revit API – 5: Stacked Group and AddStackedItems
It is always good to start with some succinct, cool, and still relatively complete code:
' Create two Ribbon Panels to host the same items and add one Panel to the 'AddIn' Ribbon Tab
' and the other to the custom 'RevitAddinWizard' Ribbon Tab if applicable in Revit 2012.
Function CreateRibbonPanel() As RibbonPanel
Dim prefix As String = "ABC" 'VendorId for example
Dim tabName1 As String = "AddIn"
Dim tabName2 As String = "RevitAddinWizard"
Dim panelTile As String = "Revit2012VBAddin"
Dim panelNameFormat As String = "{0}_{1}_{2}_{3}"
' This addes the new Ribbon Panel to the default native 'AddIn' Ribbon Tab in both Revit 2011 and 2012.
Dim panel As RibbonPanel = _cachedUiCtrApp.CreateRibbonPanel(Guid.NewGuid().ToString())
panel.Name = String.Format(panelNameFormat, prefix, tabName1, panelTile, 1) ' Unique and still possible to parse!
panel.Title = panelTile
AddRibbonItemsToRibbonPanel(panel)
' This addes the new Ribbon Panel to the custom 'RevitAddinWizard' Ribbon Tab in Revit 2012 only.
Try ' To avoid exceptions in case the same code is tried in Revit 2011
_cachedUiCtrApp.CreateRibbonTab(tabName2)
Dim panel1 As RibbonPanel = _cachedUiCtrApp.CreateRibbonPanel(tabName2, Guid.NewGuid().ToString())
panel1.Name = String.Format(panelNameFormat, prefix, tabName2, panelTile, 1) ' Unique and still possible to parse!
panel1.Title = panelTile
AddRibbonItemsToRibbonPanel(panel1)
Catch
End Try
Return panel
End Function
' Create Stacked ComboBox groups
Public Sub AddRibbonItemsToRibbonPanel(ByVal panel As RibbonPanel)
Dim assemFullName As String = Assembly.GetExecutingAssembly.Location
Dim assemPath As String = Path.GetDirectoryName(assemFullName)
Dim cnt1Stacked_grp1Data As ComboBoxData = New ComboBoxData("cnt1Stacked_grp1")
Dim cnt1Stacked_grp2Data As ComboBoxData = New ComboBoxData("cnt1Stacked_grp2")
Dim cnt1Stacked_grp3Data As ComboBoxData = New ComboBoxData("cnt1Stacked_grp3")
Dim cnt1Stacked As IList(Of RibbonItem) = panel.AddStackedItems(cnt1Stacked_grp1Data, cnt1Stacked_grp2Data, cnt1Stacked_grp3Data)
Dim cnt1Stacked_grp1 As Autodesk.Revit.UI.ComboBox = CType(cnt1Stacked(0), Autodesk.Revit.UI.ComboBox)
cnt1Stacked_grp1.ItemText = "ComboBox 1"
cnt1Stacked_grp1.ToolTip = "ComboBox Group 1"
Dim cnt1Stacked_grp1_item1Data As ComboBoxMemberData = New ComboBoxMemberData("cnt1Stacked_grp1_item1", "ComboBoxMember 11")
Dim cnt1Stacked_grp1_item1 As ComboBoxMember = cnt1Stacked_grp1.AddItem(cnt1Stacked_grp1_item1Data)
cnt1Stacked_grp1_item1.ToolTip = "ComboBoxMember 1 in Stacked Group 1"
cnt1Stacked_grp1_item1.Image = BmpImageSource("Revit2012VBAddin.smalla.bmp")
Dim cnt1Stacked_grp2 As Autodesk.Revit.UI.ComboBox = CType(cnt1Stacked(1), Autodesk.Revit.UI.ComboBox)
cnt1Stacked_grp2.ItemText = "ComboBox 2"
cnt1Stacked_grp2.ToolTip = "ComboBox Group 2"
Dim cnt1Stacked_grp2_item1Data As ComboBoxMemberData = New ComboBoxMemberData("cnt1Stacked_grp2_item1", "ComboBoxMember 21")
Dim cnt1Stacked_grp2_item1 As ComboBoxMember = cnt1Stacked_grp2.AddItem(cnt1Stacked_grp2_item1Data)
cnt1Stacked_grp2_item1.ToolTip = "ComboBoxMember 1 in Stacked Group 2"
cnt1Stacked_grp2_item1.Image = BmpImageSource("Revit2012VBAddin.smalla.bmp")
Dim cnt1Stacked_grp2_item2Data As ComboBoxMemberData = New ComboBoxMemberData("cnt1Stacked_grp2_item2", "ComboBoxMember 22")
Dim cnt1Stacked_grp2_item2 As ComboBoxMember = cnt1Stacked_grp2.AddItem(cnt1Stacked_grp2_item2Data)
cnt1Stacked_grp2_item2.ToolTip = "ComboBoxMember 2 in Stacked Group 2"
cnt1Stacked_grp2_item2.Image = BmpImageSource("Revit2012VBAddin.smallb.bmp")
Dim cnt1Stacked_grp3 As Autodesk.Revit.UI.ComboBox = CType(cnt1Stacked(2), Autodesk.Revit.UI.ComboBox)
cnt1Stacked_grp3.ItemText = "ComboBox 3"
cnt1Stacked_grp3.ToolTip = "ComboBox Group 3"
Dim cnt1Stacked_grp3_item1Data As ComboBoxMemberData = New ComboBoxMemberData("cnt1Stacked_grp3_item1", "ComboBoxMember 31")
Dim cnt1Stacked_grp3_item1 As ComboBoxMember = cnt1Stacked_grp3.AddItem(cnt1Stacked_grp3_item1Data)
cnt1Stacked_grp3_item1.ToolTip = "ComboBoxMember 1 in Stacked Group 3"
cnt1Stacked_grp3_item1.Image = BmpImageSource("Revit2012VBAddin.smalla.bmp")
Dim cnt1Stacked_grp3_item2Data As ComboBoxMemberData = New ComboBoxMemberData("cnt1Stacked_grp3_item2", "ComboBoxMember 32")
Dim cnt1Stacked_grp3_item2 As ComboBoxMember = cnt1Stacked_grp3.AddItem(cnt1Stacked_grp3_item2Data)
cnt1Stacked_grp3_item2.ToolTip = "ComboBoxMember 2 in Stacked Group 3"
cnt1Stacked_grp3_item2.Image = BmpImageSource("Revit2012VBAddin.smallb.bmp")
Dim cnt1Stacked_grp3_item3Data As ComboBoxMemberData = New ComboBoxMemberData("cnt1Stacked_grp3_item3", "ComboBoxMember 33")
Dim cnt1Stacked_grp3_item3 As ComboBoxMember = cnt1Stacked_grp3.AddItem(cnt1Stacked_grp3_item3Data)
cnt1Stacked_grp3_item3.ToolTip = "ComboBoxMember 3 in Stacked Group 3"
cnt1Stacked_grp3_item3.Image = BmpImageSource("Revit2012VBAddin.smallc.bmp")
End Sub
' Retrieve a Bitmap image resource
Public Shared Function BmpImageSource(ByVal embeddedPath As String) As System.Windows.Media.ImageSource
Dim stream As Stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(embeddedPath)
Dim decoder = New System.Windows.Media.Imaging.BmpBitmapDecoder(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default)
Return decoder.Frames(0)
End Function
The code may look a bit confusing but that is exactly we have to do to create good stacked ComboBox items. Let us get it straight in words as well.
• Create two or three (cannot be more or less) ComboBoxData objects;
• Call the AddStackedItems() method against a RibbonPanel and pass the ComboBoxData instances to its arguments;
• Collect back the stacked items into a List;
• Cast each stacked item to its right type, the ComboBox, here;
• Set its necessary properties such as ToolTip and Image;
• Add some ComboBoxMember items to the ComboBox container.
• Do similar to the other ComboBox members in the List.
The BmpImageSource() is a helper method that can be used by a Ribbon item to retrieve a specific bitmap image for its Image or LargeImage property from the Embedded Resources. It may be worth of mentioning the resource name in a few words. In VB.NET, it is generally the project Default Namespace plus the image file name regardless whether the file is put into a project folder or not. However, in C#, things are different. We have to add the folder name if any in between.
The CreateRibbonPanel() is another helper method that will create Ribbon Panels and add them to the default native ‘AddIn’ (API name, the UI name is ‘Add-Ins’) Ribbon Tab and to our custom ‘RevitAddinWizard’ Ribbon Tab (the API name and the UI name are the same) respectively. It applies some good naming conventions and follows some good coding practices such as always feeding a GUID to the panel constructor and then setting its Name as easy to remember/parse and still unique and its Title as good for display which can be duplicate.
The AddIn (API name) or Add-Ins (UI name) Ribbon Tab may look like the following:
The RevitAddinWizard (both API and UI name) Ribbon Tab may look like this:
As can be seen, except for in different Ribbon Tabs, the Ribbon Panel, the Stacked Group, and its three ComboBox items look the same. So do those hosted ComboBoxMember items.
The RevitAddinWizard is going to provide the VB.NET version of the Ribbon Creator soon.
Recent Comments