Code Snippet: change background color of Visual Studio editor with code

When working on a project which has a trunk and branches, it happens sometimes that I am changing code at the wrong location. To make it clear that I am working on a branch I added some VB Macro to the Visual Studio IDE which changes the background color when the solution is stored in a directory called “branches”.

Sub ChangeBackgroundColorForBranchProjects() Handles SolutionEvents.opened, DocumentEvents.documentopening
        Dim backgroundColor As UInt32
        backgroundColor = &HFFFFFF
        If (DTE.Solution.FullName.ToLower.Contains("branches")) Then
            backgroundColor = &HC0FFFF
        End If

        CType(DTE.Properties("FontsAndColors", "TextEditor").Item("FontsAndColorsItems").Object, EnvDTE.FontsAndColorsItems).Item("Plain Text").Background = backgroundColor
    End Sub

You can easily add above code to Visual Studio by using the Macro IDE. I put this code within the (by default exisiting) EnvironmentEvents file part of the MyMacros project.

This simple code snippet checks only if the solution fullname (which includes the path) contains the word “branches”. It is of course possible to write more intelligent behaviour which looks for example to a setting file within your solution. Just use this as a starting point. Hopefully it will reduce the number of times that you are changing code within the wrong solution.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>