We have already demonstrated various ribbon items with many samples in VB.NET but each one was putting some similar items either in a RibbonPanel or in a Stacked Group. In this sample, let us put them all together in a single ribbon panel.
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 Various Items
' Layout Various Items in a Single Revit Ribbon Panel
Public Sub AddRibbonItemsToRibbonPanel(ByVal panel As RibbonPanel)
Dim assemFullName As String = Assembly.GetExecutingAssembly().Location
Dim assemPath As String = Path.GetDirectoryName(assemFullName)
Dim cnt1Panel_grp0_item1Data As PushButtonData = New PushButtonData("cnt1Panel_grp0_item1", "PushButton 1", assemFullName, "Revit2012VBAddin.ExtCmd")
Dim cnt1Panel_grp0_item1 As PushButton = CType(panel.AddItem(cnt1Panel_grp0_item1Data), PushButton)
cnt1Panel_grp0_item1.ToolTip = ""
cnt1Panel_grp0_item1.LargeImage = BmpImageSource("Revit2012VBAddin.BigA.bmp")
panel.AddSeparator()
Dim cnt2Panel_grp1Data As ComboBoxData = New ComboBoxData("cnt2Panel_grp1")
Dim cnt2Panel_grp1 As Autodesk.Revit.UI.ComboBox = CType(panel.AddItem(cnt2Panel_grp1Data), Autodesk.Revit.UI.ComboBox)
cnt2Panel_grp1.ItemText = "Combo"
cnt2Panel_grp1.ToolTip = ""
Dim cnt2Panel_grp1_item1Data As ComboBoxMemberData = New ComboBoxMemberData("cnt2Panel_grp1_item1", "Combo Member")
Dim cnt2Panel_grp1_item1 As ComboBoxMember = cnt2Panel_grp1.AddItem(cnt2Panel_grp1_item1Data)
cnt2Panel_grp1_item1.ToolTip = ""
cnt2Panel_grp1_item1.Image = BmpImageSource("Revit2012VBAddin.smalla.bmp")
panel.AddSeparator()
Dim cnt3Panel_grp1_item2Data As TextBoxData = New TextBoxData("cnt3Panel_grp1_item2")
Dim cnt3Panel_grp1_item2 As Autodesk.Revit.UI.TextBox = CType(panel.AddItem(cnt3Panel_grp1_item2Data), Autodesk.Revit.UI.TextBox)
cnt3Panel_grp1_item2.ToolTip = ""
cnt3Panel_grp1_item2.Value = "TextBox"
cnt3Panel_grp1_item2.Image = BmpImageSource("Revit2012VBAddin.CheckAndUpdate.bmp")
cnt3Panel_grp1_item2.ShowImageAsButton = True
AddHandler cnt3Panel_grp1_item2.EnterPressed, AddressOf CallbackOfTextBoxEnterPressed
panel.AddSeparator()
Dim cnt4Panel_grp1Data As RadioButtonGroupData = New RadioButtonGroupData("cnt4Panel_grp1")
Dim cnt4Panel_grp1 As RadioButtonGroup = CType(panel.AddItem(cnt4Panel_grp1Data), RadioButtonGroup)
Dim cnt4Panel_grp1_item1Data As ToggleButtonData = New ToggleButtonData("cnt4Panel_grp1_item1", "Toggle Button 1", assemFullName, "Revit2012VBAddin.ExtCmd1")
Dim cnt4Panel_grp1_item1 As ToggleButton = cnt4Panel_grp1.AddItem(cnt4Panel_grp1_item1Data)
cnt4Panel_grp1_item1.ToolTip = ""
cnt4Panel_grp1_item1.LargeImage = BmpImageSource("Revit2012VBAddin.E.bmp")
Dim cnt4Panel_grp1_item2Data As ToggleButtonData = New ToggleButtonData("cnt4Panel_grp1_item2", "Toggle Button 2", assemFullName, "Revit2012VBAddin.ExtCmd2")
Dim cnt4Panel_grp1_item2 As ToggleButton = cnt4Panel_grp1.AddItem(cnt4Panel_grp1_item2Data)
cnt4Panel_grp1_item2.ToolTip = ""
cnt4Panel_grp1_item2.LargeImage = BmpImageSource("Revit2012VBAddin.F.bmp")
Dim cnt4Panel_grp1_item3Data As ToggleButtonData = New ToggleButtonData("cnt4Panel_grp1_item3", "Toggle Button 3", assemFullName, "Revit2012VBAddin.ExtCmd3")
Dim cnt4Panel_grp1_item3 As ToggleButton = cnt4Panel_grp1.AddItem(cnt4Panel_grp1_item3Data)
cnt4Panel_grp1_item3.ToolTip = ""
cnt4Panel_grp1_item3.LargeImage = BmpImageSource("Revit2012VBAddin.G.bmp")
panel.AddSeparator()
Dim cnt5Panel_grp1_item2Data As PushButtonData = New PushButtonData("cnt5Panel_grp1_item2", "PushButton 2", assemFullName, "Revit2012VBAddin.ExtCmd2")
Dim cnt5Panel_grp1_item2 As PushButton = CType(panel.AddItem(cnt5Panel_grp1_item2Data), PushButton)
cnt5Panel_grp1_item2.ToolTip = ""
cnt5Panel_grp1_item2.LargeImage = BmpImageSource("Revit2012VBAddin.BigB.bmp")
Dim cnt6Panel_grp1Data As PulldownButtonData = New PulldownButtonData("cnt6Panel_grp1", "Pulldown")
Dim cnt6Panel_grp1 As PulldownButton = CType(panel.AddItem(cnt6Panel_grp1Data), PulldownButton)
cnt6Panel_grp1.ToolTip = ""
cnt6Panel_grp1.LargeImage = BmpImageSource("Revit2012VBAddin.ExtCmd.bmp")
Dim cnt6Panel_grp1_item1Data As PushButtonData = New PushButtonData("cnt6Panel_grp1_item1", "Pulldown Member", assemFullName, "Revit2012VBAddin.ExtCmd3")
Dim cnt6Panel_grp1_item1 As PushButton = CType(cnt6Panel_grp1.AddPushButton(cnt6Panel_grp1_item1Data), PushButton)
cnt6Panel_grp1_item1.ToolTip = ""
cnt6Panel_grp1_item1.LargeImage = BmpImageSource("Revit2012VBAddin.smallb.bmp")
panel.AddSeparator()
Dim cnt7Panel_grp1_item2Data As PushButtonData = New PushButtonData("cnt7Panel_grp1_item2", "PushButton 3", assemFullName, "Revit2012VBAddin.ExtCmd")
Dim cnt7Panel_grp1_item2 As PushButton = CType(panel.AddItem(cnt7Panel_grp1_item2Data), PushButton)
cnt7Panel_grp1_item2.ToolTip = ""
cnt7Panel_grp1_item2.LargeImage = BmpImageSource("Revit2012VBAddin.BigC.bmp")
Dim cnt8Panel_grp1Data As SplitButtonData = New SplitButtonData("cnt8Panel_grp1", Guid.NewGuid().ToString())
Dim cnt8Panel_grp1 As SplitButton = CType(panel.AddItem(cnt8Panel_grp1Data), SplitButton)
Dim cnt8Panel_grp1_item1Data As PushButtonData = New PushButtonData("cnt8Panel_grp1_item1", "Split Member", assemFullName, "Revit2012VBAddin.ExtCmd1")
Dim cnt8Panel_grp1_item1 As PushButton = CType(cnt8Panel_grp1.AddPushButton(cnt8Panel_grp1_item1Data), PushButton)
cnt8Panel_grp1_item1.ToolTip = ""
cnt8Panel_grp1_item1.LargeImage = BmpImageSource("Revit2012VBAddin.smallc.bmp")
End Sub
Public Shared Sub CallbackOfTextBoxEnterPressed(ByVal sender As Object, ByVal args As TextBoxEnterPressedEventArgs)
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 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 and its various items look the same. So do those hosted member items. Something else interesting can also be spotted. In addition to what we observed before, let us summarize some points here.
• PushButton will always automatically adjust its width based on its text length.
• ToggleButton, PulldownButton and SplitButton behave the same.
• ComboBox is always long enough even if its members have no much to display.
• ComboBox is always about one third tall as PushButton, PulldownButton, ToggleButton, or SplitButton if not stacked.
• ComboBox will always stay at the middle of the vertical space.
• TextBox is always long enough even if its value/text has nothing.
• TextBox is always about one third tall as PushButton, PulldownButton, ToggleButton, or SplitButton if not stacked.
• TextBox will always stay at the middle of the vertical space.
• TextBox seems to be always as tall as ComboBox.
• TextBox also seems to be always as long as ComboBox.
• The check mark beside the TextBox is not born in nature. It is added programmatically to make things look cool and behave well.
• ToggleButton and PushButton look very alike including their highlight color.
• PulldownButton and SplitButton also look very alike if not highlighted.
• PulldownButton can have its own image, text and tooltip.
• SplitButton can only display the image and text of one of its members either last selected or always as specified.
• If one ToggleButton is clicked, it will be always highlighted and other ToggleButton items in the same group look as normal. Therefore, ToggleButton here really behaves like RadioButton.
• Though all the ToggleButton items are grouped in a single RadioButtonGroup, there are no visual clues at all.
• There does not seem a way to make the image transparent, or emerge it into the background, clicked or not.
• If more ribbon items being added like these, the RibbonPanel space will run out very soon.
The RevitAddinWizard is going to provide the VB.NET version of the Ribbon Creator soon.
Recent Comments