Export (0) Print
Expand All

DesignerOptionService Class

Provides a base class for getting and setting option values for a designer.

Namespace:  System.ComponentModel.Design
Assembly:  System (in System.dll)

'Declaration
<HostProtectionAttribute(SecurityAction.LinkDemand, SharedState := True)> _
Public MustInherit Class DesignerOptionService _
	Implements IDesignerOptionService

The DesignerOptionService type exposes the following members.

  NameDescription
Protected methodDesignerOptionServiceInitializes a new instance of the DesignerOptionService class.
Top

  NameDescription
Public propertyOptionsGets the options collection for this service.
Top

  NameDescription
Protected methodCreateOptionCollectionCreates a new DesignerOptionService.DesignerOptionCollection with the given name and adds it to the given parent.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected methodPopulateOptionCollectionPopulates a DesignerOptionService.DesignerOptionCollection.
Protected methodShowDialogShows the options dialog box for the given object.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Explicit interface implemetationPrivate methodIDesignerOptionService.GetOptionValueGets the value of an option defined in this package.
Explicit interface implemetationPrivate methodIDesignerOptionService.SetOptionValueSets the value of an option defined in this package.
Top

The DesignerOptionService class provides a collection of options. Each of these option collections has an indexer that enables it to be further filtered. Each option collection contains its own set of options, as well as a rollup of all of its child options. In the event of a naming conflict between properties, the outermost options object takes precedence. The following Tools | Options user interface (UI) structure shows how the outermost options object takes on greater importance:

WindowsFormsDesigner | General

  • SnapToGrid

  • ShowGrid

  • GridSize

Given a IDesignerOptionService named service, to get to the value of the GridSize property, you would make the following call:

' Obtains and shows the size of the standard design-mode grid square. 
Dim size As Size = CType(designerOptionService.GetOptionValue("WindowsFormsDesigner\General", "GridSize"), Size)

This works, until you want to move GridSize to another page. Also, IDesignerOptionService provides no discovery mechanism. If you do not know what string to pass in, the service cannot find the property value.

The DesignerOptionService class addresses these issues. You can query collections, and there is a type converter defined on the DesignerOptionService.DesignerOptionCollection object that marks the collection as expandable. With this type converter, you can pass the entire designer option service to a property window and visually inspect the service.

NoteNote

The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: SharedState. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

The following code example demonstrates accessing the DesignerOptionService to display the current values of the standard options.

Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing
Imports System.Data
Imports System.Windows.Forms
Imports System.Windows.Forms.Design


' This control demonstrates retrieving the standard  
' designer option service values in design mode. 

