The Ribbon TextBox is a data collector that will show a small editor to users and collect back some input and the Ribbon Separator is a vertical thin line for display purpose only that can visually separate two ribbon items or groups left and right.
We demonstrated how to create TextBox and Separator 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 TextBox and Separator Ribbon items
Public Sub AddRibbonItemsToRibbonPanel(panel As RibbonPanel)
Dim assemFullName As String = Assembly.GetExecutingAssembly().Location
Dim assemPath As String = Path.GetDirectoryName(assemFullName)
Dim cnt1Panel_grp0_item1Data As New TextBoxData("cnt1Panel_grp0_item1")
Dim cnt1Panel_grp0_item1 As Autodesk.Revit.UI.TextBox = TryCast(panel.AddItem(cnt1Panel_grp0_item1Data), Autodesk.Revit.UI.TextBox)
cnt1Panel_grp0_item1.ToolTip = "How are you?"
cnt1Panel_grp0_item1.Value = "Hello"
cnt1Panel_grp0_item1.Image = BmpImageSource("Revit2012VBAddin.CheckAndUpdate.bmp")
cnt1Panel_grp0_item1.ShowImageAsButton = True
AddHandler cnt1Panel_grp0_item1.EnterPressed, AddressOf CallbackOfTextBoxEnterPressed
panel.AddSeparator()
Dim cnt1Panel_grp0_item3Data As New TextBoxData("cnt1Panel_grp0_item3")
Dim cnt1Panel_grp0_item3 As Autodesk.Revit.UI.TextBox = TryCast(panel.AddItem(cnt1Panel_grp0_item3Data), Autodesk.Revit.UI.TextBox)
cnt1Panel_grp0_item3.ToolTip = "How's it going?"
cnt1Panel_grp0_item3.Value = "Hey"
cnt1Panel_grp0_item3.Image = BmpImageSource("Revit2012VBAddin.good.bmp")
cnt1Panel_grp0_item3.ShowImageAsButton = True
AddHandler cnt1Panel_grp0_item3.EnterPressed, AddressOf CallbackOfTextBoxEnterPressed
End Sub
Public Shared Sub CallbackOfTextBoxEnterPressed(sender As Object, 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 TextBox creation is not hard to follow, creating a TextBoxData object first, assigning its constructor’s arguments, creating a TextBox through calling the AddItem method of the RibbonPanel and passing the created TextBoxData object to it, and finally setting other properties such as ToolTip and Value if necessary.
Creating a separator is nothing more than straightforward, calling the AddSeparator() method of the Ribbon Panel when timing is good. No arguments and no return value!
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.
Apart from these, keen readers may have noticed that the ShowImageAsButton property of the TextBox items are set as True and their EnterPressed events have been subscribed in the above code. Why is that?
Dosing so is to correct some inconsistent behavior of the TextBox UI elements if without the patches. Readers can try removing the code and see what will happen. The TextBox input will just be lost in some desert if the mouse cursor moves away from the edit field rather than the Enter key is pressed hard.
With the patches applied, when the associated icons are clicked the TextBox fields will be updated automatically, and that explains why the images look like check marks. The event handler here does not really do anything and leaving it empty is sufficient to do the trick. In this way, the TextBox items behave normal an up front and still look natural and professional.
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 two TextBox items and one Separator look the same.
The RevitAddinWizard is going to provide the VB.NET version of the Ribbon Creator soon.
Recent Comments