Difference between revisions of "Using events in VB.NET"
From Ribbon Commander Documentation
(→Background) |
(→Background) |
||
(5 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
== Background == | == Background == | ||
− | You can subscribe to events of [[controls| | + | You can subscribe to events of [[controls|Ribbon Commander controls]] by using .NET events or .NET delegates. Here, we show how it can be done by using .NET events. |
In general you need to take the following steps: | In general you need to take the following steps: | ||
* Declare a member variable of the events source control type ''WithEvents'' | * Declare a member variable of the events source control type ''WithEvents'' | ||
Line 7: | Line 7: | ||
== Example == | == Example == | ||
In this example we will set up a UI that contains an rxButton control and subscribe to all its events. | In this example we will set up a UI that contains an rxButton control and subscribe to all its events. | ||
− | * Add a new class to [[ | + | * Add a new class to [[Creating a new project in VB.NET|your project]] and name it ''ButtonEventsUI'' |
* Add the following code to your class | * Add the following code to your class | ||
<syntaxhighlight lang="vb" line> | <syntaxhighlight lang="vb" line> | ||
+ | Public Class ButtonEventsUI | ||
+ | Private _customUI As rxCustomUI | ||
+ | Private WithEvents _myButton As rxButton | ||
+ | |||
+ | 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" | ||
+ | |||
+ | ' Add a new group and label it | ||
+ | With .groups.add(New rxGroup()) | ||
+ | .label = "My VB Group" | ||
+ | |||
+ | 'Add a new button an label it | ||
+ | _myButton = .buttons.add(New rxButton) | ||
+ | End With | ||
+ | End With | ||
+ | |||
+ | ' Render the UI | ||
+ | .refresh() | ||
+ | End With | ||
+ | |||
+ | End Sub | ||
+ | |||
+ | Private Sub _myButton_OnActionEvent(control As LogismiX.Interop.DynamicRibbonX.IRibbonControl) Handles _myButton.OnActionEvent | ||
+ | System.Diagnostics.Debug.Print("_myButton_OnActionEvent clicked") | ||
+ | MessageBox.Show("Hello from VB.NET") | ||
+ | End Sub | ||
+ | |||
+ | Private Sub _myButton_OnGetDescription(control As LogismiX.Interop.DynamicRibbonX.IRibbonControl, ByRef description As String) Handles _myButton.OnGetDescription | ||
+ | System.Diagnostics.Debug.Print("_myButton_OnGetDescription clicked") | ||
+ | description = "My button description" | ||
+ | End Sub | ||
+ | |||
+ | Private Sub _myButton_OnGetEnabled(control As LogismiX.Interop.DynamicRibbonX.IRibbonControl, ByRef enabled As Boolean) Handles _myButton.OnGetEnabled | ||
+ | System.Diagnostics.Debug.Print("_myButton_OnGetEnabled clicked") | ||
+ | enabled = True | ||
+ | End Sub | ||
+ | |||
+ | Private Sub _myButton_OnGetImage(control As LogismiX.Interop.DynamicRibbonX.IRibbonControl, ByRef imageDispOrImageMso As Object) Handles _myButton.OnGetImage | ||
+ | System.Diagnostics.Debug.Print("_myButton_OnGetImage clicked") | ||
+ | imageDispOrImageMso = "AccessTableAssets" | ||
+ | End Sub | ||
+ | |||
+ | Private Sub _myButton_OnGetKeytip(control As LogismiX.Interop.DynamicRibbonX.IRibbonControl, ByRef keytip As String) Handles _myButton.OnGetKeytip | ||
+ | System.Diagnostics.Debug.Print("_myButton_OnGetKeytip clicked") | ||
+ | keytip = "K" | ||
+ | End Sub | ||
+ | |||
+ | Private Sub _myButton_OnGetLabel(control As LogismiX.Interop.DynamicRibbonX.IRibbonControl, ByRef label As String) Handles _myButton.OnGetLabel | ||
+ | System.Diagnostics.Debug.Print("_myButton_OnGetLabel clicked") | ||
+ | label = "My VB Button" | ||
+ | End Sub | ||
+ | |||
+ | Private Sub _myButton_OnGetScreentip(control As LogismiX.Interop.DynamicRibbonX.IRibbonControl, ByRef screentip As String) Handles _myButton.OnGetScreentip | ||
+ | System.Diagnostics.Debug.Print("_myButton_OnGetScreentip clicked") | ||
+ | screentip = "My button screentip" | ||
+ | End Sub | ||
+ | |||
+ | Private Sub _myButton_OnGetShowImage(control As LogismiX.Interop.DynamicRibbonX.IRibbonControl, ByRef showImage As Boolean) Handles _myButton.OnGetShowImage | ||
+ | System.Diagnostics.Debug.Print("_myButton_OnGetShowImage clicked") | ||
+ | showImage = True | ||
+ | End Sub | ||
+ | |||
+ | Private Sub _myButton_OnGetShowLabel(control As LogismiX.Interop.DynamicRibbonX.IRibbonControl, ByRef showLabel As Boolean) Handles _myButton.OnGetShowLabel | ||
+ | System.Diagnostics.Debug.Print("_myButton_OnGetShowLabel clicked") | ||
+ | showLabel = True | ||
+ | End Sub | ||
+ | |||
+ | Private Sub _myButton_OnGetSize(control As LogismiX.Interop.DynamicRibbonX.IRibbonControl, ByRef size As LogismiX.Interop.DynamicRibbonX.RibbonControlSize) Handles _myButton.OnGetSize | ||
+ | System.Diagnostics.Debug.Print("_myButton_OnGetSize clicked") | ||
+ | size = RibbonControlSize.RibbonControlSizeRegular | ||
+ | End Sub | ||
+ | |||
+ | Private Sub _myButton_OnGetSupertip(control As LogismiX.Interop.DynamicRibbonX.IRibbonControl, ByRef supertip As String) Handles _myButton.OnGetSupertip | ||
+ | System.Diagnostics.Debug.Print("_myButton_OnGetSupertip clicked") | ||
+ | supertip = "My button supertip" | ||
+ | End Sub | ||
+ | |||
+ | Private Sub _myButton_OnGetVisible(control As LogismiX.Interop.DynamicRibbonX.IRibbonControl, ByRef visible As Boolean) Handles _myButton.OnGetVisible | ||
+ | System.Diagnostics.Debug.Print("_myButton_OnGetVisible clicked") | ||
+ | visible = True | ||
+ | End Sub | ||
+ | End Class | ||
</syntaxhighlight> | </syntaxhighlight> | ||
* Modify class ThisAddin in ThisAddin.vb as follows | * Modify class ThisAddin in ThisAddin.vb as follows | ||
<syntaxhighlight lang="vb" line> | <syntaxhighlight lang="vb" line> | ||
+ | Public Class ThisAddIn | ||
+ | |||
+ | Private _myCustomUI As ButtonEventsUI | ||
+ | |||
+ | Private Sub ThisAddIn_Startup() Handles Me.Startup | ||
+ | _myCustomUI = New ButtonEventsUI() | ||
+ | End Sub | ||
+ | |||
+ | Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown | ||
+ | |||
+ | End Sub | ||
+ | |||
+ | End Class | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | * Run the add-in. If everything went according to plan, the following UI will be created: | ||
+ | *: [[image:UsingEventsInVBButton.png|link=]] | ||
+ | |||
+ | == How to use Visual Studio to insert event stubs automatically == | ||
+ | * Select the events source variable in the left list-box at the top of the code window ((1) below) | ||
+ | * Select the desired event in the right list-box at the top of the code window ((2) below) | ||
+ | *: [[image:InsertingEventStubVB.png|link=]] | ||
== Remarks == | == Remarks == |
Latest revision as of 20:53, 19 October 2013
Contents
Background
You can subscribe to events of Ribbon Commander controls by using .NET events or .NET delegates. Here, we show how it can be done by using .NET events. In general you need to take the following steps:
- Declare a member variable of the events source control type WithEvents
- Use Visual Studio to insert the desired event stubs
Example
In this example we will set up a UI that contains an rxButton control and subscribe to all its events.
- Add a new class to your project and name it ButtonEventsUI
- Add the following code to your class
Public Class ButtonEventsUI
Private _customUI As rxCustomUI
Private WithEvents _myButton As rxButton
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"
' Add a new group and label it
With .groups.add(New rxGroup())
.label = "My VB Group"
'Add a new button an label it
_myButton = .buttons.add(New rxButton)
End With
End With
' Render the UI
.refresh()
End With
End Sub
Private Sub _myButton_OnActionEvent(control As LogismiX.Interop.DynamicRibbonX.IRibbonControl) Handles _myButton.OnActionEvent
System.Diagnostics.Debug.Print("_myButton_OnActionEvent clicked")
MessageBox.Show("Hello from VB.NET")
End Sub
Private Sub _myButton_OnGetDescription(control As LogismiX.Interop.DynamicRibbonX.IRibbonControl, ByRef description As String) Handles _myButton.OnGetDescription
System.Diagnostics.Debug.Print("_myButton_OnGetDescription clicked")
description = "My button description"
End Sub
Private Sub _myButton_OnGetEnabled(control As LogismiX.Interop.DynamicRibbonX.IRibbonControl, ByRef enabled As Boolean) Handles _myButton.OnGetEnabled
System.Diagnostics.Debug.Print("_myButton_OnGetEnabled clicked")
enabled = True
End Sub
Private Sub _myButton_OnGetImage(control As LogismiX.Interop.DynamicRibbonX.IRibbonControl, ByRef imageDispOrImageMso As Object) Handles _myButton.OnGetImage
System.Diagnostics.Debug.Print("_myButton_OnGetImage clicked")
imageDispOrImageMso = "AccessTableAssets"
End Sub
Private Sub _myButton_OnGetKeytip(control As LogismiX.Interop.DynamicRibbonX.IRibbonControl, ByRef keytip As String) Handles _myButton.OnGetKeytip
System.Diagnostics.Debug.Print("_myButton_OnGetKeytip clicked")
keytip = "K"
End Sub
Private Sub _myButton_OnGetLabel(control As LogismiX.Interop.DynamicRibbonX.IRibbonControl, ByRef label As String) Handles _myButton.OnGetLabel
System.Diagnostics.Debug.Print("_myButton_OnGetLabel clicked")
label = "My VB Button"
End Sub
Private Sub _myButton_OnGetScreentip(control As LogismiX.Interop.DynamicRibbonX.IRibbonControl, ByRef screentip As String) Handles _myButton.OnGetScreentip
System.Diagnostics.Debug.Print("_myButton_OnGetScreentip clicked")
screentip = "My button screentip"
End Sub
Private Sub _myButton_OnGetShowImage(control As LogismiX.Interop.DynamicRibbonX.IRibbonControl, ByRef showImage As Boolean) Handles _myButton.OnGetShowImage
System.Diagnostics.Debug.Print("_myButton_OnGetShowImage clicked")
showImage = True
End Sub
Private Sub _myButton_OnGetShowLabel(control As LogismiX.Interop.DynamicRibbonX.IRibbonControl, ByRef showLabel As Boolean) Handles _myButton.OnGetShowLabel
System.Diagnostics.Debug.Print("_myButton_OnGetShowLabel clicked")
showLabel = True
End Sub
Private Sub _myButton_OnGetSize(control As LogismiX.Interop.DynamicRibbonX.IRibbonControl, ByRef size As LogismiX.Interop.DynamicRibbonX.RibbonControlSize) Handles _myButton.OnGetSize
System.Diagnostics.Debug.Print("_myButton_OnGetSize clicked")
size = RibbonControlSize.RibbonControlSizeRegular
End Sub
Private Sub _myButton_OnGetSupertip(control As LogismiX.Interop.DynamicRibbonX.IRibbonControl, ByRef supertip As String) Handles _myButton.OnGetSupertip
System.Diagnostics.Debug.Print("_myButton_OnGetSupertip clicked")
supertip = "My button supertip"
End Sub
Private Sub _myButton_OnGetVisible(control As LogismiX.Interop.DynamicRibbonX.IRibbonControl, ByRef visible As Boolean) Handles _myButton.OnGetVisible
System.Diagnostics.Debug.Print("_myButton_OnGetVisible clicked")
visible = True
End Sub
End Class
- Modify class ThisAddin in ThisAddin.vb as follows
Public Class ThisAddIn
Private _myCustomUI As ButtonEventsUI
Private Sub ThisAddIn_Startup() Handles Me.Startup
_myCustomUI = New ButtonEventsUI()
End Sub
Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown
End Sub
End Class
- Run the add-in. If everything went according to plan, the following UI will be created:
How to use Visual Studio to insert event stubs automatically
- Select the events source variable in the left list-box at the top of the code window ((1) below)
- Select the desired event in the right list-box at the top of the code window ((2) below)