Having problems with your account or logging in?
A lot of changes are happening in the community right now. Some may affect you. READ MORE HERE

Commenting out code in multiple source files using Visual Studio

Commenting out code in multiple source files using Visual Studio

Original Question: Commenting out code in multiple source files using Visual Studio by Scot Nielsen

[Migrated content. Thread originally posted on 18 January 2011]

I was recently asked if it was possible to comment out multiple lines of code quickly?

Sure -- just select the code and choose comment/uncomment from Edit->Advanced menu.

What if these lines aren’t adjacent in the source code?

Here’s a VB macro you can import using the VS Macros IDE or Macros Explorer which might help.

It works from the output of the Find in Files results window. So you can search for the code you need to comment out and then run this macro to do the job.


Option Strict Off
Option Explicit Off
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics

Public Module CommentOutFinds

    ' Run through all the hits in the find results window
    ' open each hit and comment it out.
    Sub TemporaryMacro()
        ' This gets the Find in Files Results Window
        Dim findResultsWindow As EnvDTE.Window = _
        DTE.Windows.Item(Constants.vsWindowKindFindResults1)

        ' I expect there's a more elegant way to do this...

        ' Get all the text in the window by selecting it
        Dim txtSel As TextSelection = findResultsWindow.Selection()
        txtSel.SelectAll()

        ' Determine number of lines in the string subtract the find pre- and post-amble
        Dim c As Int32 = txtSel.Text.Split(Environment.NewLine).Length - 3

        If c
            Return
        End If

        ' Do the commenting out
        Do Until c = 0
            DTE.Windows.Item(Constants.vsWindowKindFindResults1).Activate()
            DTE.ExecuteCommand("Edit.GoToFindResults1NextLocation")
            DTE.ExecuteCommand("Edit.CommentSelection")

            c = c - 1
        Loop

    End Sub
End Module


Verified Answer: RE: Commenting out code in multiple source files using Visual Studio by Chris Glazier

Example

DISCLAIMER:

Some content on Community Tips & Information pages is not officially supported by Micro Focus. Please refer to our Terms of Use for more detail.
Version history
Revision #:
1 of 1
Last update:
‎2012-07-12 08:58
Updated by:
 
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.