Partager via


Button.Delete Method

Deletes a dynamically created Button from the worksheet and removes it from the ControlCollection.

Namespace:  Microsoft.Office.Tools.Excel.Controls
Assembly:  Microsoft.Office.Tools.Excel.v4.0.Utilities (in Microsoft.Office.Tools.Excel.v4.0.Utilities.dll)

Syntax

'Declaration
Public Function Delete As Object
public Object Delete()

Return Value

Type: System.Object

Remarks

This method should only be used with a Button that is created programmatically at run time. An exception is thrown if you call this method on a Button that is added to the worksheet at design time.

Examples

The following code example demonstrates a Button control that deletes itself when the user clicks it. The Click event handler of the button calls the Delete method to delete the button.

This example is for a document-level customization.

Private Sub DeleteControl()
    Dim DeleteButton As Microsoft.Office.Tools.Excel.Controls.Button = _
        Me.Controls.AddButton(25, 75, 80, 30, "DeleteButton")
    DeleteButton.Text = "Click to delete" 
    AddHandler DeleteButton.Click, AddressOf DeleteButton_Click
End Sub 

' Delete the clicked button. 
Private Sub DeleteButton_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim ClickedButton As Microsoft.Office.Tools.Excel.Controls.Button = _
        CType(sender, Microsoft.Office.Tools.Excel.Controls.Button)

    ClickedButton.Delete()
End Sub
private void DeleteControl()
        {
            Microsoft.Office.Tools.Excel.Controls.Button deleteButton =
                this.Controls.AddButton(25, 75, 80, 30, "deleteButton");
            deleteButton.Text = "Click to delete";
            deleteButton.Click += new EventHandler(deleteButton_Click);
        }

        // Delete the clicked button. 
        void deleteButton_Click(object sender, EventArgs e)
        {
            Microsoft.Office.Tools.Excel.Controls.Button clickedButton =
                (Microsoft.Office.Tools.Excel.Controls.Button)sender;

            clickedButton.Delete();
        }

.NET Framework Security

See Also

Reference

Button Class

Microsoft.Office.Tools.Excel.Controls Namespace