Dave Donaldson
Critical thinking in software development
Search
Advertisement
Subscribe
Follow Me
My Tweets
- Just went flying off the road. No lie. More soon.
- Planning to watch the OSU-MichSt in the bar, if anyone wants to join, but will be late
- Geeking out with @fallenrogue and @danhounshell on way to #codemash
- Watching Spongebob while I wait for @fallenrogue and @danhounshell.
- Re-syncing my entire 30GB Zune.
- Follow Me on Twitter
Add-In Tips
Sunday, November 21 2004
As I mentioned in my last post, writing an add-in is “fun”. It's just a different animal. I mean, you ARE extending Visual Studio itself, so you kind of expect some challenges. But I do think it could be easier.
Here's a couple tips I gathered while writing the CreateProperty add-in:
1. Language Doesn't Matter
Unlike creating macros where VB is your only language choice, you can create Visual Studio add-ins with VB, C#, or C++. I tried creating the add-in with VB and C#, and both were just as painful. Neither one has an advantage over the other for this type of development.
2. Change the Default Add-In Name on the Tools Menu
The key to an add-in is its Connect class. The Connect class has a method named OnConnection which receives notification that the add-in is being loaded. Here is where you add your add-in to any menus or command windows. When you first create your add-in project and look at the OnConnection method, VS automatically generates code to add your add-in to the Tools menu. However, by default, it uses the name of your add-in project as the name to display in the Tools menu (the line of code looks like this: Command command = commands.AddNamedCommand(addInInstance, "CreatePropertyAddIn", "CreatePropertyAddIn",...). I just wanted the name “CreateProperty” to show on the Tools menu, so I changed each “CreatePropertyAddIn” string to “CreateProperty”.
3. Add the Add-In to Right-Click Menu
Since my add-in generates code in the IDE text editor, I figured it would be useful to have the add-in appear in the right-click menu. To do this, go to the OnConnection method in the Connect class and add these two lines in the try{} block:
CommandBar commandBar2 = (CommandBar)commandBars["Code Window"];
CommandBarControl commandBarControl2 = command.AddControl(commandBar2, 1);
4. Determine Current Language with TextDocument.Language
In the CreateProperty add-in, I had to determine at runtime which language was being used so that I could generate the appropriate property code (sorry, no C++; only VB and C#). This gave me some serious fits for which I'll share in more detail later, but in the end I figured out what to use and more importantly, the values it returned for each language. The EnvDTE object has a class named TextDocument. The TextDocument class has a property named Language (simple enough it seems) that you can use to grab which language the IDE text document is in. In my opinion, the APIs for EnvDTE, TextDocument, and Language are screwed up, but what I found out what was that for C#, the Language property returns “CSharp” and for VB it returns “Basic”. Again, I'll post more on this issue soon.
5. Cheat Where Appropriate
One of the big problems I ran into was how to get a string that was created in the add-in form into the open text document in the IDE. I thought maybe I could find an example somewhere for how to do this, but nothing turned up, so I was forced to figure it out on my own (damn). Anyway, after experimenting with all sorts of methods to get that to work, I hacked my way through it, and it turns out it's the simplest and easiest solution (isn't it always?). I created a hidden public label on the add-in form and populated it. Then I set the EnvDTE.TextDocument.Selection.Text equal to the text of that public hidden label. Viola: problem solved. However, although this cheat gets the job done, if someone knows of a more “preferred” way of accomplishing this, please let me know.
