Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Making F1 do something useful in Visual Studio

By , 16 Feb 2011
Rate this:
Please Sign up or sign in to vote.
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.
DTE.ItemOperations.Navigate("" & _
    Web.HttpUtility.UrlEncode(s)) ' HttpUtility is not part of Web
Here's my modified version.
Public Sub SearchInBrowser()
    Dim strUrl As String = ActiveWindowSelection().Trim()
    If strUrl.Length > 0 Then
        System.Diagnostics.Process.Start(" " & _
    End If
End Sub
Public Sub SearchInVS()
    Dim strUrl As String = ActiveWindowSelection().Trim()
    If strUrl.Length > 0 Then
        DTE.ItemOperations.Navigate("" & _
    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
    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.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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Henry Minute
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 ;) Pinmemberhoernchenmeister20-Sep-11 2:21 
GeneralReason for my vote of 5 In my default browser, nice Pinmemberjim lahey16-Feb-11 4:58 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 16 Feb 2011
Article Copyright 2011 by Henry Minute
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid