Walkthrough: Debugging an Add-in Project

This walkthrough illustrates how to create a simple Visual Studio add-in project and how to use breakpoints to debug the project. For more information, see Breakpoints and Tracepoints.

Add-ins are compiled applications that use the Visual Studio automation object model to manipulate or automate the integrated development environment (IDE). For more information, see Creating Add-ins and Wizards.

Note

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. These procedures were developed with the General Development Settings active. To change your settings, choose Import and ExportSettings on the Tools menu. For more information, see Visual Studio Settings.

To create a simple Visual Studio add-in project

  1. From the File menu, point to New, and than click Project.

    The New Project Dialog Box appears.

  2. In the Project Types pane, expand Other Project Types, and then select Extensibility.

  3. In the Templates pane, select Visual Studio Add-in.

  4. In the Name field, type SimpleAddIn as the name for your add-in project. Click OK.

    The Welcome to the Add-In Wizard, Visual Studio Add-In Wizard starts.

  5. In the Welcome to the Add-in Wizard page, click Next.

  6. In the Select a Programming Language page, click Create an Add-in using Visual C#, and then click Next.

  7. In the Select An Application Host page, accept the default options, and click Next.

  8. In the Enter a Name and Description page, type Simple Add-in as the name for your add-in, and type Used to illustrate how to debug a simple Add-in. as a description for your add-in. Click Next.

  9. On the Choose Add-in Options page, check the option, Yes, create a 'Tools' menu item. Accept the remaining defaults. Click Next.

  10. In the Choosing 'Help About' Information page, select the Yes, I would like my Add-in to offer 'About' box information. option, and then click Next.

  11. On the Summary page, click Finish.

    The Add-in Wizard generates your new add-in project and opens the IDE with focus on the Connect.cs file. This is the main class that contains the code for your add-in.

  12. In Solution Explorer, right-click the References node, and select Add Reference to add a reference to your assembly.

    This permits the use of types in the System.Windows.Forms namespace.

    The Add Reference Dialog Box appears.

  13. In the .NET tab, double-click the component, System.Windows.Forms.dll.

    In Solution Explorer, a reference to the System.Windows.Forms namespace is displayed under the References node.

    This namespace contains the code necessary to display a Message Box, which is used in the following code example.

  14. In Connect.cs, add the following statement near the top of the SimpleAddIn scope to permit easier use of a MessageBox object:

    using System.Windows.Forms;
    
  15. Add a MessageBox object to your add-in by addingMessageBox.Show("Debugging a Simple Add-in"); to the Exec method in Connect.cs:

    public void Exec(string commandName,
       vsCommandExecOption executeOption,
       ref object varIn, ref object varOut, ref bool handled)
    {
       handled = false;
       if(executeOption ==
          vsCommandExecOption.vsCommandExecOptionDoDefault)
       {
          if(commandName == "MyAddin1.Connect.MyAddin1")
          {
             handled = true;
             MessageBox.Show("Debugging a Simple Add-in");
             return;
          }
       }
    }
    
  16. Click the Save All button to save your work.

To debug your Visual Studio add-in project

  1. In Connect.cs, click in the left margin by the statement:

    MessageBox.Show("Debugging a Simple Add-in")
    

    A red dot (or, breakpoint) is displayed, and the text on this line is highlighted in red.

  2. From the Debug menu, click Start Debugging.

    Your current Visual Studio session — the debugger session — will lose focus and the program being debugged, will open in another instance of Visual Studio.

  3. From the Visual Studio IDE that is being debugged, click the Tools menu.

    SimpleAddIn is listed at the top of the menu items.

  4. Select the SimpleAddIn command to execute your add-in.

    This takes you to the line where you set your breakpoint in the Visual Studio debugger session, which is highlighted in yellow.

  5. From the Debug menu in the debugger session, select Step Into.

    The focus switches back to the debugged program. Your message box opens, indicating that your add-in has been executed.

  6. Click OK to close your message box.

  7. In the debugger session, select Continue from the Debug menu.

    The debugged Visual Studio session regains focus.

  8. Exit the debugged Visual Studio session.

  9. In Connect.cs, click the breakpoint that you set next to the MessageBox statement to remove it from your source code.

    As you can see, when you debug an add-in, another instance of the Visual Studio IDE opens to host the running add-in. The first instance displays its code and allows you to debug, such as setting watch variables, breakpoints, and so forth. When you are finished debugging, the second instance of the IDE closes and returns you to your add-in's code in the first instance.

See Also

Other Resources

Creating Add-ins and Wizards

Automation and Extensibility Reference