Share via


ControlCollection.AddControl Method (Control, Range, String)

Adds the specified Control to the ControlCollection at the specified range.

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, _
    range As Range, _
    name As String _
) As ControlSite
ControlSite AddControl(
    Control control,
    Range range,
    string name
)

Parameters

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, name or range argument is nulla null reference (Nothing in Visual Basic), or the name argument has zero length.

ControlNameAlreadyExistsException

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

InvalidRangeException

The range that was specified is not valid. Multi-area ranges cannot be used. The range should be on the same worksheet as 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