Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Making F1 do something useful in Visual Studio

0.00/5 (No votes)
16 Feb 2011 1  
I have an alternative Macro which, like the modification from newmodel, only requires you to have the cursor in or at either end of the search word.It also allows for searching from the Output Window and HTML Text.I have to confess that it is not all my own work, although the version...
I have an alternative Macro which, like the modification from newmodel, only requires you to have the cursor in or at either end of the search word.

It also allows for searching from the Output Window and HTML Text.

I have to confess that it is not all my own work, although the version from Coding Horror[^] won't compile.

So my only contribution is to find an alternative to the second line of this snippet.
VB
DTE.ItemOperations.Navigate("http://www.google.com/search?q=" & _
    Web.HttpUtility.UrlEncode(s)) ' HttpUtility is not part of Web


Here's my modified version.
VB
Public Sub SearchInBrowser()
    Dim strUrl As String = ActiveWindowSelection().Trim()
    If strUrl.Length > 0 Then
        System.Diagnostics.Process.Start("http://www.google.com/search?q=site:microsoft.com " & _
           Uri.EscapeDataString(strUrl))
    End If
End Sub

Public Sub SearchInVS()
    Dim strUrl As String = ActiveWindowSelection().Trim()
    If strUrl.Length > 0 Then
        DTE.ItemOperations.Navigate("http://www.google.com/search?q=" & _
           Uri.EscapeDataString(strUrl))
    End If
End Sub

Private Function ActiveWindowSelection() As String
    If DTE.ActiveWindow.ObjectKind = EnvDTE.Constants.vsWindowKindOutput Then
        Return OutputWindowSelection()
    End If
    If DTE.ActiveWindow.ObjectKind = "{57312C73-6202-49E9-B1E1-40EA1A6DC1F6}" Then
        Return HTMLEditorSelection()
    End If
    Return SelectionText(DTE.ActiveWindow.Selection)
End Function

Private Function HTMLEditorSelection() As String
    Dim hw As HTMLWindow = ActiveDocument.ActiveWindow.Object
    Dim tw As TextWindow = hw.CurrentTabObject
    Return SelectionText(tw.Selection)
End Function

Private Function OutputWindowSelection() As String
    Dim w As Window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
    Dim ow As OutputWindow = w.Object
    Dim owp As OutputWindowPane = ow.OutputWindowPanes.Item(ow.ActivePane.Name)
    Return SelectionText(owp.TextDocument.Selection)
End Function

Private Function SelectionText(ByVal sel As EnvDTE.TextSelection) As String
    If sel Is Nothing Then
        Return ""
    End If
    If sel.Text.Length = 0 Then
        SelectWord(sel)
    End If
    If sel.Text.Length <= 2 Then
        Return ""
    End If
    Return sel.Text
End Function

Private Sub SelectWord(ByVal sel As EnvDTE.TextSelection)
    Dim leftPos As Integer
    Dim line As Integer
    Dim pt As EnvDTE.EditPoint = sel.ActivePoint.CreateEditPoint()
    sel.WordLeft(True, 1)
    line = sel.TextRanges.Item(1).StartPoint.Line
    leftPos = sel.TextRanges.Item(1).StartPoint.LineCharOffset
    pt.MoveToLineAndOffset(line, leftPos)
    sel.MoveToPoint(pt)
    sel.WordRight(True, 1)
End Sub


There are two versions in this snippet SearchInBrowser which utilizes the modification in MattPenner's Alternate and SearchInVS which uses the rectified code from the original.

Assign whichever you prefer to F1 and if, you want to, the other to Alt-F1 or a key combination of your choice.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here