Freigeben über


Controlling Options Settings

Visual Studio add-ins are deprecated in Visual Studio 2013. You should upgrade your add-ins to VSPackage extensions. For more information about upgrading, see FAQ: Converting Add-ins to VSPackage Extensions.

You can write code to activate or deactivate many of the settings on the pages in the Options dialog box (hereafter referred to as Options pages). Just use the Properties property, Value property, and Item method of the DTE object in the Visual Studio automation model.

Note

Although many items on many Options pages can be accessed programmatically, certain pages may contain items that cannot be accessed. Also, it is possible that an Options page itself is not accessible. If you cannot affect a setting by using the automation model, you may be able to do so by using the Visual Studio SDK. For more information, see "Adding Settings to Existing Options Pages" later in this document. For a list of programmatically accessible options and their exact names, see "Property Item Names" in Determining the Names of Property Items on Options Pages.

To open the Options dialog box in the integrated development environment (IDE), on the Tools menu, click Options.

Displaying Options Settings

Use the Properties collection and Property object to access settings on an Options page. The following Visual Studio example displays the names, current values, and types for the items on the Documents page.

Sub PropertiesExample()
    ' Create and initialize a variable to represent the Documents 
    ' Options page.
    Dim envGenTab As EnvDTE.Properties = _
    DTE.Properties("Environment", "Documents")
    Dim prop As EnvDTE.Property
    Dim msg As String

    ' Loop through each item in the Documents Options box.
    For Each prop In envGenTab
            Try
                msg += ("PROP NAME: " & prop.Name & _ 
                " VALUE: " & prop.Value) & _
                "   TYPE: " & prop.Value.GetType.ToString()) & vbCr
            Catch
            End Try
    Next
    MsgBox(msg)
End Sub

The following example displays the available properties on the Options page for the Task List (under the Environment node). It also lists the available values for the comment Token list.

Sub DisplayProperties()
    ' Variables to represent the properties collection
    ' and each property in the Options dialog box.
    Dim prop As EnvDTE.Property
    Dim props As EnvDTE.Properties
    Dim propVals As Object()
    Dim propVal, msg As String

    ' Represents the Task List Node under the 
    ' Enviroment node.
    props = DTE.Properties("Environment", "TaskList")
    ' Represents the items in the comment Token list
    ' and their priorities (1-3/low-high).
    prop = props.Item("CommentTokens")
    propVals = prop.Value

    Try
        ' List each property name for the Options page
        ' and all of its possible values.
        For Each prop In props
            msg += "PROP NAME: " & prop.Name & vbCr
            For Each propVal In propVals
                msg += "  Value: " & propVal & vbCr
            Next
        Next
        MsgBox(msg)
    Catch ex As System.Exception
        MsgBox("ERROR: " & ex.Message)
    End Try
End Sub

