Partager via


ControlCollection.AddControl Method

Definition

Overloads

AddControl(Control, Range, String)

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

AddControl(Control, Double, Double, Double, Double, String)

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

AddControl(Control, Range, String)

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

public:
 Microsoft::Office::Tools::Excel::ControlSite ^ AddControl(System::Windows::Forms::Control ^ control, Microsoft::Office::Interop::Excel::Range ^ range, System::String ^ name);
public Microsoft.Office.Tools.Excel.ControlSite AddControl (System.Windows.Forms.Control control, Microsoft.Office.Interop.Excel.Range range, string name);
abstract member AddControl : System.Windows.Forms.Control * Microsoft.Office.Interop.Excel.Range * string -> Microsoft.Office.Tools.Excel.ControlSite
Public Function AddControl (control As Control, range As Range, name As String) As ControlSite

Parameters

control
Control

The control to be added to the ControlCollection instance.

range
Range

A Range that provides the bounds for the control.

name
String

The name of the control that can be used to index the control in the ControlCollection instance.

Returns

An object that represents the control that contains the specified control on the worksheet.

Exceptions

The control, name or range argument is null, or the name argument has zero length.

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

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.

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 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;
}
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

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.

Applies to

AddControl(Control, Double, Double, Double, Double, String)

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

public:
 Microsoft::Office::Tools::Excel::ControlSite ^ AddControl(System::Windows::Forms::Control ^ control, double left, double top, double width, double height, System::String ^ name);
public Microsoft.Office.Tools.Excel.ControlSite AddControl (System.Windows.Forms.Control control, double left, double top, double width, double height, string name);
abstract member AddControl : System.Windows.Forms.Control * double * double * double * double * string -> Microsoft.Office.Tools.Excel.ControlSite
Public Function AddControl (control As Control, left As Double, top As Double, width As Double, height As Double, name As String) As ControlSite

Parameters

control
Control

The control to be added to the ControlCollection instance.

left
Double

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

top
Double

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

width
Double

The width of the control in points.

height
Double

The height of the control in points.

name
String

The name of the control.

Returns

An object that represents the control that contains the specified control on the worksheet.

Exceptions

The control or name argument is null or has zero length.

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

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 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;
}
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

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.

Applies to