DefaultEventAttribute Class
 

Specifies the default event for a component.

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

Inheritance Hierarchy
SystemObject
   SystemAttribute
    System.ComponentModelDefaultEventAttribute

Syntax
[AttributeUsageAttribute(AttributeTargets.Class)]
public sealed class DefaultEventAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::Class)]
public ref class DefaultEventAttribute sealed : Attribute
[<Sealed>]
[<AttributeUsageAttribute(AttributeTargets.Class)>]
type DefaultEventAttribute = 
    class
        inherit Attribute
    end
<AttributeUsageAttribute(AttributeTargets.Class)>
Public NotInheritable Class DefaultEventAttribute
	Inherits Attribute
Constructors
NameDescription
System_CAPS_pubmethod DefaultEventAttribute

Initializes a new instance of the DefaultEventAttribute class.

Properties
NameDescription
System_CAPS_pubproperty

Gets the name of the default event for the component this attribute is bound to.

System_CAPS_pubproperty

When implemented in a derived class, gets a unique identifier for this Attribute.(Inherited from Attribute.)

Methods
NameDescription
System_CAPS_pubmethod Equals

Returns whether the value of the given object is equal to the current DefaultEventAttribute.(Overrides AttributeEquals.)

System_CAPS_pubmethod GetHashCode

Returns the hash code for this instance.(Overrides AttributeGetHashCode.)

System_CAPS_pubmethod GetType

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethod 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.)

System_CAPS_pubmethod Match

When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.(Inherited from Attribute.)

System_CAPS_pubmethod ToString

Returns a string that represents the current object.(Inherited from Object.)

Fields
NameDescription
System_CAPS_pubfield System_CAPS_static Default

Specifies the default value for the DefaultEventAttribute, which is null. This static field is read-only.

Explicit Interface Implementations
NameDescription
System_CAPS_pubinterface System_CAPS_privmethod _AttributeGetIDsOfNames

Maps a set of names to a corresponding set of dispatch identifiers.(Inherited from Attribute.)

System_CAPS_pubinterface System_CAPS_privmethod _AttributeGetTypeInfo

Retrieves the type information for an object, which can be used to get the type information for an interface.(Inherited from Attribute.)

System_CAPS_pubinterface System_CAPS_privmethod _AttributeGetTypeInfoCount

Retrieves the number of type information interfaces that an object provides (either 0 or 1).(Inherited from Attribute.)

System_CAPS_pubinterface System_CAPS_privmethod _AttributeInvoke

Provides access to properties and methods exposed by an object.(Inherited from Attribute.)

Remarks

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

For more information, see Extending Metadata Using Attributes.

Examples

The following example defines a collection class named MyCollection. The class is marked with a DefaultEventAttribute that specifies CollectionChanged as the default event.

[DefaultEvent("CollectionChanged")]
public class MyCollection : BaseCollection {

    private CollectionChangeEventHandler onCollectionChanged;

    public event CollectionChangeEventHandler CollectionChanged {
       add {
          onCollectionChanged += value;
       }
       remove {
          onCollectionChanged -= value;
       }
    }
    // Insert additional code.
}
<DefaultEvent("CollectionChanged")> _ 
Public Class MyCollection
    Inherits BaseCollection

    Public Event CollectionChanged (ByVal sender As Object, _
        ByVal e As CollectionChangeEventArgs)

    ' Insert additional code.
End Class 'MyCollection
[DefaultEvent("CollectionChanged")]
public ref class TestCollection: public BaseCollection
{
private:
    CollectionChangeEventHandler^ onCollectionChanged;

public:
    event CollectionChangeEventHandler^ CollectionChanged 
    {
    public:
        void add(CollectionChangeEventHandler^ eventHandler)
        { 
            onCollectionChanged += eventHandler; 
        }

    protected:
        void remove(CollectionChangeEventHandler^ eventHandler) 
        { 
            onCollectionChanged -= eventHandler; 
        }
    }
    // Insert additional code.
};

The next example creates an instance of MyCollection. Then it gets the attributes for the class, extracts the DefaultEventAttribute, and prints the name of the default event.

public static int Main() {
    // Creates a new collection.
    MyCollection myNewCollection = new MyCollection();

    // Gets the attributes for the collection.
    AttributeCollection attributes = TypeDescriptor.GetAttributes(myNewCollection);

    /* Prints the name of the default event by retrieving the 
     * DefaultEventAttribute from the AttributeCollection. */
    DefaultEventAttribute myAttribute = 
       (DefaultEventAttribute)attributes[typeof(DefaultEventAttribute)];
    Console.WriteLine("The default event is: " + myAttribute.Name);
    return 0;
 }
Public Shared Function Main() As Integer
    ' Creates a new collection.
    Dim myNewCollection As New MyCollection()

    ' Gets the attributes for the collection.
    Dim attributes As AttributeCollection = TypeDescriptor.GetAttributes(myNewCollection)

    ' Prints the name of the default event by retrieving the
    ' DefaultEventAttribute from the AttributeCollection. 
    Dim myAttribute As DefaultEventAttribute = _
        CType(attributes(GetType(DefaultEventAttribute)), DefaultEventAttribute)
    Console.WriteLine(("The default event is: " & myAttribute.Name))
    Return 0
End Function 'Main
int main()
{
    // Creates a new collection.
    DefaultEventAttributeExample::TestCollection^ newCollection = 
        gcnew DefaultEventAttributeExample::TestCollection;

    // Gets the attributes for the collection.
    AttributeCollection^ attributes = 
        TypeDescriptor::GetAttributes(newCollection);

    // Prints the name of the default event by retrieving the 
    // DefaultEventAttribute from the AttributeCollection.
    DefaultEventAttribute^ attribute = (DefaultEventAttribute^)
        attributes[DefaultEventAttribute::typeid];
    Console::WriteLine("The default event is: {0}", attribute->Name);
}
Version Information
.NET Framework
Available since 1.1
Silverlight
Available since 4.0
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.

Return to top