Logo
BUG: EnvDTE.FontsAndColorsItems.Item('Keyword').Background not updated after changing Visual Studio 2012 theme.

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio 2012
Date: September 2012    
       
Introduction

This article explains a bug that happens retrieving the background color of display items other than "Plain Text" (such as "Keyword", "Comment", "String", etc.) of the Text Editor in Visual Studio 2012 after changing the theme from Dark to Light or viceversa.

More information

Steps to reproduce the problem:

  • Create a VB.NET add-in with the code below. The add-in shows the Background color of the "Plain Text" and "Keyword" items when loaded and after the Options window is closed.
  • With Visual Studio 2012 with Dark theme, load the add-in. Both background colors are 0x1E1E1E, which is correct.
  • Go to the "Tools", "Options" window, "Environment", "General" section and change the color theme from Dark to Light. Click the OK button. The background colors are now 0xFFFFFF for the "Plain Text" (correct) but still 0x1E1E1E for the "Keyword" item.

 A similar problem happens if Visual Studio 2012 is initially with the Light theme and you change to the Dark theme.

As workaround, you can detect the current theme (see the article HOWTO: Get the current theme and detect changing it from a Visual Studio 2012 add-in) and if the background color returns 0xFFFFFF with the Dark theme or 0x1E1E1E with the Light theme then use the background color of the "Plain Text" item (this workaround assumes that background color of items such as "Keyword" is the same than the "Plain Text", which may not be true, but it is better than using the opposite background color).

Language: VB.NET   Copy Code Copy Code (IE only)
Imports System
Imports Microsoft.VisualStudio.CommandBars
Imports Extensibility
Imports EnvDTE
Imports EnvDTE80
Imports Microsoft.VisualBasic.ControlChars

Public Class Connect
   Implements IDTExtensibility2
	
   Private _applicationObject As DTE2
   Private _addInInstance As AddIn
   Private WithEvents _commandEvents As EnvDTE.CommandEvents

   Public Sub OnConnection(ByVal application As Object, ByVal connectMode As ext_ConnectMode, ByVal addInInst As Object, _
      ByRef custom As Array) Implements IDTExtensibility2.OnConnection

      Dim optionsCommand As Command

      _applicationObject = CType(application, DTE2)
      _addInInstance = CType(addInInst, AddIn)

      Select Case connectMode

         Case ext_ConnectMode.ext_cm_AfterStartup, ext_ConnectMode.ext_cm_Startup

            optionsCommand = _applicationObject.Commands.Item("Tools.Options")
            _commandEvents = _applicationObject.Events.CommandEvents(optionsCommand.Guid, optionsCommand.ID)

            CheckColors()

      End Select

   End Sub

   Private Sub _commandEvents_AfterExecute(Guid As String, ID As Integer, CustomIn As Object, CustomOut As Object) _
      Handles _commandEvents.AfterExecute

      CheckColors()

   End Sub

   Private Sub CheckColors()

      Dim fontsAndColorsItems As EnvDTE.FontsAndColorsItems
      Dim plainTextBackColor As UInteger
      Dim keywordBackColor As UInteger
      Dim msg As String

      fontsAndColorsItems = DirectCast(_applicationObject.Properties("FontsAndColors", "TextEditor").Item("FontsAndColorsItems").Object, _
         EnvDTE.FontsAndColorsItems)

      plainTextBackColor = fontsAndColorsItems.Item("Plain Text").Background
      keywordBackColor = fontsAndColorsItems.Item("Keyword").Background
    
      If plainTextBackColor <> keywordBackColor Then

         msg = "Back colors DON'T match:" & CrLf

      Else

         msg = "Back colors DO match:" & CrLf

      End If

      msg &= "Plain Text BackColor: " & plainTextBackColor.ToString("X") & CrLf
      msg &= "Keyword Text BackColor: " & keywordBackColor.ToString("X") & CrLf
    
      MessageBox.Show(msg)

   End Sub

   Public Sub OnDisconnection(ByVal disconnectMode As ext_DisconnectMode, ByRef custom As Array) _
      Implements IDTExtensibility2.OnDisconnection

      _commandEvents = Nothing

   End Sub

   Public Sub OnAddInsUpdate(ByRef custom As Array) Implements IDTExtensibility2.OnAddInsUpdate
   End Sub

   Public Sub OnStartupComplete(ByRef custom As Array) Implements IDTExtensibility2.OnStartupComplete
   End Sub

   Public Sub OnBeginShutdown(ByRef custom As Array) Implements IDTExtensibility2.OnBeginShutdown
   End Sub

End Class


Go to the 'Visual Studio Extensibility (VSX)' web site for more articles like this (Articles section)


Top