This example lists the programmable settings for the Options page for Formatting (under Text Editor, C#).

Sub PropertiesExample()
    ' Create and initialize a variable to represent the C# 
    ' Formatting text editor options page.
    Dim txtEdCSFormat As EnvDTE.Properties = _
    DTE.Properties("TextEditor", "CSharp - Formatting")
    Dim prop As EnvDTE.Property
    Dim msg As String

    ' Loop through each item in the C# Formatting Options page.
    For Each prop In txtEdCSFormat
        msg += ("PROP NAME: " & prop.Name & "   VALUE: " & _
        prop.Value) & vbCr
    Next
    MsgBox(msg)
End Sub

Changing Options Settings

Not only can you display the value of a setting on an Options page, you can also change the value. The following Visual Studio examples demonstrate how to do this.

Note

Although you can change the value of controls on an existing Options page, you cannot add, remove, or modify any of the controls or settings. To specify your own settings, you must create a custom Options page. For more information, see How to: Create Custom Options Pages.

The first example (ToolOpt1) toggles the Boolean value of ReuseSavedActiveDocWindow, which is the name of the Reuse current document window, if saved option on the Documents page under the Environment node.

Sub ToolOpt1()
    Dim props As EnvDTE.Properties = DTE.Properties("Environment", _
    "Documents")
    Dim prop As EnvDTE.Property

    prop = props.Item("ReuseSavedActiveDocWindow")
    ' If value is TRUE, change it to FALSE, or vice-versa.
    MsgBox("PROP NAME: " & prop.Name & "   VALUE: " & prop.Value)
    prop.Value = Not (prop.Value)
    MsgBox("PROP NAME: " & prop.Name & "   VALUE: " & prop.Value)
    ' Change it to the original value.
    prop.Value = Not (prop.Value)
End Sub

The following example changes and then resets the Tab size value in the Tabs section of the Basic page under the Text Editor node.

Sub ToolOpt2()
    Dim props As EnvDTE.Properties = DTE.Properties("TextEditor", _
    "Basic")
    Dim prop As EnvDTE.Property
    Dim tmp As String

    prop = props.Item("TabSize")
    ' Set a new value for Tab Size.
    MsgBox("PROP NAME: " & prop.Name & "   VALUE: " & prop.Value)
    tmp = prop.Value
    prop.Value = 10
    MsgBox("PROP NAME: " & prop.Name & "   VALUE: " & prop.Value)
    ' Change it back to the original value.
    prop.Value = tmp
    MsgBox("PROP NAME: " & prop.Name & "   VALUE: " & prop.Value)
End Sub

This example changes a setting on the Fonts and Colors page under the Environment node.

Sub ToolOpt3()
    ' Changes the background color of text in the Fonts and Colors
    ' page of the Options dialog box on the Tools menu.
    Dim props As EnvDTE.Properties
    Dim prop As EnvDTE.Property
    Dim fontColorItems As EnvDTE.FontsAndColorsItems

    props = DTE.Properties("FontsAndColors", "TextEditor")
    prop = props.Item("FontsAndColorsItems")
    fontColorItems = prop.Object

    Try
        MsgBox("NAME: " & prop.Name & vbCr & "BACKGROUND VALUE: " & _
        CStr(fontColorItems.Item("Plain Text").Background.ToString))
        ' Turn the text background from its current color to red.
        fontColorItems.Item("Plain Text").Background = 255
        MsgBox("NAME: " & prop.Name & vbCr & "BACKGROUND VALUE: " & _
        Hex(fontColorItems.Item("Plain Text").Background.ToString))
    Catch ex As System.Exception
        MsgBox("ERROR: " & ex.Message)
    End Try
End Sub

This example turns on line numbering for several languages in the Text Editor node.

Sub TurnOnLineNumbers()
   DTE.Properties("TextEditor", "Basic").Item("ShowLineNumbers") _
   .Value = True
   DTE.Properties("TextEditor", "PlainText").Item("ShowLineNumbers") _
   .Value = True
   DTE.Properties("TextEditor", "CSharp").Item("ShowLineNumbers") _
   .Value = True
   DTE.Properties("TextEditor", "HTML/XML").Item("ShowLineNumbers") _
   .Value = True
   DTE.Properties("TextEditor", "C/C++").Item("ShowLineNumbers") _
   .Value = True
   DTE.Properties("TextEditor", "Visual JSharp") _
   .Item("ShowLineNumbers").Value = True
End Sub

Adding Settings to Existing Options Pages

You cannot use the Visual Studio automation model to add settings to an existing Options page or to change existing settings, To make these kinds of modifications, you must use the Visual Studio SDK. For more information, see the Development Tools Ecosystem Partner Portal Web site.

See Also

Tasks

How to: Create Custom Options Pages

How to: Change Window Characteristics

How to: Create an Add-In

Walkthrough: Creating a Wizard

Concepts

Automation Object Model Chart

Other Resources

Creating and Controlling Environment Windows

Creating Add-ins and Wizards

Automation and Extensibility Reference