Difference between revisions of "Using events in VB.NET"

From Ribbon Commander Documentation
Jump to: navigation, search
(Background)
(Background)
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
== Background ==
 
== Background ==
You can subscribe to events of [[controls|Dynamic RibbonX controls]] by using .NET events or .NET delegates. Here, we show how it can be done by using .NET events.
+
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 [[temp|your project]] and name it ''ButtonEventsUI''
+
* 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

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
  1. Public Class ButtonEventsUI
  2.     Private _customUI As rxCustomUI
  3.     Private WithEvents _myButton As rxButton
  4.  
  5.     Public Sub New()
  6.         ' Instantiate a new rxCustomUI in context 'my_vb_context'
  7.         _customUI = rxCustomUI.create("my_vb_context", "My VB.NET context!")
  8.  
  9.         With _customUI
  10.             ' Clear old state in the context
  11.             .clear()
  12.  
  13.             ' Add a new tab and label it 
  14.             With .ribbon.tabs.add(New rxTab())
  15.                 .label = "My VB Tab"
  16.  
  17.                 ' Add a new group and label it
  18.                 With .groups.add(New rxGroup())
  19.                     .label = "My VB Group"
  20.  
  21.                     'Add a new button an label it
  22.                     _myButton = .buttons.add(New rxButton)
  23.                 End With
  24.             End With
  25.  
  26.             ' Render the UI
  27.             .refresh()
  28.         End With
  29.  
  30.     End Sub
  31.  
  32.     Private Sub _myButton_OnActionEvent(control As LogismiX.Interop.DynamicRibbonX.IRibbonControl) Handles _myButton.OnActionEvent
  33.         System.Diagnostics.Debug.Print("_myButton_OnActionEvent clicked")
  34.         MessageBox.Show("Hello from VB.NET")
  35.     End Sub
  36.  
  37.     Private Sub _myButton_OnGetDescription(control As LogismiX.Interop.DynamicRibbonX.IRibbonControl, ByRef description As String) Handles _myButton.OnGetDescription
  38.         System.Diagnostics.Debug.Print("_myButton_OnGetDescription clicked")
  39.         description = "My button description"
  40.     End Sub
  41.  
  42.     Private Sub _myButton_OnGetEnabled(control As LogismiX.Interop.DynamicRibbonX.IRibbonControl, ByRef enabled As Boolean) Handles _myButton.OnGetEnabled
  43.         System.Diagnostics.Debug.Print("_myButton_OnGetEnabled clicked")
  44.         enabled = True
  45.     End Sub
  46.  
  47.     Private Sub _myButton_OnGetImage(control As LogismiX.Interop.DynamicRibbonX.IRibbonControl, ByRef imageDispOrImageMso As Object) Handles _myButton.OnGetImage
  48.         System.Diagnostics.Debug.Print("_myButton_OnGetImage clicked")
  49.         imageDispOrImageMso = "AccessTableAssets"
  50.     End Sub
  51.  
  52.     Private Sub _myButton_OnGetKeytip(control As LogismiX.Interop.DynamicRibbonX.IRibbonControl, ByRef keytip As String) Handles _myButton.OnGetKeytip
  53.         System.Diagnostics.Debug.Print("_myButton_OnGetKeytip clicked")
  54.         keytip = "K"
  55.     End Sub
  56.  
  57.     Private Sub _myButton_OnGetLabel(control As LogismiX.Interop.DynamicRibbonX.IRibbonControl, ByRef label As String) Handles _myButton.OnGetLabel
  58.         System.Diagnostics.Debug.Print("_myButton_OnGetLabel clicked")
  59.         label = "My VB Button"
  60.     End Sub
  61.  
  62.     Private Sub _myButton_OnGetScreentip(control As LogismiX.Interop.DynamicRibbonX.IRibbonControl, ByRef screentip As String) Handles _myButton.OnGetScreentip
  63.         System.Diagnostics.Debug.Print("_myButton_OnGetScreentip clicked")
  64.         screentip = "My button screentip"
  65.     End Sub
  66.  
  67.     Private Sub _myButton_OnGetShowImage(control As LogismiX.Interop.DynamicRibbonX.IRibbonControl, ByRef showImage As Boolean) Handles _myButton.OnGetShowImage
  68.         System.Diagnostics.Debug.Print("_myButton_OnGetShowImage clicked")
  69.         showImage = True
  70.     End Sub
  71.  
  72.     Private Sub _myButton_OnGetShowLabel(control As LogismiX.Interop.DynamicRibbonX.IRibbonControl, ByRef showLabel As Boolean) Handles _myButton.OnGetShowLabel
  73.         System.Diagnostics.Debug.Print("_myButton_OnGetShowLabel clicked")
  74.         showLabel = True
  75.     End Sub
  76.  
  77.     Private Sub _myButton_OnGetSize(control As LogismiX.Interop.DynamicRibbonX.IRibbonControl, ByRef size As LogismiX.Interop.DynamicRibbonX.RibbonControlSize) Handles _myButton.OnGetSize
  78.         System.Diagnostics.Debug.Print("_myButton_OnGetSize clicked")
  79.         size = RibbonControlSize.RibbonControlSizeRegular
  80.     End Sub
  81.  
  82.     Private Sub _myButton_OnGetSupertip(control As LogismiX.Interop.DynamicRibbonX.IRibbonControl, ByRef supertip As String) Handles _myButton.OnGetSupertip
  83.         System.Diagnostics.Debug.Print("_myButton_OnGetSupertip clicked")
  84.         supertip = "My button supertip"
  85.     End Sub
  86.  
  87.     Private Sub _myButton_OnGetVisible(control As LogismiX.Interop.DynamicRibbonX.IRibbonControl, ByRef visible As Boolean) Handles _myButton.OnGetVisible
  88.         System.Diagnostics.Debug.Print("_myButton_OnGetVisible clicked")
  89.         visible = True
  90.     End Sub
  91. End Class
  • Modify class ThisAddin in ThisAddin.vb as follows
  1. Public Class ThisAddIn
  2.  
  3.     Private _myCustomUI As ButtonEventsUI
  4.  
  5.     Private Sub ThisAddIn_Startup() Handles Me.Startup
  6.         _myCustomUI = New ButtonEventsUI()
  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, the following UI will be created:
    UsingEventsInVBButton.png

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)
    InsertingEventStubVB.png

Remarks