AmbientValueAttribute Class (System.ComponentModel)

Switch View :
ScriptFree
.NET Framework Class Library
AmbientValueAttribute Class

Specifies the value to pass to a property to cause the property to get its value from another source. This is known as ambience. This class cannot be inherited.

Inheritance Hierarchy

System.Object
  System.Attribute
    System.ComponentModel.AmbientValueAttribute

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

Visual Basic
<AttributeUsageAttribute(AttributeTargets.All)> _
Public NotInheritable Class AmbientValueAttribute _
	Inherits Attribute
C#
[AttributeUsageAttribute(AttributeTargets.All)]
public sealed class AmbientValueAttribute : Attribute
Visual C++
[AttributeUsageAttribute(AttributeTargets::All)]
public ref class AmbientValueAttribute sealed : public Attribute
F#
[<Sealed>]
[<AttributeUsageAttribute(AttributeTargets.All)>]
type AmbientValueAttribute =  
    class
        inherit Attribute
    end

The AmbientValueAttribute type exposes the following members.

Constructors

  Name Description
Public method AmbientValueAttribute(Boolean) Initializes a new instance of the AmbientValueAttribute class, given a Boolean value for its value.
Public method AmbientValueAttribute(Byte) Initializes a new instance of the AmbientValueAttribute class, given an 8-bit unsigned integer for its value.
Public method AmbientValueAttribute(Char) Initializes a new instance of the AmbientValueAttribute class, given a Unicode character for its value.
Public method AmbientValueAttribute(Double) Initializes a new instance of the AmbientValueAttribute class, given a double-precision floating-point number for its value.
Public method AmbientValueAttribute(Int16) Initializes a new instance of the AmbientValueAttribute class, given a 16-bit signed integer for its value.
Public method AmbientValueAttribute(Int32) Initializes a new instance of the AmbientValueAttribute class, given a 32-bit signed integer for its value.
Public method AmbientValueAttribute(Int64) Initializes a new instance of the AmbientValueAttribute class, given a 64-bit signed integer for its value.
Public method AmbientValueAttribute(Object) Initializes a new instance of the AmbientValueAttribute class, given an object for its value.
Public method AmbientValueAttribute(Single) Initializes a new instance of the AmbientValueAttribute class, given a single-precision floating point number for its value.
Public method AmbientValueAttribute(String) Initializes a new instance of the AmbientValueAttribute class, given a string for its value.
Public method AmbientValueAttribute(Type, String) Initializes a new instance of the AmbientValueAttribute class, given the value and its type.
Top
Properties

  Name Description
Public property TypeId When implemented in a derived class, gets a unique identifier for this Attribute. (Inherited from Attribute.)
Public property Value Gets the object that is the value of this AmbientValueAttribute.
Top
Methods

  Name Description
Public method Equals Determines whether the specified AmbientValueAttribute is equal to the current AmbientValueAttribute. (Overrides Attribute.Equals(Object).)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCode Returns the hash code for this instance. (Overrides Attribute.GetHashCode().)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method IsDefaultAttribute When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class. (Inherited from Attribute.)
Public method Match When overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Top
Explicit Interface Implementations

  Name Description
Explicit interface implemetation Private method _Attribute.GetIDsOfNames Maps a set of names to a corresponding set of dispatch identifiers. (Inherited from Attribute.)
Explicit interface implemetation Private method _Attribute.GetTypeInfo Retrieves the type information for an object, which can be used to get the type information for an interface. (Inherited from Attribute.)
Explicit interface implemetation Private method _Attribute.GetTypeInfoCount Retrieves the number of type information interfaces that an object provides (either 0 or 1). (Inherited from Attribute.)
Explicit interface implemetation Private method _Attribute.Invoke Provides access to properties and methods exposed by an object. (Inherited from Attribute.)
Top
Remarks

If a property on a control has ambient behavior, this attribute must be present. Ambient properties query their parent for their value, for example, a Control.Font property or a Control.BackColor property.

Typically, a visual designer uses the AmbientValueAttribute attribute to decide which value to persist for a property. This is usually a value that causes the property to get its value from another source. An example of an ambient value is Color.Empty as the ambient value for the BackColor property. If you have a control on a form and the BackColor property of the control is set to a different color than the BackColor property of the form, you can reset the BackColor property of the control to that of the form by setting the BackColor of the control to Color.Empty.

Examples

The following code example demonstrates using AmbientValueAttribute to enforce ambient behavior for a property called AlertForeColor. For a full code listing, see How to: Apply Attributes in Windows Forms Controls.

Visual Basic

<AmbientValue(GetType(Color), "Empty"), _
Category("Appearance"), _
DefaultValue(GetType(Color), "White"), _
Description("The color used for painting alert text.")> _
Public Property AlertForeColor() As Color
    Get
        If Me.alertForeColorValue = Color.Empty AndAlso (Me.Parent IsNot Nothing) Then
            Return Parent.ForeColor
        End If

        Return Me.alertForeColorValue
    End Get

    Set(ByVal value As Color)
        Me.alertForeColorValue = value
    End Set
End Property

' This method is used by designers to enable resetting the
' property to its default value.
Public Sub ResetAlertForeColor()
    Me.AlertForeColor = AttributesDemoControl.defaultAlertForeColorValue
End Sub

' This method indicates to designers whether the property
' value is different from the ambient value, in which case
' the designer should persist the value.
Private Function ShouldSerializeAlertForeColor() As Boolean
    Return Me.alertForeColorValue <> AttributesDemoControl.ambientColorValue
End Function


C#

[AmbientValue(typeof(Color), "Empty")]
[Category("Appearance")]
[DefaultValue(typeof(Color), "White")]
[Description("The color used for painting alert text.")]
public Color AlertForeColor
{
    get
    {
        if (this.alertForeColorValue == Color.Empty &&
            this.Parent != null)
        {
            return Parent.ForeColor;
        }

        return this.alertForeColorValue;
    }

    set
    {
        this.alertForeColorValue = value;
    }
}

// This method is used by designers to enable resetting the
// property to its default value.
public void ResetAlertForeColor()
{
    this.AlertForeColor = AttributesDemoControl.defaultAlertForeColorValue;
}

// This method indicates to designers whether the property
// value is different from the ambient value, in which case
// the designer should persist the value.
private bool ShouldSerializeAlertForeColor()
{
    return (this.alertForeColorValue != AttributesDemoControl.ambientColorValue);
}


Version Information

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Platforms

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
See Also

Reference