Melody's VSTA Blog

Get the latest and greatest information on VSTA here! Find new samples, workarounds, and inside tips in our downloads and blogs. Get your questions answered quickly in our forums or search our site for a FAQ. We're here to help- just ask.

Events in VSTA Part II: Declaring Custom Events and EventArgs to Expose

To declare custom events to expose to VSTA, you will need to:

1)      Create a delegate outside of any class (VSTA does not support nested types).

2)      Explicitly declare the event as the delegate type from above (for more information on declaring events in VB see Part I- Declaring non-custom events to expose).

C# Sample:
public delegate void CustomEventHandler(object sender, CustomEventArgs e);

public class Application
{
   public event CustomEventHandler CustomEvent;
}

 

VB Sample:
Public Delegate Sub CustomEventHandler(ByVal sender As Object, ByVal e As CustomEventArgs)

Public Class Application
    Public Event CustomEvent As CustomEventHandler
End
Class

 

If you would like your custom event to use custom event args, then declare the CustomEventArgs class outside of any other class (can't be nested).  The CustomEventArgs class must only contain serializable types or they will not be able to be marshaled across the app domain.  Custom EventArgs should derive from System.EventArgs for good programming practice; however, System.EventArgs is a special case for ProxyGen.  ProxyGen removes the inheritance, so it will not show up in the descriptor or proxy file.  Below is an example of custom event args.

C# Sample:
public class CustomEventArgs : System.EventArgs
{
   public CustomEventArgs(object inObject)
   {
      myObject = inObject;
   } 

   private object myObject; 

   public object MyObject
   {
      get { return myObject; }
      set { myObject = value; }
   }
}

VB Sample:
Public Class CustomEventArgs
   Inherits System.EventArgs 

   Private mMyObject As Object 

   Public Sub New(ByVal inObject As Object)
      Me.mMyObject = inObject
   End Sub 

   Public ReadOnly Property MyObject()
      Get
        
Return Me.mMyObject
      End Get
  
End Property
End
Class

Published Wednesday, May 07, 2008 3:46 PM by Melody

Comments

  • No Comments
Anonymous comments are disabled