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.
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.
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.
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.
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:
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.