Click here to Skip to main content
15,885,733 members
Articles / Programming Languages / C#

VS.NET Add-In for creating functions

Rate me:
Please Sign up or sign in to vote.
3.18/5 (8 votes)
7 Feb 2005CPOL2 min read 33.9K   664   28   1
Writing add-ins for VS.NET 2003.

Dialog on click of the command

Introduction

This article can be a template for writing your own add-ins for Visual Studio .NET 2003. I’ve explained here with an example which helps to copy a bunch of code lines into a new function. This add-in is built with a GUI where you need to input function name, access type and data type.

Functionality

This add-in contains a command “Add To Function”. This is made visible once you right click after selecting a bunch of lines together. This command will open a dialog where you need to enter the function name, access modifier and return type.

Menu text

On clicking the “Add To Function” link, the pop up dialog seen above will be opened.

Why Add-in?

It’s an amazing and powerful scheme that allows a plug-in developer have various degrees of power. It allows a developer to customize the VS.NET IDE according to the need, and helps in faster development.

How to create the add-in?

In VS.NET 2003, you can find a project template for Visual Studio .NET add-in. Select that and start creating your add-ins.

Dialog on click of the command

This will create an add-in project, and a setup project which helps to wrap up the application to the setup file. After installing the application in the machine, restart VS.NET to get the add-in commands in the IDE.

To reset the toolbars, you need to run DEVENV /SETUP from command prompt. This can be done by navigating to the “…\Program Files\Microsoft Visual Studio .NET 2003\Common7\IDE”, and typing DEVENV /SETUP from the command window.

Dialog on click of the command

My Add-in

Helps in creating the function and copying a set of code to the function. Sometimes, we may need to wrap a bunch of code lines to another function. This add-in helps in achieving that.

C#
try
{
    CommandBar commandBar = (CommandBar)commandBars["Code Window"];
    Command command=commands.AddNamedCommand(addInInstance,"AddToNew","Add To
                    Function", "",true,1554,ref contextGUIDS,
                    (int)vsCommandStatus.vsCommandStatusSupported+
                    (int)vsCommandStatus.vsCommandStatusEnabled)
    CommandBarControl commandBarControl = command.AddControl(commandBar, 1);
}
catch(Exception)
{
}

Piece of code when the command is invoked:

C#
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 == "FunctionsAddIn.Connect.AddToNew")
    {
        bool result = AddToNewFunc();
        handled = true;
        if(result)
        {
        TextDocument doc = 
          applicationObject.ActiveDocument.Object("TextDocument") 
          as Text Document;
        doc.Selection.SmartFormat();
        }
        return;
    }
  }
}

The AddToNewFunc takes care of formatting the text inside the class file.

Conclusion

Hope this article will guide you to create an add-in of your own interest/usage. You can download the source code here and mail back any queries to thotatri@gmail.com.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
"Variety is the spice of life", I always believe in this in whatever I do.

I started my IT career back in 99 as faculty. I love those moments, when I use to educate oracle for a group of account students. Perhaps the best moment in my IT life till day. Now currently doing my services for accenture, chennai facility.

I'm strong admirer of C# and C++.

I do blog at http://spaces.msn.com/members/thotatri

Comments and Discussions

 
GeneralThank you Pin
xibeifeijian3-Jan-07 21:54
xibeifeijian3-Jan-07 21:54 
Excellent example for beginners!Big Grin | :-D

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.