Visual Studio Search Toolbar

by Alan Dean

Subscribe to feeds:  Atom feed for Alan Dean Atom  | RSS feed for Alan Dean RSS  | RDF feed for Alan Dean RDF

 

Preamble

Using F1 to access the MSDN Library is all well and good, but there are times when you really want to use an internet search engine, so here is one way to do it.

Visual Studio Macro

The first step is to write the macro code in the IDE. To do this, open the Macro Editor:

Open the Macro Editor

Visual Studio macros are written in VB.NET, so you need to create a new module.

Imports EnvDTE
Imports System.Diagnostics
Imports System.Windows.Forms
Imports System.Globalization
Imports System.Web

Public Module Search

  Public Sub Google()
    NavigateTo("http://www.google.com/search?q={0}")
  End Sub
  
  Private Sub NavigateTo(ByVal url As String)
    Dim selection As String = DTE.ActiveDocument.Selection.Text
    If selection.Length > 0 Then
      DTE.ItemOperations.Navigate(String.Format(CultureInfo.InvariantCulture, url, HttpUtility.UrlPathEncode(selection)), vsNavigateOptions.vsNavigateOptionsNewWindow)
    Else
      MessageBox.Show("No search text selected.")
    End If
  End Sub
  
End Module

Update: Tweaked the code to encode the selection text (thanks rippleyong)

Create the toolbar

The next step is to create a new toolbar. Close the Macro Editor and bring up the Customize dialog (either from the toolbar context menu or Tools >> Customize). Create a new toolbar (I called mine "Search") on the Toolbars tab. To add new buttons to the newly created Search toolbar, select the Commands tab then select the Macros Category and scroll to the newly created command:

Open the Macro Editor

Simple drag this onto the Search toolbar and the toolbar button is created:

New toolbar

The new button can now be edited. Note that you must be in customize mode to do this. The first step is to change the name from the Macro name as this will become the tooltip text. I changed mine to "Google":

Edit name

The next step is to set up the icon. The easy way to do this is to simply select from one of the standard icons:

Select standard icon

Personally, I prefer to use custom icons. There is a very simple hack to do this for an internet search engine. Just navigate to the site favicon (for Google it is http://www.google.com/favicon.ico). Then just copy the icon:

Google Favicon

To apply to copied icon, just paste it:

Paste custom icon

The toolbar now looks like this:

Paste custom icon

The last step is to select Default Style to display the icon without text:

Paste custom icon

To use the toolbar, simply select the text you want to search on and click the button representing the desired search engine.

Extending the toolbar

It's very easy to add any other engines that you want.

I added search buttons for Live Search and for my own del.icio.us bookmarks. Here is the additional macro code:

Public Sub Delicious()
  NavigateTo("http://del.icio.us/alan.dean/{0}")
End Sub

Public Sub Live()
  NavigateTo("http://search.live.com/results.aspx?q={0}")
End Sub

Sometimes the icon does not paste properly. As an example, the Live Search favicon loses transparency. Luckily, Visual Studio provides a way of editing the pasted icon:

Icon context menu

The following dialog is displayed to edit the icon:

Icon edit dialog

After adding these two additional buttons and docking the toolbar, it looks like this:

Completed toolbar