EventDescriptorCollection.Item[] Property

Definition

Gets the specified event.

Overloads

Item[Int32]

Gets or sets the event with the specified index number.

Item[String]

Gets or sets the event with the specified name.

Item[Int32]

Source:
EventDescriptorCollection.cs
Source:
EventDescriptorCollection.cs
Source:
EventDescriptorCollection.cs

Gets or sets the event with the specified index number.

C#
public virtual System.ComponentModel.EventDescriptor? this[int index] { get; }
C#
public virtual System.ComponentModel.EventDescriptor this[int index] { get; }

Parameters

index
Int32

The zero-based index number of the EventDescriptor to get or set.

Property Value

The EventDescriptor with the specified index number.

Exceptions

index is not a valid index for Item[Int32].

Examples

The following code example uses the Item[] property to print the name of the EventDescriptor specified by the index number in a text box. Because the index number is zero-based, this example prints the name of the second EventDescriptor. It requires that button1 and textBox1 have been instantiated on a form.

C#
private void PrintIndexItem() {
    // Creates a new collection and assigns it the events for button1.
    EventDescriptorCollection events = TypeDescriptor.GetEvents(button1);
 
    // Prints the second event's name.
    textBox1.Text = events[1].ToString();
 }

Remarks

The index number is zero-based. Therefore, you must subtract 1 from the numerical position of a particular EventDescriptor to access that EventDescriptor. For example, to get the third EventDescriptor, you need to specify myColl[2].

See also

Applies to

.NET 10 och andra versioner
Produkt Versioner
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Item[String]

Source:
EventDescriptorCollection.cs
Source:
EventDescriptorCollection.cs
Source:
EventDescriptorCollection.cs

Gets or sets the event with the specified name.

C#
public virtual System.ComponentModel.EventDescriptor this[string name] { get; }

Parameters

name
String

The name of the EventDescriptor to get or set.

Property Value

The EventDescriptor with the specified name, or null if the event does not exist.

Examples

The following code example uses the Item[] property to print the type of the component for the EventDescriptor specified by the index. It requires that button1 and textBox1 have been instantiated on a form.

C#
private void PrintIndexItem2() {
    // Creates a new collection and assigns it the events for button1.
    EventDescriptorCollection events = TypeDescriptor.GetEvents(button1);
 
    // Sets an EventDescriptor to the specific event.
    EventDescriptor myEvent = events["KeyDown"];
 
    // Prints the name of the event.
    textBox1.Text = myEvent.Name;
 }

Remarks

The Item[] property is case-sensitive when searching for names. That is, the names "Ename" and "ename" are considered to be two different events.

Anteckning

The HostProtectionAttribute attribute applied to this class has the following Resources property value: Synchronization. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

See also

Applies to

.NET 10 och andra versioner
Produkt Versioner
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1