BindingList<T>.CancelNew(Int32) 方法

定义

丢弃挂起的新项。

public:
 virtual void CancelNew(int itemIndex);
public virtual void CancelNew (int itemIndex);
abstract member CancelNew : int -> unit
override this.CancelNew : int -> unit
Public Overridable Sub CancelNew (itemIndex As Integer)

参数

itemIndex
Int32

要添加的新项的 索引。

实现

示例

下面的代码示例演示如何使用 CancelNew 方法。 有关完整示例,请参阅 BindingList<T> 类概述主题。

// Add the new part unless the part number contains
// spaces. In that case cancel the add.
private void button1_Click(object sender, EventArgs e)
{
    Part newPart = listOfParts.AddNew();

    if (newPart.PartName.Contains(" "))
    {
        MessageBox.Show("Part names cannot contain spaces.");
        listOfParts.CancelNew(listOfParts.IndexOf(newPart));
    }
    else
    {
        textBox2.Text = randomNumber.Next(9999).ToString();
        textBox1.Text = "Enter part name";
    }
}
' Add the new part unless the part number contains
' spaces. In that case cancel the add.
Private Sub button1_Click(ByVal sender As Object, _
    ByVal e As EventArgs) Handles button1.Click

    Dim newPart As Part = listOfParts.AddNew()

    If newPart.PartName.Contains(" ") Then
        MessageBox.Show("Part names cannot contain spaces.")
        listOfParts.CancelNew(listOfParts.IndexOf(newPart))
    Else
        textBox2.Text = randomNumber.Next(9999).ToString()
        textBox1.Text = "Enter part name"
    End If

End Sub

注解

方法 CancelNew 回滚通过 AddNew 方法添加但尚未提交的挂起的新项。

有关添加和提交新项的详细信息,请参阅 AddNew 方法

适用于

另请参阅