65.9K
CodeProject is changing. Read more.
Home

Making F1 do something useful in Visual Studio

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.90/5 (13 votes)

Feb 11, 2011

CPOL

1 min read

viewsIcon

13527

Thanks OriginalGriff! This is great and really helped me out. :-D However, for my tastes I like to open the URL in the default browser (currently that's Chrome for me), which allows me to easily open up several tabs from the links or bookmark useful entries.Also, the original F1 would...

Thanks OriginalGriff! This is great and really helped me out. :-D However, for my tastes I like to open the URL in the default browser (currently that's Chrome for me), which allows me to easily open up several tabs from the links or bookmark useful entries. Also, the original F1 would search the Help based on what word the text cursor was on, whether it was selected or not. I liked the ease of that. Last, but not least, as I was writing a blog post on this great tip I stated that 95% of the time my search term came up in the first result. Then it hit me. :omg: If I think that what I want will be my first hit, why not just return Google's first result; the equivalent of hitting the older I'm Feeling Lucky button on Google's home page. So, I added a second subroutine that does exactly that. I set the key assignment for GoogleSearchMSDN_FeelingLucky to Shift+F1. Now, if I think a term is likely to return what I want in the first hit, like "msdn ActionResult" grabs the Microsoft MSDN page for the ActionResult class, then I use Shift+F1, otherwise I just use the standard F1 and get all the results. So, I updated the code with these three features as follows:
Sub GoogleSearchMSDN_FeelingLucky()
    GoogleSearchMSDN(True)
End Sub
Sub GoogleSearchMSDN(Optional ByVal feelingLucky As Boolean = False)
    Dim url As String
    Dim text As String
    Dim searchFor As TextSelection = DTE.ActiveDocument.Selection()

    If Not searchFor.IsEmpty Then
        'Search value is selected.  Get the selected text.
        text = searchFor.Text
    Else
        'Text cursor is on the search value.
        'Store the current cursor location
        Dim currentColumn As Integer = searchFor.ActivePoint.DisplayColumn
        'Move to the beginning of the word
        searchFor.WordLeft()
        'Select to the end of the word
        searchFor.WordRight(True)
        text = Trim(searchFor.Text)
        'Move the cursor back to the original position
        searchFor.MoveToDisplayColumn(0, currentColumn)
    End If

    url = "http://www.google.com/search?q=MSDN+" + text
    If feelingLucky Then
        url += "&btnI=745"
    End If

    'Launch search in default browser
    System.Diagnostics.Process.Start(url)
End Sub
I hope this helps!