Export (0) Print
Expand All

DefaultPropertyAttribute Class

Specifies the default property for a component.

For a list of all members of this type, see DefaultPropertyAttribute Members.

System.Object
   System.Attribute
      System.ComponentModel.DefaultPropertyAttribute

[Visual Basic]
<AttributeUsage(AttributeTargets.Class)>
NotInheritable Public Class DefaultPropertyAttribute
   Inherits Attribute
[C#]
[AttributeUsage(AttributeTargets.Class)]
public sealed class DefaultPropertyAttribute : Attribute
[C++]
[AttributeUsage(AttributeTargets::Class)]
public __gc __sealed class DefaultPropertyAttribute : public
   Attribute
[JScript]
public
   AttributeUsage(AttributeTargets.Class)
class DefaultPropertyAttribute extends Attribute

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.

Remarks

Use the Name property to get the name of the default event.

For more information, see Attributes Overview and Extending Metadata Using Attributes.

Example

[Visual Basic, C#, C++] The following example defines a control named MyControl. The class is marked with a DefaultPropertyAttribute that specifies MyProperty as the default property.

[Visual Basic] 
<DefaultProperty("MyProperty")> _
Public Class MyControl
    Inherits Control

    Public Property MyProperty() As Integer
        Get
            ' Insert code here.
            Return 0
        End Get
        Set
            ' Insert code here.
        End Set 
    End Property
    ' Insert any additional code.
End Class 'MyControl

[C#] 
[DefaultProperty("MyProperty")]
 public class MyControl : Control {
 
    public int MyProperty {
       get {
          // Insert code here.
          return 0;
       }
       set {
          // Insert code here.
       }
    }
 
    // Insert any additional code.
 
 }

[C++] 
public:
    [DefaultProperty(S"MyProperty")]
    __gc class MyControl : public Control {

    public:
        __property int get_MyProperty() {
            // Insert code here.
            return 0;
        }
        __property void set_MyProperty( int value ) {
            // Insert code here.
        }

        // Insert any additional code.
    };

[Visual Basic, C#, C++] The next example creates an instance of MyControl. Then it gets the attributes for the class, extracts the DefaultPropertyAttribute, and prints the name of the default property.

[Visual Basic] 
Public Shared Function Main() As Integer
    ' Creates a new control.
    Dim myNewControl As New MyControl()
    
    ' Gets the attributes for the collection.
    Dim attributes As AttributeCollection = TypeDescriptor.GetAttributes(myNewControl)
    
    ' Prints the name of the default property by retrieving the
    ' DefaultPropertyAttribute from the AttributeCollection. 
    Dim myAttribute As DefaultPropertyAttribute = _
        CType(attributes(GetType(DefaultPropertyAttribute)), DefaultPropertyAttribute)
    Console.WriteLine(("The default property is: " + myAttribute.Name))
    Return 0
End Function 'Main


[C#] 
public static int Main() {
    // Creates a new control.
    MyControl myNewControl = new MyControl();
 
    // Gets the attributes for the collection.
    AttributeCollection attributes = TypeDescriptor.GetAttributes(myNewControl);
 
    /* Prints the name of the default property by retrieving the 
     * DefaultPropertyAttribute from the AttributeCollection. */
    DefaultPropertyAttribute myAttribute = 
       (DefaultPropertyAttribute)attributes[typeof(DefaultPropertyAttribute)];
    Console.WriteLine("The default property is: " + myAttribute.Name);
  
    return 0;
 }

[C++] 
int main() {
    // Creates a new control.
    Form1::MyControl* myNewControl = new Form1::MyControl();

    // Gets the attributes for the collection.
    AttributeCollection* attributes = TypeDescriptor::GetAttributes(myNewControl);

    /* Prints the name of the default property by retrieving the 
    * DefaultPropertyAttribute from the AttributeCollection. */
    DefaultPropertyAttribute* myAttribute = 
        dynamic_cast<DefaultPropertyAttribute*>(attributes->Item[__typeof(DefaultPropertyAttribute)]);
    Console::WriteLine(S"The default property is: {0}", myAttribute->Name);

    return 0;
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.ComponentModel

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System (in System.dll)

See Also

DefaultPropertyAttribute Members | System.ComponentModel Namespace | Attribute

Show:
© 2014 Microsoft