ControlCollection.AddControl Method (Control, Single, Single, Single, Single, String)
Adds the specified control to the ControlCollection at the specified location, with the specified size.
Namespace: Microsoft.Office.Tools.Word
Assembly: Microsoft.Office.Tools.Word (in Microsoft.Office.Tools.Word.dll)
Syntax
'Declaration
Function AddControl ( _
control As Control, _
left As Single, _
top As Single, _
width As Single, _
height As Single, _
name As String _
) As ControlSite
ControlSite AddControl(
Control control,
float left,
float top,
float width,
float height,
string name
)
Parameters
- control
Type: System.Windows.Forms.Control
The control to be added to the ControlCollection instance.
- left
Type: System.Single
The distance in points between the left edge of the control and the left edge of the document.
- top
Type: System.Single
The distance in points between the top edge of the control and the top edge of the document.
- width
Type: System.Single
The width of the control in points.
- height
Type: System.Single
The height of the control in points.
- name
Type: System.String
The name of the control.
Return Value
Type: Microsoft.Office.Tools.Word.ControlSite
An object that represents the control that contains the specified control on the document.
Exceptions
Exception | Condition |
---|---|
ArgumentNullException | The control or name 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. |
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 document using the AddControl method. The first control is added to a Range. 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 document. 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 WordRangeAddControl()
Me.Paragraphs(1).Range.InsertParagraphBefore()
Dim CustomUserControl As New UserControl1()
Dim CustomUserControl2 As New UserControl2()
Dim DynamicControl As Microsoft.Office.Tools.Word.ControlSite = _
Me.Controls.AddControl(CustomUserControl, _
Me.Paragraphs(1).Range, 150, 150, "DynamicControl")
Dim DynamicControl2 As Microsoft.Office.Tools.Word.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 WordRangeAddControl()
{
this.Paragraphs[1].Range.InsertParagraphBefore();
UserControl1 customUserControl = new UserControl1();
UserControl2 customUserControl2 = new UserControl2();
Microsoft.Office.Tools.Word.ControlSite dynamicControl =
this.Controls.AddControl(customUserControl,
this.Paragraphs[1].Range, 150, 150, "dynamicControl");
Microsoft.Office.Tools.Word.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
- Full trust for the immediate caller. This member cannot be used by partially trusted code. For more information, see Using Libraries from Partially Trusted Code.