Using Multiple Languages in the Add-in

 < Free Open Study > 



Creating a C# Add-in

In this section you are going to create an add-in in C#. Up to this point in the book, you have built all of the add-ins using Visual Basic as the base language. Anything that can be done in Visual Basic can also be done in C#. To create a C# add-in, you will use the Add-in Wizard. You will select the exact same options you would select for a Visual Basic add-in, except for the base language option: You will select C# as the base language for the add-in rather than Visual Basic. Once the wizard has created the basic add-in, the generated code will appear as shown in Listing 10-3.

Listing 10-3: C# Add-in Code Created by the Add-in Wizard

start example
 namespace Chap10AddinCS { using System; using Microsoft.Office.Core; using Extensibility; using System.Runtime.InteropServices; using EnvDTE; /// <summary> ///    The object for implementing an Add-in. /// </summary> /// <seealso class='IDTExtensibility2' /> [GuidAttribute("8133C305-7BB5-4BC0-9FBD-172BE4A40BB3"),       ProgId("Chap10AddinCS.Connect")] public class Connect : Object, Extensibility.IDTExtensibility2,      IDTCommandTarget {    Command command; /// <summary> /// Implements the constructor for the Add-in object. /// Place your initialization code within this method. /// </summary> public Connect() { } /// <summary> ///          Implements the OnConnection method of the I ///          DTExtensibility2 interface. ///          Receives notification that the Add-in is being loaded. /// </summary> /// <param term='application'> ///     Root object of the host application. /// </param> /// <param term='connectMode'> ///     Describes how the Add-in is being loaded. /// </param> /// <param tv.erm='addInInst'> ///     Object representing this Add-in. /// </param> /// <seealso class='IDTExtensibility2' /> public void OnConnection(object application,     Extensibility.ext_ConnectMode connectMode,     object addInInst, ref System.Array custom) {    applicationObject = (_DTE)application;    addInInstance = (AddIn)addInInst;    if(connectMode ==       Extensibility.ext_ConnectMode.ext_cm_UISetup ||          connectMode ==          Extensibility.ext_ConnectMode.ext_cm_AfterStartup)    {       object []contextGUIDS = new object[] { };       Commands commands = applicationObject.Commands;       _CommandBars commandBars =          applicationObject.CommandBars;    try    {       Command command =          commands.AddNamedCommand(addInInstance,                        "Chap10AddinCS", "Chap10AddinCS",                        "Executes the command for Chap10AddinCS", true,                        59, ref contextGUIDS,                       (int)vsCommandStatus.vsCommandStatusSupported+                        (int)vsCommandStatus.vsCommandStatusEnabled);                    CommandBar commandBar =                        (CommandBar)commandBars["Tools"];                    CommandBarControl commandBarControl =                           command.AddControl(commandBar, 1);                }                catch(System.Exception e)                {                   MessageBox.Show("Error setting command " +                      e.Message);                }             }          }          public void OnDisconnection(Extensibility.ext_DisconnectMode             disconnectMode, ref System.Array custom)          {             try             {                command.Delete();             }             catch(System.Exception e)             {                MessageBox.Show("Error removing command " +                   e.Message);             }             {             public void OnStartupComplete(ref System.Array custom)          {          }          public void OnBeginShutdown(ref System.Array custom)          {          }          public void OnAddInsUpdate(ref System.Array custom)          }          }    public void QueryStatus(string commandName,       EnvDTE.vsCommandStatusTextWanted neededText,       ref EnvDTE.vsCommandStatus status,       ref object commandText)    {       if(neededText ==           EnvDTE.vsCommandStatusTextWanted.           vsCommandStatusTextWantedNone)       {          if(commandName ==              "Chap10AddinCS.Connect.Chap10AddinCS")          {             status =                 (vsCommandStatus)vsCommandStatus.                 vsCommandStatusSupported|222                vsCommandStatus.                 vsCommandStatusEnabled;          }       }    }    public void Exec(string commandName,         EnvDTE.vsCommandExecOption executeOption,         ref object varIn,         ref object varOut,         ref bool handled)    {       handled = false;       if(executeOption ==           EnvDTE.vsCommandExecOption.vsCommandExecOptionDoDefault)       {          if(commandName ==              "Chap10AddinCS.Connect.Chap10AddinCS")          {             MessageBox.Show ("You Rang?");             handled = true;             return;          {          }    }    private _DTE applicationObject;    private AddIn addInInstance; } } 
end example

The code generated by the wizard is basically the same as the code you have seen generated for a Visual Basic add-in, except for the basic syntax differences ({} ;). However, C# does something neat that Visual Basic does not do.

Visual Studio .NET provides a special feature in C# that allows you to document your C# code using XML. You will notice that there are some new lines of comments. However, these special comment lines have three slashes instead of two, which is the normal C/C++/C# comment delimiter. I have highlighted the first few sets of these comments in boldface. This special comment is eXtensible Markup Language (XML) documentation of the functions. You will notice the XML keyword <summary> followed by the comment and terminated by </summary>. At some time in the future, you can extract the XML comments and place them into an XML file.

Note 

Although the compiler initially placed these XML comments in front of every function, I have removed most of them from the code for the add-in in order to save space.You can still see the code as originally created by downloading the book's code from the Downloads section of the Apress Web site ( http://www.apress.com) and looking at the code for this section.

The C# text editor will automatically produce these comments for you as you code a new function. After you enter a new function, position the cursor to a blank line just above the function definition line. Enter three slashes (///) and press Enter. The editor will automatically enter the XML comments documenting the function and any parameters that you may have entered in the function definition line.

I have made a few simple changes to the code for the add-in in Listing 10-3 that I normally make to the code generated by the Add-in Wizard. I have highlighted each of the changes and summarized them in the following list:

  1. I added a module-level command object so that it would be in scope for the OnDisconnection method.

  2. I added another condition to the test for connect mode to the OnConnection method.

  3. I added a message box display to report any error encountered while attempting to load the menu in the OnConnection method.

  4. I added code to remove the add-in menu in the OnDisconnection method. I also added error-handling code in case an error is encountered when removing the menu.



 < Free Open Study > 



Writing Add-Ins for Visual Studio  .NET
Writing Add-Ins for Visual Studio .NET
ISBN: 1590590260
EAN: 2147483647
Year: 2002
Pages: 172
Authors: Les Smith

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net