The Revit Ribbon TextBox is a data centric item that can be used to collect input from users. Revit TextBox 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 TextBox 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 TextBox Items
Public Sub AddRibbonItemsToRibbonPanel(ByVal panel As RibbonPanel)
Dim assemFullName As String = Assembly.GetExecutingAssembly.Location
Dim assemPath As String = Path.GetDirectoryName(assemFullName)
Dim cnt1Stacked_grp0_item1Data As TextBoxData = New TextBoxData("cnt1Stacked_grp0_item1")
Dim cnt1Stacked_grp0_item2Data As TextBoxData = New TextBoxData("cnt1Stacked_grp0_item2")
Dim cnt1Stacked_grp0_item3Data As TextBoxData = New TextBoxData("cnt1Stacked_grp0_item3")
Dim cnt1Stacked As IList(Of RibbonItem) = panel.AddStackedItems(cnt1Stacked_grp0_item1Data, cnt1Stacked_grp0_item2Data, cnt1Stacked_grp0_item3Data)
Dim cnt1Stacked_grp0_item1 As Autodesk.Revit.UI.TextBox = CType(cnt1Stacked(0), Autodesk.Revit.UI.TextBox)
cnt1Stacked_grp0_item1.ToolTip = "The Name of Person 1 "
cnt1Stacked_grp0_item1.Value = "Person 1"
cnt1Stacked_grp0_item1.Image = BmpImageSource("Revit2012VBAddin.CheckAndUpdate.bmp")
cnt1Stacked_grp0_item1.ShowImageAsButton = True
AddHandler cnt1Stacked_grp0_item1.EnterPressed, AddressOf CallbackOfTextBoxEnterPressed
Dim cnt1Stacked_grp0_item2 As Autodesk.Revit.UI.TextBox = CType(cnt1Stacked(1), Autodesk.Revit.UI.TextBox)
cnt1Stacked_grp0_item2.ToolTip = "The Name of Person 2"
cnt1Stacked_grp0_item2.Value = "Person 2"
cnt1Stacked_grp0_item2.Image = BmpImageSource("Revit2012VBAddin.CheckAndUpdate.bmp")
cnt1Stacked_grp0_item2.ShowImageAsButton = True
AddHandler cnt1Stacked_grp0_item2.EnterPressed, AddressOf CallbackOfTextBoxEnterPressed
Dim cnt1Stacked_grp0_item3 As Autodesk.Revit.UI.TextBox = CType(cnt1Stacked(2), Autodesk.Revit.UI.TextBox)
cnt1Stacked_grp0_item3.ToolTip = "The Name of Person 3"
cnt1Stacked_grp0_item3.Value = "Person 3"
cnt1Stacked_grp0_item3.Image = BmpImageSource("Revit2012VBAddin.CheckAndUpdate.bmp")
cnt1Stacked_grp0_item3.ShowImageAsButton = True
AddHandler cnt1Stacked_grp0_item3.EnterPressed, AddressOf CallbackOfTextBoxEnterPressed
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 code may look a bit confusing but that is exactly we have to do to create good stacked TextBox items. Let us get it straight in words as well.
• Create two or three (cannot be more or less) TextBoxData objects;
• Call the AddStackedItems() method against a RibbonPanel and pass the TextBoxData instances to its arguments;
• Collect back the stacked items into a List;
• Cast each stacked item to its right type, the TextBox, here;
• Set its necessary properties such as ToolTip, Value and Image;
• Show the image as a button through setting the ShowImageAsButton property as True;
• Add a handler to the EnterPressed event to make the TextBox behave normal.
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 TextBox items look the same. But wait, what are those check mark buttons/images really for?
They are used to synchronize the input in the editor UI and the Value of the TextBox instance in a normal way. On the other hand, the ENTER key has to be pressed to keep the data. Otherwise, the input will be lost when the focus is moving away.
The RevitAddinWizard is going to provide the VB.NET version of the Ribbon Creator soon.
Recent Comments