Share via


ControlCollection.AddControl Method (Control, Double, Double, Double, Double, String)

Adds the specified Control to the ControlCollection at the location and size specified.

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

Syntax

'Declaration
Function AddControl ( _
    control As Control, _
    left As Double, _
    top As Double, _
    width As Double, _
    height As Double, _
    name As String _
) As ControlSite
ControlSite AddControl(
    Control control,
    double left,
    double top,
    double width,
    double height,
    string name
)

Parameters

  • left
    Type: System.Double

    The distance in points between the left edge of the control and the left edge of the worksheet.

  • top
    Type: System.Double

    The distance in points between the top edge of the control and the top edge of the worksheet.

  • height
    Type: System.Double

    The height of the control in points.

Return Value

Type: Microsoft.Office.Tools.Excel.ControlSite
An object that represents the control that contains the specified control on the worksheet.

Exceptions

Exception Condition
ArgumentNullException

The control or name argument is nulla null reference (Nothing in Visual Basic) or has zero length.

ControlNameAlreadyExistsException

A control with the same name is already in the ControlCollection instance.

Remarks

This method can be used to add any control to the ControlCollection at run time. For more information, see Adding Controls to Office Documents at Run Time.

Examples

The following code example adds two custom user controls to the worksheet using the AddControl method. The first control is added to a range of cells. The second control is added to a specific location. The code changes the Top property of the first custom user control, which only moves the control relative to the ControlSite that contains the control on the worksheet. The code then sets the Top property of the ControlSite returned by the second user control to illustrate the correct way to set the Top property of the control.

Private Sub ExcelRangeAddControl()

        Dim CustomUserControl As New UserControl1()
        Dim CustomUserControl2 As New UserControl2()

        Dim DynamicControl As Microsoft.Office.Tools.Excel.ListObject = Me.Controls.AddControl( _
            CustomUserControl, 0, 0, 150, 150, _
            "DynamicControl")

        Dim DynamicControl2 As Microsoft.Office.Tools.Excel. _
            ControlSite = Me.Controls.AddControl( _
            CustomUserControl2, 200, 0, 150, 150, _
            "DynamicControl2")

        CustomUserControl.BackColor = Color.Blue
        CustomUserControl2.BackColor = Color.Green

        CustomUserControl.Top = 100
        DynamicControl2.Top = 100

    End Sub
private void ExcelRangeAddControl()
{

    UserControl1 customUserControl = new UserControl1();
    UserControl2 customUserControl2 = new UserControl2();

    Microsoft.Office.Tools.Excel.ControlSite dynamicControl =
        this.Controls.AddControl(customUserControl,
        0, 0, 150, 150, "dynamicControl");

    Microsoft.Office.Tools.Excel.ControlSite dynamicControl2 =
        this.Controls.AddControl(customUserControl2, 200, 0,
        150, 150, "dynamicControl2");

    customUserControl.BackColor = Color.Blue;
    customUserControl2.BackColor = Color.Green;

    customUserControl.Top = 100;
    dynamicControl2.Top = 100;
}

.NET Framework Security

See Also

Reference

ControlCollection Interface

AddControl Overload

Microsoft.Office.Tools.Excel Namespace