Export (0) Print
Expand All

DefaultPropertyAttribute Class

Specifies the default property for a component.

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

[AttributeUsageAttribute(AttributeTargets.Class)] 
public sealed class DefaultPropertyAttribute : Attribute
/** @attribute AttributeUsageAttribute(AttributeTargets.Class) */ 
public final class DefaultPropertyAttribute extends Attribute
AttributeUsageAttribute(AttributeTargets.Class) 
public final class DefaultPropertyAttribute extends Attribute
Not applicable.

The following example defines a control named MyControl. The class is marked with a DefaultPropertyAttribute that specifies MyProperty as the default property.

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

/** @attribute DefaultProperty("MyProperty")
 */
public static class MyControl extends Control
{
    /** @property 
     */
    public int get_MyProperty()
    {
        // Insert code here.
        return 0;
    } //get_MyProperty

    /** @property 
     */
    public void set_MyProperty(int value)
    {
        // Insert code here.
    } //set_MyProperty

    // Insert any additional code.

} //MyControl

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.

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;
 }

public static void main(String[] args)
{
    // 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.get_Item(
        DefaultPropertyAttribute.class.ToType()));

    Console.WriteLine("The default property is: " 
        + myAttribute.get_Name());
    
} //main

System.Object
   System.Attribute
    System.ComponentModel.DefaultPropertyAttribute

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

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft