Brian Schmitt bio photo

Brian Schmitt

I write about software development, security, and the tools of our trade.

Email Twitter LinkedIn Github Stackoverflow Feed

Better Visual Studio F1

Reading time ~2 minutes

So recently there have been some great tips on turning off the F1 key in Visual Studio.

Roy Osherove and Infinities Loop

While that is useful in turning off the Visual Studio help, it may not be useful in the event that you actually NEED help.

I have found it useful to re-bind F1 to a macro that will take the currently highlighted word from your Visual Studio text editor and perform a search at the designated site.

I have provided code below to allow you to quickly search StackOverflow, Google, MSDN, and Searchdotnet.

Caveats - The below script will only work in the text editor, I can provide additional code that will also use the selected text from the output window or the html-editor. (I tried to keep it simple.)

I love macros in VS and think they are highly under used, I will be posting more soon, so subscribe and welcome to my new blog!

Take your pick of the four provided below (or bind several to the key combinations F1, Alt+F1, Ctrl+F1, etc…)

Imports EnvDTE
Imports System.Web
Public Module Search
#Region "Search Internet Sites"
  Public Const GOOGLE_FORMAT As String = "{0}"
  Public Const STACKOVERFLOW_FORMAT As String = "{0}"
  Public Const SEARCHDOTNET_FORMAT As String = "{0}&sa=Search&cof=FORID:9#1144"
  Public Const MSDN_FORMAT As String = "{0}&ac=8"
  Public Sub SearchStackOverflowForSelectedText()
  End Sub

  Public Sub SearchGoogleForSelectedText()
  End Sub

  Public Sub SearchSearchDotNetForSelectedText()
  End Sub

  Public Sub SearchMSDNForSelectedText()
  End Sub

  Private Sub SearchWebPage(ByVal SearchURLFormat As String)
      Dim sel As EnvDTE.TextSelection = DTE.ActiveWindow.Selection
      Dim srchTxt As String = sel.Text.Trim
      If srchTxt.Length > 0 Then
          DTE.ItemOperations.Navigate(String.Format(SearchURLFormat, HttpUtility.UrlEncode(srchTxt)))
      End If
  End Sub
#End Region
End Module