다음을 통해 공유


Control.ControlCollection.Add 메서드

지정된 컨트롤을 컨트롤 컬렉션에 추가합니다.

네임스페이스: System.Windows.Forms
어셈블리: System.Windows.Forms(system.windows.forms.dll)

구문

‘선언
Public Overridable Sub Add ( _
    value As Control _
)
‘사용 방법
Dim instance As ControlCollection
Dim value As Control

instance.Add(value)
public virtual void Add (
    Control value
)
public:
virtual void Add (
    Control^ value
)
public void Add (
    Control value
)
public function Add (
    value : Control
)

매개 변수

  • value
    컨트롤 컬렉션에 추가할 Control입니다.

예외

예외 형식 조건

Exception

지정된 컨트롤이 최상위 컨트롤이거나, 이 컨트롤이 컨트롤 컬렉션에 추가될 때 순환 컨트롤 참조가 발생하는 경우

ArgumentException

value 매개 변수에 할당된 개체가 Control이 아닌 경우

설명

Add 메서드를 사용하면 Control 개체를 컨트롤 컬렉션의 끝에 추가할 수 있습니다.

또한 AddRange 메서드를 사용하여 새 Control 개체를 컬렉션에 추가할 수도 있습니다.

이전에 추가한 Control을 제거하려면 Remove, RemoveAt 또는 Clear 메서드를 사용합니다.

참고

Control은 한 번에 하나의 Control.ControlCollection에만 할당될 수 있습니다. Control이 이미 다른 컨트롤의 자식이라면 다른 컨트롤에 추가되기 전에 해당 컨트롤에서 제거됩니다.

상속자 참고 사항 파생 클래스에서 Add를 재정의하는 경우에는 컨트롤이 컬렉션에 추가되도록 기본 클래스의 Add 메서드를 호출해야 합니다.

예제

다음 코드 예제에서는 파생 클래스 PanelControl.ControlCollectionControl을 추가합니다. 이 예제를 실행하려면 먼저 FormPanel 컨트롤과 Button 컨트롤을 만들어야 합니다. 단추를 클릭하면 TextBox 컨트롤이 패널의 Control.ControlCollection에 추가됩니다.

' Create a TextBox to add to the Panel.
Dim TextBox1 As TextBox = New TextBox()

' Add controls to the Panel using the Add method.
Private Sub AddButton_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles AddButton.Click
    Panel1.Controls.Add(TextBox1)
End Sub
// Create a TextBox to add to the Panel.
private TextBox textBox1 = new TextBox();

// Add controls to the Panel using the Add method.
private void addButton_Click(object sender, System.EventArgs e)
{
   panel1.Controls.Add(textBox1);
}
   // Create a TextBox to add to the Panel.
private:
   TextBox^ textBox1;

   // Add controls to the Panel using the Add method.
   void addButton_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      textBox1 = gcnew TextBox;
      panel1->Controls->Add( textBox1 );
   }
// Create a TextBox to add to the Panel.
private TextBox textBox1 = new TextBox();

// Add controls to the Panel using the Add method.
private void addButton_Click(Object sender, System.EventArgs e)
{
    panel1.get_Controls().Add(textBox1);
} //addButton_Click

플랫폼

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0, 1.0에서 지원

참고 항목

참조

Control.ControlCollection 클래스
Control.ControlCollection 멤버
System.Windows.Forms 네임스페이스
Remove
RemoveAt
AddRange