The SplitButton group can hold some PushButton and Separator ribbon items. PushButton is action centric as introduced before, so it will be associated with an external command class.
We demonstrated how to create SplitButton group and PushButton items of Revit Ribbon API before in C#. In this article, let us see how to do so in VB.NET.
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 SplitButton group and PushButton items
Public Sub AddRibbonItemsToRibbonPanel(ByVal panel As RibbonPanel)
Dim assemFullName As String = Assembly.GetExecutingAssembly.Location
Dim assemPath As String = Path.GetDirectoryName(assemFullName)
Dim cnt1Panel_grp1Data As SplitButtonData = New SplitButtonData("cnt1Panel_grp1", Guid.NewGuid.ToString)
Dim cnt1Panel_grp1 As SplitButton = CType(panel.AddItem(cnt1Panel_grp1Data), SplitButton)
Dim cnt1Panel_grp1_item1Data As PushButtonData = New PushButtonData("cnt1Panel_grp1_item1", "PushButton 1", assemFullName, "RevitAddinCSProject.ExtCmd1")
Dim cnt1Panel_grp1_item1 As PushButton = CType(cnt1Panel_grp1.AddPushButton(cnt1Panel_grp1_item1Data), PushButton)
cnt1Panel_grp1_item1.ToolTip = "PushButton 1 Tooltip"
cnt1Panel_grp1_item1.LargeImage = BmpImageSource("Revit2012VBAddin.BigA.bmp")
cnt1Panel_grp1.AddSeparator()
Dim cnt1Panel_grp1_item3Data As PushButtonData = New PushButtonData("cnt1Panel_grp1_item3", "PushButton 2", assemFullName, "RevitAddinCSProject.ExtCmd2")
Dim cnt1Panel_grp1_item3 As PushButton = CType(cnt1Panel_grp1.AddPushButton(cnt1Panel_grp1_item3Data), PushButton)
cnt1Panel_grp1_item3.ToolTip = "PushButton 2 Tooltip"
cnt1Panel_grp1_item3.LargeImage = BmpImageSource("Revit2012VBAddin.BigB.bmp")
cnt1Panel_grp1.AddSeparator()
Dim cnt1Panel_grp1_item5Data As PushButtonData = New PushButtonData("cnt1Panel_grp1_item5", "PushButton 3", assemFullName, "RevitAddinCSProject.ExtCmd3")
Dim cnt1Panel_grp1_item5 As PushButton = CType(cnt1Panel_grp1.AddPushButton(cnt1Panel_grp1_item5Data), PushButton)
cnt1Panel_grp1_item5.ToolTip = "PushButton 3 Tooltip"
cnt1Panel_grp1_item5.LargeImage = BmpImageSource("Revit2012VBAddin.BigC.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 SplitButton and PushButton creation is not hard to follow. We need to create the SplitButton container object first through creating a SplitButtonData and adding a SplitButton object into a Ribbon Panel from the data object.
Then we can add any number of PushButton and Separator instances to the SplitButton group that has been just created. The steps are pretty the same as adding ComboBoxMember items to ComboBox group, e.g. creating a data object PushButtonData first, adding a PushButton through calling the AddPushButton method against the SplitButton container next, and finally setting some other properties such as ToolTip and LargeImage.
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 if any from the Embedded Resources. It may be worth of mentioning the resource name here with 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 SplitButton container and its PushButton items look and behave exactly the same.
People may have noticed that the PulldownButton behave and can be implemented almost the same as the SplitButton. It is true that they are almost the same except for some slightly difference in behavior. Both of them can hold push buttons (PushButton) and separators, and both of them can have a button and a drop down arrow, which will pop up their members if being pressed.
The difference is the appearance of the group button (PulldownButton or SplitButton) and some action to get it updated regarding the SplitButton. The former can have an image and a descriptive text for the group itself but the latter can only show the image and text of the default button or the last selected one. An interesting behavior about the SplitButton as can be seen above is that when the split group button is highlighted, a horizontal separator will appear in the middle of the button image. The first half will show the button image and the second half will not only show the drop down arrow but also the text of the default button. Though the text property of the SplitButton does not really have a chance to show up, we have to provide it anyway.
The RevitAddinWizard is going to provide the VB.NET version of the Ribbon Creator soon.
Recent Comments