Hello World Operation

Top  Previous  Next

The Hello World Operation is a minimal operation that can be created with just the following steps:

  • Create a public class that inherits from OperationBase:
     
    [VB.NET]
    Public Class HelloWorldOperation
      Inherits OperationBase
      ...
    End Class
     
    [C#]
    public class HelloWorldOperation: OperationBase
    {
    ...
    }
     
  • Override the OnInitialize method. This method is called by the OperationBase class to retrieve information about the operation, such as the command name that will be created for the operation, its caption, its image, the supported .NET languages, the kinds of supported files, etc. To provide all this information, you need to fill the properties of the OperationInfo object that is passed as parameter to this method:
     
    [VB.NET]
    Protected Overrides Sub OnInitialize(ByVal operationInfo As OperationInfo)
     
      operationInfo.TargetNetLanguages = NetLanguage.CSharp Or NetLanguage.VisualBasic Or NetLanguage.JSharp
     
      operationInfo.TargetProjectItemKinds = ProjectItemKind.SourceCode
     
      operationInfo.TargetIDEs = IDE.VSNET Or IDE.Macros
     
      operationInfo.CommandName = "HelloWorldOperation"
     
      operationInfo.CommandImageIndex = 59 ' The office button image number
     
      operationInfo.CommandAvailability = CommandAvailability.DesignTime Or CommandAvailability.DebugTime
     
      ' You can honor the language of the add-in
      Select Case MyBase.UILanguage
     
         Case UILanguage.Spanish ' In this sample, we will honor only the Spanish language
            operationInfo.CommandCaption = "Operación Hola mundo..."
     
         Case Else ' Default to English language
            operationInfo.CommandCaption = "Hello World Operation..."
     
      End Select
     
      ' This operation does not use result window
      operationInfo.ShowResultsCommandName = ""
     
    End Sub
     
    [C#]
    protected override void OnInitialize(OperationInfo operationInfo)
    {
      operationInfo.TargetNetLanguages = NetLanguage.CSharp | NetLanguage.VisualBasic | NetLanguage.JSharp;
     
      operationInfo.TargetProjectItemKinds = ProjectItemKind.SourceCode;
     
      operationInfo.TargetIDEs = IDE.VSNET | IDE.Macros;
     
      operationInfo.CommandName = "HelloWorldOperation";
     
      operationInfo.CommandImageIndex = 59; // The office button image number
     
      operationInfo.CommandAvailability = CommandAvailability.DesignTime | CommandAvailability.DebugTime;
     
      // You can honor the language of the add-in
      switch (base.UILanguage)
      {
         case UILanguage.Spanish: // In this sample, we will honor only the Spanish language
            operationInfo.CommandCaption = "Operación Hola mundo...";
            break;
     
         default: // Default to English language
            operationInfo.CommandCaption = "Hello World Operation...";
            break;
      }
     
      //This operation does not use result window
      operationInfo.ShowResultsCommandName = "";
    }
     
  • Override the OnExecuteOperationInProjectItem method. This method is called by engine of the OperationBase class when it is the time to perform the actual operation on a file. The method receives through its parameters all the information needed to process the file, such as the admin options and the user options, the actual file (project item), the start and end edit points that define the scope of the operation (for text or source code operations) or the designer host (for designer operations):
     
    [VB.NET]
    Protected Overrides Sub OnExecuteOperationInProjectItem( _
      ByVal adminOptions As AdminOptionsBase, _
      ByVal userOptions As UserOptions, _
      ByVal projectItem As EnvDTE.ProjectItem, _
      ByVal startEditPoint As EnvDTE.EditPoint, _
      ByVal endEditPoint As EnvDTE.EditPoint, _
      ByVal designerHost As System.ComponentModel.Design.IDesignerHost, _
      ByRef cancel As Boolean)
     
      If MessageBox.Show("Now performing 'Hello World' operation on project item " & _
         projectItem.Name, "Hello World Operation", MessageBoxButtons.OKCancel, _
         MessageBoxIcon.Information) = DialogResult.Cancel Then
     
         cancel = True
     
      End If
     
    End Sub
     
    [C#]
    protected override void OnExecuteOperationInProjectItem(
      AdminOptionsBase adminOptions,
      UserOptions userOptions,
      EnvDTE.ProjectItem projectItem,
      EnvDTE.EditPoint startEditPoint,
      EnvDTE.EditPoint endEditPoint,
      System.ComponentModel.Design.IDesignerHost designerHost,
      ref bool cancel)
    {
      if (MessageBox.Show("Now performing 'Hello World' operation on project item "
         + projectItem.Name, "Hello World Operation", MessageBoxButtons.OKCancel,
         MessageBoxIcon.Information) == DialogResult.Cancel)
     
         cancel = true;
    }