A 'hello world' VB.NET program

From Ribbon Commander Documentation
Jump to: navigation, search

Creating a tab

  • Add a new class to your project and name it MyCustomUI.
  • Add the following code to your class
  1. Imports LogismiX.Interop.DynamicRibbonX
  2. Imports LogismiX.DynamicRibbonX.Core
  3.  
  4. Public Class MyCustomUI
  5.     Dim _customUI As rxCustomUI
  6.  
  7.     Public Sub New()
  8.         ' Instantiate a new rxCustomUI in context 'my_vb_context'
  9.         _customUI = rxCustomUI.create("my_vb_context", "My VB.NET context!")
  10.  
  11.         ' Cache a reference to the customUI's ribbon
  12.         Dim myRibbon As rxRibbon = _customUI.ribbon
  13.  
  14.         ' Create a new tab and label it 'My VB Tab'
  15.         Dim myTab As rxTab = New rxTab()
  16.         myTab.label = "My VB Tab"
  17.  
  18.         ' Add the tab to myRibbon's tabs
  19.         myRibbon.tabs.add(myTab)
  20.  
  21.         ' Render the UI
  22.         _customUI.refresh()
  23.  
  24.     End Sub
  25.  
  26. End Class
  • Modify class ThisAddin in ThisAddin.vb as follows
  1. Public Class ThisAddIn
  2.  
  3.     Private _myCustomUI As MyCustomUI
  4.  
  5.     Private Sub ThisAddIn_Startup() Handles Me.Startup
  6.         _myCustomUI = New MyCustomUI()
  7.     End Sub
  8.  
  9.     Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown
  10.  
  11.     End Sub
  12.  
  13. End Class
  • Run the add-in. If everything went according to plan a new tab labeled 'My VB Tab' will appear when the application starts
    MyFirstVBTab.png


Code Analysis

  1. ' Instantiate a new rxCustomUI in context 'my_vb_context'
  2. _customUI = rxCustomUI.create("my_vb_context", "My VB.NET context!")

rxCustomUI is at the top of the object model hierarchy. Here we instantiate a new rxCustomUI object in a new context with id 'my_vb_context' and description 'My VB.NET context!'


  1. ' Cache a reference to the customUI's ribbon
  2. Dim myRibbon As rxRibbon = _customUI.ribbon

Each rxCustomUI object owns a unique rxRibbon object. Here, we are caching a reference to the rxRibbon object of _customUI


  1. ' Create a new tab and label it 'My VB Tab'
  2. Dim myTab As rxTab = New rxTab()
  3. myTab.label = "My VB Tab"

Here, we create a new rxTab object and give it a label


  1. ' Add the tab to myRibbon's tabs
  2. myRibbon.tabs.add(myTab)

Each rxRibbon object has a collection of rxTab object (accessible through its tabs property). Here we add the tab we created above to our ribbon's tabs


  1. ' Render the UI
  2. _customUI.refresh()

To ensure optimal performance, UI updates always take place in two steps:

  1. Update the target rxCustomUI state, which we have done above
  2. Render the updated UI, which we are doing here

Remarks

  • rxCustomUI contexts are identified by their unique context ids (e.g. 'my_vb_context' above). When an rxCustomUI is created with a contextId that doesn't correspond to an existing context, the context gets created. Otherwise the rxCustomUI joins the context and shares its state with other rxCustomUIs in it.
  • You can use rxCustomUI.clear to clear a context's state

Making our code more compact

The Ribbon Commander API is composable, which we can take advantage to make our code more compact:

  1. Public Class MyCustomUI
  2.     Dim _customUI As rxCustomUI
  3.  
  4.     Public Sub New()
  5.         ' Instantiate a new rxCustomUI in context 'my_vb_context'
  6.         _customUI = rxCustomUI.create("my_vb_context", "My VB.NET context!")
  7.  
  8.         With _customUI
  9.             ' Clear old state in the context
  10.             .clear()
  11.  
  12.             ' Add a new tab and label it 
  13.             With .ribbon.tabs.add(New rxTab())
  14.                 .label = "My VB Tab"
  15.             End With
  16.  
  17.             ' Render the UI
  18.             .refresh()
  19.         End With
  20.     End Sub
  21.  
  22. End Class