다음을 통해 공유


ControlCollection.AddControl 메서드 (Control, Range, Single, Single, String)

지정된 범위와 크기의 지정된 컨트롤을 ControlCollection에 추가합니다.

네임스페이스:  Microsoft.Office.Tools.Word
어셈블리:  Microsoft.Office.Tools.Word(Microsoft.Office.Tools.Word.dll)

구문

‘선언
Function AddControl ( _
    control As Control, _
    range As Range, _
    width As Single, _
    height As Single, _
    name As String _
) As ControlSite
ControlSite AddControl(
    Control control,
    Range range,
    float width,
    float height,
    string name
)

매개 변수

  • width
    형식: System.Single
    컨트롤의 너비(포인트)입니다.
  • height
    형식: System.Single
    컨트롤의 높이(포인트)입니다.

반환 값

형식: Microsoft.Office.Tools.Word.ControlSite
문서의 지정된 컨트롤이 들어 있는 컨트롤을 나타내는 개체입니다.

예외

예외 상황
ArgumentNullException

control, name 또는 range 인수가 nullNull 참조(Visual Basic의 경우 Nothing)이거나 name 인수의 길이가 0인 경우

ControlNameAlreadyExistsException

이름이 같은 컨트롤이 ControlCollection 인스턴스에 있는 경우

InvalidRangeException

지정된 범위가 올바르지 않은 경우.

설명

이 메서드는 런타임에 컨트롤을 ControlCollection에 추가하는 데 사용될 수 있습니다. 자세한 내용은 런타임에 Office 문서에 컨트롤 추가을 참조하십시오.

예제

다음 코드 예제에서는 AddControl 메서드를 사용하여 두 개의 사용자 지정 사용자 정의 컨트롤을 문서에 추가합니다. 첫째 컨트롤은 Range에 추가됩니다. 두 번째 컨트롤은 특정 위치에 추가됩니다. 코드에서는 첫째 사용자 지정 컨트롤의 Top 속성을 변경하여 문서에서 컨트롤을 포함하는 ControlSite에 상대적으로 컨트롤을 이동합니다. 그런 다음 둘째 사용자 정의 컨트롤이 반환하는 ControlSiteTop 속성을 설정하여 컨트롤의 Top 속성을 올바로 설정하는 방법을 설명합니다.

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 보안

  • 직접 실행 호출자의 경우 완전히 신뢰합니다. 이 멤버는 부분적으로 신뢰할 수 있는 코드에서 사용할 수 없습니다. 자세한 내용은 부분 신뢰 코드에서 라이브러리 사용을 참조하십시오.

참고 항목

참조

ControlCollection 인터페이스

AddControl 오버로드

Microsoft.Office.Tools.Word 네임스페이스