Difference between revisions of "A 'hello world' VB.NET program"
From Ribbon Commander Documentation
(→Code Analysis) |
(→Making our code more compact) |
||
(5 intermediate revisions by the same user not shown) | |||
Line 56: | Line 56: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
[[rxCustomUI]] is at the top of the object model hierarchy. Here we [[Method create|instantiate]] a new [[rxCustomUI]] object in a new context with id 'my_vb_context' and description 'My VB.NET context!' | [[rxCustomUI]] is at the top of the object model hierarchy. Here we [[Method create|instantiate]] a new [[rxCustomUI]] object in a new context with id 'my_vb_context' and description 'My VB.NET context!' | ||
+ | |||
+ | |||
+ | <syntaxhighlight lang="vb" line start="11"> | ||
+ | ' Cache a reference to the customUI's ribbon | ||
+ | Dim myRibbon As rxRibbon = _customUI.ribbon | ||
+ | </syntaxhighlight> | ||
+ | Each [[rxCustomUI]] object owns a unique [[rxRibbon]] object. Here, we are caching a reference to the [[rxRibbon]] object of ''_customUI'' | ||
+ | |||
+ | |||
+ | <syntaxhighlight lang="vb" line start="14"> | ||
+ | ' Create a new tab and label it 'My VB Tab' | ||
+ | Dim myTab As rxTab = New rxTab() | ||
+ | myTab.label = "My VB Tab" | ||
+ | </syntaxhighlight> | ||
+ | Here, we create a new [[rxTab]] object and give it a label | ||
+ | |||
+ | |||
+ | <syntaxhighlight lang="vb" line start="18"> | ||
+ | ' Add the tab to myRibbon's tabs | ||
+ | myRibbon.tabs.add(myTab) | ||
+ | </syntaxhighlight> | ||
+ | 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 | ||
+ | |||
+ | |||
+ | <syntaxhighlight lang="vb" line start="21"> | ||
+ | ' Render the UI | ||
+ | _customUI.refresh() | ||
+ | </syntaxhighlight> | ||
+ | To ensure optimal performance, UI updates always take place in two steps: | ||
+ | # Update the target [[rxCustomUI]] state, which we have done above | ||
+ | # 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 [[Method create|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 [[rxCustomUI]]s 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: | ||
+ | <syntaxhighlight lang="vb" line> | ||
+ | Public Class MyCustomUI | ||
+ | Dim _customUI As rxCustomUI | ||
+ | |||
+ | Public Sub New() | ||
+ | ' Instantiate a new rxCustomUI in context 'my_vb_context' | ||
+ | _customUI = rxCustomUI.create("my_vb_context", "My VB.NET context!") | ||
+ | |||
+ | With _customUI | ||
+ | ' Clear old state in the context | ||
+ | .clear() | ||
+ | |||
+ | ' Add a new tab and label it | ||
+ | With .ribbon.tabs.add(New rxTab()) | ||
+ | .label = "My VB Tab" | ||
+ | End With | ||
+ | |||
+ | ' Render the UI | ||
+ | .refresh() | ||
+ | End With | ||
+ | End Sub | ||
+ | |||
+ | End Class | ||
+ | </syntaxhighlight> |
Latest revision as of 20:52, 19 October 2013
Creating a tab
- Add a new class to your project and name it MyCustomUI.
- Add the following code to your class
Imports LogismiX.Interop.DynamicRibbonX
Imports LogismiX.DynamicRibbonX.Core
Public Class MyCustomUI
Dim _customUI As rxCustomUI
Public Sub New()
' Instantiate a new rxCustomUI in context 'my_vb_context'
_customUI = rxCustomUI.create("my_vb_context", "My VB.NET context!")
' Cache a reference to the customUI's ribbon
Dim myRibbon As rxRibbon = _customUI.ribbon
' Create a new tab and label it 'My VB Tab'
Dim myTab As rxTab = New rxTab()
myTab.label = "My VB Tab"
' Add the tab to myRibbon's tabs
myRibbon.tabs.add(myTab)
' Render the UI
_customUI.refresh()
End Sub
End Class
- Modify class ThisAddin in ThisAddin.vb as follows
Public Class ThisAddIn
Private _myCustomUI As MyCustomUI
Private Sub ThisAddIn_Startup() Handles Me.Startup
_myCustomUI = New MyCustomUI()
End Sub
Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown
End Sub
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
Code Analysis
' Instantiate a new rxCustomUI in context 'my_vb_context'
_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!'
' Cache a reference to the customUI's ribbon
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
' Create a new tab and label it 'My VB Tab'
Dim myTab As rxTab = New rxTab()
myTab.label = "My VB Tab"
Here, we create a new rxTab object and give it a label
' Add the tab to myRibbon's tabs
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
' Render the UI
_customUI.refresh()
To ensure optimal performance, UI updates always take place in two steps:
- Update the target rxCustomUI state, which we have done above
- 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:
Public Class MyCustomUI
Dim _customUI As rxCustomUI
Public Sub New()
' Instantiate a new rxCustomUI in context 'my_vb_context'
_customUI = rxCustomUI.create("my_vb_context", "My VB.NET context!")
With _customUI
' Clear old state in the context
.clear()
' Add a new tab and label it
With .ribbon.tabs.add(New rxTab())
.label = "My VB Tab"
End With
' Render the UI
.refresh()
End With
End Sub
End Class