views:

99

answers:

3

I'm finding the built-in Visual Studio Document Explorer less relevant, especially as more of the SDKs I work with have the most up-to-date content on-line. Pressing F1 starts Document Explorer usually with something unhelpful and it's not usable any more for me.

Is there any way that on the press of a key combination in Visual Studio:

  • the default browser opens to the URL of a search engine
  • query used is the keyword under the current cursor position
  • a filter is added such as site:msdn.microsoft.com

I don't know anything about macros in VS but presumably that's what I need. Does anyone know how to go about setting this up? teh codez would be nice!

+1  A: 

I think this may be exactly what you want.

In fact I have the impetus to install it in the morning myself!

Preet Sangha
+1 Gave me enough to come up with a 'default browser' solution
Alex Angas
+2  A: 

Using the link Preet provided I came up with this which starts the default browser:

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics

Public Module Search
    Sub GoogleSearch()
        AnySearch("http://www.google.com/search?q=")
    End Sub

    Sub BingSearch()
        AnySearch("http://www.bing.com/search?q=")
    End Sub

    Private Sub AnySearch(ByVal searchUrl)
        Dim strUrl As String
        Dim selection As TextSelection = DTE.ActiveDocument.Selection()
        If selection.Text <> "" Then
            strUrl = searchUrl + selection.Text
            DTE.ExecuteCommand("nav", strUrl & " /ext")
        Else
            MsgBox("Select text to search for.")
        End If
    End Sub
End Module
Alex Angas
+3  A: 

Here is another version (based on Alex's answer) that will also select the current word you are on. More like the typical F1 help.

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics

Public Module Search
    Sub GoogleSearch()
        AnySearch("http://www.google.com/search?q=.net+")
    End Sub

    Sub BingSearch()
        AnySearch("http://www.bing.com/search?q=")
    End Sub

    Private Sub AnySearch(ByVal searchUrl)
        Dim strUrl As String
        Dim selection As String = GetSelection()
        If selection <> "" Then
            strUrl = searchUrl + selection
            DTE.ExecuteCommand("nav", strUrl & " /ext")
        Else
            MsgBox("Select text to search for.")
        End If
    End Sub

    Private Function GetSelection() As String
        Dim selection As TextSelection = DTE.ActiveDocument.Selection()
        If selection.Text <> "" Then
            Return selection.Text
        Else
            DTE.ExecuteCommand("Edit.SelectCurrentWord")
            selection = DTE.ActiveDocument.Selection()
            Return selection.Text
        End If
    End Function
End Module
Michael Silver
Awesome! I was trying to work out how to do this and couldn't get it.
Alex Angas
This doesn't work on my VS 2005 install - it just does nothing - no error message. If I change the DTE.ExecuteCommand to System.Diagnostics.Process.Start(strUrl), then it opens the URL in my default browser, but the browser isn't given focus. What is "nav" supposed to do. Is this documented anywhere? And the " /ext", this all seems like magic not mentioned in the msdn page for ExecuteCommand. Any ideas how I can fix this? Thanks.
Andrew Bainbridge