Export (0) Print
Expand All

DefaultEventAttribute Class

Specifies the default event for a component.

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

System.Object
   System.Attribute
      System.ComponentModel.DefaultEventAttribute

[Visual Basic]
<AttributeUsage(AttributeTargets.Class)>
NotInheritable Public Class DefaultEventAttribute
   Inherits Attribute
[C#]
[AttributeUsage(AttributeTargets.Class)]
public sealed class DefaultEventAttribute : Attribute
[C++]
[AttributeUsage(AttributeTargets::Class)]
public __gc __sealed class DefaultEventAttribute : public
   Attribute
[JScript]
public
   AttributeUsage(AttributeTargets.Class)
class DefaultEventAttribute 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#] The following example defines a collection class named MyCollection. The class is marked with a DefaultEventAttribute that specifies CollectionChanged as the default event.

[Visual Basic] 
<DefaultEvent("CollectionChanged")> _ 
Public Class MyCollection
    Inherits BaseCollection

    Public Event CollectionChanged (ByVal sender As Object, _
        ByVal e As CollectionChangeEventArgs)
    
    ' Insert additional code.
End Class 'MyCollection

[C#] 
[DefaultEvent("CollectionChanged")]
public class MyCollection : BaseCollection {
     
    private CollectionChangeEventHandler onCollectionChanged;
     
    public event CollectionChangeEventHandler CollectionChanged {
       add {
          onCollectionChanged += value;
       }
       remove {
          onCollectionChanged -= value;
       }
    }
    // Insert additional code.
}

[Visual Basic, C#] 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.

[Visual Basic] 
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

[C#] 
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;
 }

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic 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

DefaultEventAttribute Members | System.ComponentModel Namespace | Attribute

Show:
© 2014 Microsoft