left-icon

Visual Studio Add-Ins Succinctly®
by Joseph D. Booth

Previous
Chapter

of
A
A
A

CHAPTER 18

Add-in Helper Class

Add-in Helper Class


As you begin to work with add-ins, you might find yourself writing your own library of helper routines. Here is a sample class library to get you started.

We begin by declaring a variable in the helper class to hold a reference to the DTE2 object (_applicationObject), so we do not have to pass it around as a parameter.

using System;

using EnvDTE80;

using Microsoft.VisualStudio.CommandBars;

using System.Windows.Forms;

public class AddInsHelper

{

    public DTE2 app { get; set; }

MakeEmptySolution

While the solution object allows you to create an empty solution, it has a couple of changes to be aware of. The directory will not be made if it does not exist, and you need to save the new solution file. To isolate these behaviors, we can create our own method call to make an empty solution.

public void MakeEmptySolution(string folder, string SolName)

{

    string FullFolder;

    // Close solution if open.

    if (app.Solution.IsOpen)

        {  app.Solution.Close(true);   }

    // Get or make the folder for the solution.

    FullFolder = System.IO.Path.Combine(GetVSProjectsFolder(), folder);

    if (!System.IO.Directory.Exists(FullFolder))

       {

          System.IO.Directory.CreateDirectory(FullFolder);  }

    string tempFile = System.IO.Path.Combine(FullFolder, "TempSolution.sln");

    app.Solution.Create(FullFolder, tempFile);

    tempFile = System.IO.Path.Combine(FullFolder, SolName);

    app.Solution.SaveAs(tempFile);

}

GetVSProjectsFolder

Another useful function is a wrapper to the get_properties method to return a path where Visual Studio stores new projects.

public string GetVSProjectsFolder()

{

  EnvDTE.Properties theProp = app.get_Properties("Environment", "ProjectsAndSolution");

  return theProp.Item("ProjectsLocation").Value.ToString();

    }

FindMenuIndex

The FindMenuIndex method finds the index of a particular menu item on one of the main menu’s menu bars. This allows you to control where to palce your add-in module if you don’t want it as the first or last item on the menu.

public int FindMenuIndex(string MainMenu, string subMenu)

{

   int res = 1;

   try

     {

       CommandBar menuBar = ((CommandBars)app.CommandBars)["MenuBar"];

       {

         foreach (CommandBarControl cb in menuBar.Controls)

         {

           if (MainMenu.ToString().ToUpper() == 

                cb.Caption.ToString().ToUpper().Replace("&", ""))

           {

            CommandBarPopup toolsPopup = (CommandBarPopup)cb;

            for (int xx = 1; xx <= toolsPopup.Controls.Count; xx++)

            {

              if (toolsPopup.Controls[xx].Caption.ToString().ToUpper().Replace("&", "")

                       == subMenu.ToUpper())

                 {

                    res = toolsPopup.Controls[xx].Index;

                    break;

                 }

            }

            break;

          }

        }

      }

    }

    catch

    {

    }

    return res;

 }

Hopefully a few of these functions will help you get started writing your own tools and help support your add-in development projects.

Scroll To Top
Disclaimer
DISCLAIMER: Web reader is currently in beta. Please report any issues through our support system. PDF and Kindle format files are also available for download.

Previous

Next



You are one step away from downloading ebooks from the Succinctly® series premier collection!
A confirmation has been sent to your email address. Please check and confirm your email subscription to complete the download.