Public Class DesignerOptionServiceControl
   Inherits System.Windows.Forms.UserControl
   Private designerOptionSvc As DesignerOptionService


   Public Sub New()
      Me.BackColor = Color.Beige
      Me.Size = New Size(404, 135)
    End Sub 


   Public Overrides Property Site() As System.ComponentModel.ISite
      Get 
         Return MyBase.Site
      End Get 
      Set 
         MyBase.Site = value

         ' If siting component, attempt to obtain an DesignerOptionService. 
         If (MyBase.Site IsNot Nothing) Then
            designerOptionSvc = CType(Me.GetService(GetType(DesignerOptionService)), DesignerOptionService)
         End If 
      End Set 
   End Property 

   ' Displays control information and current DesignerOptionService  
   ' values, if available. 
   Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
        e.Graphics.DrawString("DesignerOptionServiceControl", _
        New Font("Arial", 9), _
        New SolidBrush(Color.Blue), 4, 4)

      If Me.DesignMode Then
            e.Graphics.DrawString("Currently in design mode", _
            New Font("Arial", 8), _
            New SolidBrush(Color.Black), 4, 18)
      Else
            e.Graphics.DrawString("Not in design mode. Cannot access DesignerOptionService.", _
            New Font("Arial", 8), _
            New SolidBrush(Color.Red), 4, 18)
      End If 

      If (MyBase.Site IsNot Nothing) AndAlso (designerOptionSvc IsNot Nothing) Then
            e.Graphics.DrawString("DesignerOptionService provides access to the table of option values listed when", _
            New Font("Arial", 8), _
            New SolidBrush(Color.Black), 4, 38)

            e.Graphics.DrawString("the Windows Forms Designer\General tab of the Tools\Options menu is selected.", _
            New Font("Arial", 8), _
            New SolidBrush(Color.Black), 4, 50)

            e.Graphics.DrawString("Table of standard value names and current values", _
            New Font("Arial", 8), _
            New SolidBrush(Color.Red), 4, 76)

            ' Displays a table of the standard value names and current values. 
            Dim ypos As Integer = 90

            ' Obtains and shows the size of the standard design-mode grid square. 
            Dim pd As PropertyDescriptor
            pd = designerOptionSvc.Options.Properties("GridSize")

            e.Graphics.DrawString("GridSize", _
            New Font("Arial", 8), _
            New SolidBrush(Color.Black), 4, ypos)

            e.Graphics.DrawString(pd.GetValue(Nothing).ToString(), _
            New Font("Arial", 8), _
            New SolidBrush(Color.Black), 200, ypos)

            ypos += 12

            ' Uncomment the following code to demonstrate that this 
            ' alternate syntax works the same as the previous syntax. 
            'pd = designerOptionSvc.Options["WindowsFormsDesigner"].Properties["GridSize"]; 
            'e.Graphics.DrawString("GridSize", 
            '    new Font("Arial", 8), 
            '    new SolidBrush(Color.Black), 4, ypos); 
            'e.Graphics.DrawString(pd.GetValue(null).ToString(), 
            '    new Font("Arial", 8), 
            '    new SolidBrush(Color.Black), 200, ypos); 
            'ypos += 12; 
            'pd = designerOptionSvc.Options["WindowsFormsDesigner"]["General"].Properties["GridSize"]; 
            'e.Graphics.DrawString("GridSize", 
            '    new Font("Arial", 8), 
            '    new SolidBrush(Color.Black), 4, ypos); 
            'e.Graphics.DrawString(pd.GetValue(null).ToString(), 
            '    new Font("Arial", 8), 
            '    new SolidBrush(Color.Black), 200, ypos); 
            'ypos += 12; 

            ' Obtains and shows whether the design mode surface grid is enabled.
            pd = designerOptionSvc.Options.Properties("ShowGrid")

            e.Graphics.DrawString("ShowGrid", _
            New Font("Arial", 8), _
            New SolidBrush(Color.Black), 4, ypos)

            e.Graphics.DrawString(pd.GetValue(Nothing).ToString(), _
            New Font("Arial", 8), _
            New SolidBrush(Color.Black), 200, ypos)

            ypos += 12

            ' Obtains and shows whether components should be aligned with the surface grid.
            pd = designerOptionSvc.Options.Properties("SnapToGrid")

            e.Graphics.DrawString("SnapToGrid", _
            New Font("Arial", 8), _
            New SolidBrush(Color.Black), 4, ypos)

            e.Graphics.DrawString(pd.GetValue(Nothing).ToString(), _
            New Font("Arial", 8), _
            New SolidBrush(Color.Black), 200, ypos)

            ypos += 12

            ' Obtains and shows which layout mode is selected.
            pd = designerOptionSvc.Options.Properties("LayoutMode")

            e.Graphics.DrawString("LayoutMode", _
            New Font("Arial", 8), _
            New SolidBrush(Color.Black), 4, ypos)

            e.Graphics.DrawString(pd.GetValue(Nothing).ToString(), _
            New Font("Arial", 8), _
            New SolidBrush(Color.Black), 200, ypos)

            ypos += 12

            ' Obtains and shows whether the Toolbox is automatoically 
            ' populated with custom controls and components.
            pd = designerOptionSvc.Options.Properties("AutoToolboxPopulate")

            e.Graphics.DrawString("AutoToolboxPopulate", _
            New Font("Arial", 8), _
            New SolidBrush(Color.Black), 4, ypos)

            e.Graphics.DrawString(pd.GetValue(Nothing).ToString(), _
            New Font("Arial", 8), _
            New SolidBrush(Color.Black), 200, ypos)

            ypos += 12

            ' Obtains and shows whether the component cache is used.
            pd = designerOptionSvc.Options.Properties("UseOptimizedCodeGeneration")

            e.Graphics.DrawString("Optimized Code Generation", _
            New Font("Arial", 8), _
            New SolidBrush(Color.Black), 4, ypos)

            e.Graphics.DrawString(pd.GetValue(Nothing).ToString(), _
            New Font("Arial", 8), _
            New SolidBrush(Color.Black), 200, ypos)

            ypos += 12

            ' Obtains and shows whether smart tags are automatically opened.
            pd = designerOptionSvc.Options.Properties("ObjectBoundSmartTagAutoShow")

            e.Graphics.DrawString("Automatically Open Smart Tags", _
            New Font("Arial", 8), _
            New SolidBrush(Color.Black), 4, ypos)

            e.Graphics.DrawString(pd.GetValue(Nothing).ToString(), _
            New Font("Arial", 8), _
            New SolidBrush(Color.Black), 200, ypos)
      End If 
    End Sub 
End Class

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft