ControlCollection.RemoveAt(Int32) 方法

定义

移除 ControlCollection 实例中指定索引处的控件。

public void RemoveAt (int index);

参数

index
Int32

要从 ControlCollection 实例移除的控件的从零开始的索引。

例外

索引小于零或者索引等于或大于计数。

示例

下面的代码示例向 Button 工作表添加一个控件和一个 MonthCalendar 控件。 单击该按钮时,该代码将删除 MonthCalendar 由索引指定的控件。

private void ExcelControlRemoveAt()
{

    Microsoft.Office.Tools.Excel.Controls.Button button2 =
        this.Controls.AddButton(0, 0, 56.25, 17.25,
        "button2");
    Microsoft.Office.Tools.Excel.Controls.MonthCalendar
         monthCalendar1 = this.Controls.AddMonthCalendar(
         0, 20, 75, 15, "monthCalendar1");
    button2.Text = "Remove";
    button2.Click += new EventHandler(button2_Click);
}

void button2_Click(object sender, EventArgs e)
{
    this.Controls.RemoveAt(1);
}
Private Sub ExcelControlRemoveAt()

    Dim Button2 As Microsoft.Office.Tools.Excel. _
        Controls.Button = Me.Controls.AddButton( _
        0, 0, 56.25, 17.25, "Button2")
    Dim MonthCalendar1 As Microsoft.Office.Tools. _
        Excel.Controls.MonthCalendar = Me.Controls. _
        AddMonthCalendar(0, 20, 75, 15, "MonthCalendar1")

    Button2.Text = "Remove"

    AddHandler Button2.Click, AddressOf Button2_Click

End Sub 
Private Sub Button2_Click(ByVal sender As Object, ByVal e _
    As EventArgs)

    Me.Controls.RemoveAt(1)

End Sub

注解

在设计时添加的控件无法从实例中移除 ControlCollection 。 只能使用 RemoveAt 方法删除在运行时添加的控件。

适用于