DefaultEventAttribute Class
.NET Framework 3.0
Specifies the default event for a component.
Namespace: System.ComponentModel
Assembly: System (in system.dll)
Assembly: System (in system.dll)
[AttributeUsageAttribute(AttributeTargets.Class)] public sealed class DefaultEventAttribute : Attribute
/** @attribute AttributeUsageAttribute(AttributeTargets.Class) */ public final class DefaultEventAttribute extends Attribute
AttributeUsageAttribute(AttributeTargets.Class) public final class DefaultEventAttribute extends Attribute
Not applicable.
Use the Name property to get the name of the default event.
For more information, see Attributes Overview and Extending Metadata Using Attributes.
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. }
/** @attribute DefaultEvent("CollectionChanged")
*/
public static class MyCollection extends BaseCollection
{
private CollectionChangeEventHandler onCollectionChanged;
public void add_onCollectionChanged(CollectionChangeEventHandler value)
{
onCollectionChanged = (CollectionChangeEventHandler)
System.Delegate.Combine(onCollectionChanged, value);
}
public void remove_onCollectionChanged(CollectionChangeEventHandler
value)
{
onCollectionChanged = (CollectionChangeEventHandler)
System.Delegate.Remove(onCollectionChanged, value);
}
// Insert additional code.
} //MyCollection
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 static void main(String[] args)
{
// 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.
get_Item(DefaultEventAttribute.class.ToType());
Console.WriteLine("The default event is: " + myAttribute.get_Name());
} //main
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.