Share via


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

지정된 ControlControlCollection의 지정된 범위에 추가합니다.

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

구문

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

매개 변수

반환 값

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

예외

예외 상황
ArgumentNullException

컨트롤, 이름 또는 범위 인수가 nullNull 참조(Visual Basic의 경우 Nothing)이거나 이름 인수의 길이가 0인 경우

ControlNameAlreadyExistsException

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

InvalidRangeException

지정된 범위가 올바르지 않은 경우. 여러 영역 범위는 사용할 수 없습니다. ControlCollection 인스턴스와 동일한 워크시트의 범위여야 합니다.

설명

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

예제

다음 코드 예제에서는 AddControl 메서드를 사용하여 워크시트에 두 개의 사용자 지정 사용자 정의 컨트롤을 추가합니다. 첫 번째 컨트롤은 셀 범위에 추가됩니다. 두 번째 컨트롤은 특정 위치에 추가됩니다. 이 코드는 첫 번째 사용자 지정 사용자 정의 컨트롤의 Top 속성을 변경합니다. 이렇게 하면 워크시트에서 해당 컨트롤이 들어 있는 ControlSite를 기준으로 컨트롤이 이동만 합니다. 그런 다음 둘째 사용자 정의 컨트롤이 반환하는 ControlSiteTop 속성을 설정하여 컨트롤의 Top 속성을 올바로 설정하는 방법을 설명합니다.

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

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

참고 항목

참조

ControlCollection 인터페이스

AddControl 오버로드

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