AddingNewEventHandler 委托

定义

表示用来处理 AddingNew 事件的方法。Represents the method that will handle the AddingNew event.

public delegate void AddingNewEventHandler(System::Object ^ sender, AddingNewEventArgs ^ e);
public delegate void AddingNewEventHandler(object sender, AddingNewEventArgs e);
type AddingNewEventHandler = delegate of obj * AddingNewEventArgs -> unit
Public Delegate Sub AddingNewEventHandler(sender As Object, e As AddingNewEventArgs)

参数

sender
Object

事件的源,通常为数据容器或数据绑定集合。The source of the event, typically a data container or data-bound collection.

e
AddingNewEventArgs

包含事件数据的 AddingNewEventArgsA AddingNewEventArgs that contains the event data.

继承
AddingNewEventHandler

示例

下面的代码示例演示如何使用AddingNewEventHandler委托来BindingSource.AddingNew处理事件。The following code example demonstrates how to use the AddingNewEventHandler delegate to handle the BindingSource.AddingNew event. 此代码示例摘自了如何:自定义带有 Windows 窗体 BindingSource的项添加。This code example is part of a larger example provided in How to: Customize Item Addition with the Windows Forms BindingSource.

// This event handler provides custom item-creation behavior.
void OnCustomersBindingSourceAddingNew(Object^ sender, 
    AddingNewEventArgs^ e)
{
    e->NewObject = DemoCustomer::CreateNewCustomer();
}
// This event handler provides custom item-creation behavior.
void customersBindingSource_AddingNew(
    object sender, 
    AddingNewEventArgs e)
{
    e.NewObject = DemoCustomer.CreateNewCustomer();
}
' This event handler provides custom item-creation behavior.
 Private Sub customersBindingSource_AddingNew( _
 ByVal sender As Object, _
 ByVal e As AddingNewEventArgs) _
 Handles customersBindingSource.AddingNew

     e.NewObject = DemoCustomer.CreateNewCustomer()

 End Sub

注解

BindingSource.AddingNew事件发生在将新项添加到集合之前, 通常在数据绑定方案中。The BindingSource.AddingNew event occurs prior to adding a new item to a collection, typically in data-binding scenarios. 此事件的处理程序可以提供要添加的新项, 并重写集合类的标准操作。The handler of this event can supply the new item to be added, overriding the standard action of the collection class. 这是通过将NewObject AddingNewEventArgs参数e的属性设置为此新项来完成的。This is accomplished by setting the NewObject property of the AddingNewEventArgs parameter e to this new item. 通常, 此项必须是接收方集合所需的类型, 否则集合将引发类型InvalidCastException为的异常。Typically this item must be of a type expected by the recipient collection, or the collection will throw an exception of type InvalidCastException.

此事件通常用于数据绑定方案, 如System.Windows.Forms.BindingSourceSystem.ComponentModel.BindingList<T>等类中。This event is commonly used in data-binding scenarios, within classes such as System.Windows.Forms.BindingSource and System.ComponentModel.BindingList<T>.

创建 AddingNewEventHandler 委托时,需要标识将要处理该事件的方法。When you create an AddingNewEventHandler delegate, you identify the method that will handle the event. 若要将事件与事件处理程序关联,请将该委托的一个实例添加到事件中。To associate the event with your event handler, add an instance of the delegate to the event. 除非移除了该委托,否则每当发生该事件时就会调用事件处理程序。The event handler is called whenever the event occurs, unless you remove the delegate. 有关事件处理程序委托的详细信息, 请参阅处理和引发事件For more information about event-handler delegates, see Handling and Raising Events.

扩展方法

GetMethodInfo(Delegate)

获取指示指定委托表示的方法的对象。Gets an object that represents the method represented by the specified delegate.

适用于

另请参阅