EventDescriptorCollection.Item[] 属性

定义

获取指定的事件。

重载

Item[Int32]

获取或设置具有指定索引号的事件。

Item[String]

获取或设置具有指定名称的事件。

Item[Int32]

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

获取或设置具有指定索引号的事件。

public:
 virtual property System::ComponentModel::EventDescriptor ^ default[int] { System::ComponentModel::EventDescriptor ^ get(int index); };
public virtual System.ComponentModel.EventDescriptor this[int index] { get; }
public virtual System.ComponentModel.EventDescriptor? this[int index] { get; }
member this.Item(int) : System.ComponentModel.EventDescriptor
Default Public Overridable ReadOnly Property Item(index As Integer) As EventDescriptor

参数

index
Int32

要获取或设置的 EventDescriptor 的从零开始的索引号。

属性值

具有指定索引号的 EventDescriptor

例外

index 不是 Item[Int32] 的有效索引。

示例

下面的代码示例使用 Item[] 属性在文本框中打印由索引号指定的 的名称 EventDescriptor 。 由于索引号从零开始,此示例打印第二个 EventDescriptor的名称。 它要求 button1textBox1 已在窗体上实例化。

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();
   }
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();
 }
Private Sub PrintIndexItem()
    ' Creates a new collection and assigns it the events for button1.
    Dim events As EventDescriptorCollection = TypeDescriptor.GetEvents(button1)
    
    ' Prints the second event's name.
    textBox1.Text = events(1).ToString()
End Sub

注解

索引号从零开始。 因此,必须从特定 EventDescriptor 的数字位置中减去 1 才能访问该 EventDescriptor。 例如,若要获取第三个 EventDescriptor,需要指定 myColl[2]

另请参阅

适用于

Item[String]

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

获取或设置具有指定名称的事件。

public:
 virtual property System::ComponentModel::EventDescriptor ^ default[System::String ^] { System::ComponentModel::EventDescriptor ^ get(System::String ^ name); };
public virtual System.ComponentModel.EventDescriptor this[string name] { get; }
member this.Item(string) : System.ComponentModel.EventDescriptor
Default Public Overridable ReadOnly Property Item(name As String) As EventDescriptor

参数

name
String

要获取或设置的 EventDescriptor 的名称。

属性值

具有指定名称的 EventDescriptor,或者如果该事件不存在,则为 null

示例

下面的代码示例使用 Item[] 属性打印索引指定的 的 组件的 EventDescriptor 类型。 它要求 button1textBox1 已在窗体上实例化。

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;
   }
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;
 }
Private Sub PrintIndexItem2()
    ' Creates a new collection and assigns it the events for button1.
    Dim events As EventDescriptorCollection = TypeDescriptor.GetEvents(button1)
    
    ' Sets an EventDescriptor to the specific event.
    Dim myEvent As EventDescriptor = events("KeyDown")
    
    ' Prints the name of the event.
    textBox1.Text = myEvent.Name
End Sub

注解

搜索 Item[] 名称时, 属性区分大小写。 也就是说,名称“Ename”和“ename”被视为两个不同的事件。

注意

HostProtectionAttribute应用于此类的属性具有以下Resources属性值:SynchronizationHostProtectionAttribute 不影响桌面应用程序(通常通过双击图标、键入命令或在浏览器中输入 URL 来启动这些应用程序)。 有关详细信息,请参阅 HostProtectionAttribute 类或SQL Server编程和主机保护属性

另请参阅

适用于