Click here to Skip to main content
15,886,258 members
Articles / Visual Studio
Alternative
Tip/Trick

Making F1 do something useful in Visual Studio

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
16 Feb 2011CPOL 7.7K   2
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Retired
United Kingdom United Kingdom
Retired Systems Admin, Programmer, Dogsbody.
Mainly on Systems for Local Government, Health Authorities,
Insurance Industry - (COBOL eeeeeeeugh).
Inventor of Synchronized Shopping.

Comments and Discussions

 
GeneralReason for my vote of 5 Great alternative ;) Pin
hoernchenmeister20-Sep-11 2:21
hoernchenmeister20-Sep-11 2:21 
GeneralReason for my vote of 5 In my default browser, nice Pin
jim lahey16-Feb-11 4:58
jim lahey16-Feb-11 4:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.