Share via


Visual Basic 6.0 사용자를 위한 Controls 컬렉션

업데이트: 2007년 11월

Visual Basic 6.0의 Controls 컬렉션은 Visual Basic 2008에서 Control.ControlCollection 클래스로 대체되었습니다.

개념적 차이

Visual Basic 6.0의 Controls 컬렉션은 컨테이너 컨트롤 또는 폼의 컨트롤을 나타내는 요소가 있는 컬렉션입니다.

Visual Basic 2008에서는 Control.ControlCollection 클래스가 Controls 컬렉션을 대신합니다. 폼에는 Me.Controls 구문을 사용하여 액세스할 수 있는 Control.ControlCollection 클래스가 기본적으로 포함됩니다.

Add 메서드

Visual Basic 6.0에서는 Controls 컬렉션의 Add 메서드가 런타임에 바인딩되며 Control 클래스를 인수로 지정하여 Add 메서드에서 컨트롤이 생성됩니다.

Visual Basic 2008에서 Control.ControlCollection 클래스의 Add 메서드를 사용하려면 컬렉션에 추가하기 전에 New 키워드를 사용하여 컨트롤을 만들어야 합니다.

Remove 메서드

Visual Basic 6.0 Controls 컬렉션의 Remove 메서드는 Add 메서드를 사용하여 추가된 컨트롤에만 사용할 수 있었지만 Visual Basic 2008Control.ControlCollection 클래스에는 이러한 제한이 없습니다.

Timer 및 Menu 컨트롤

Visual Basic 6.0에서는 Timer 및 Menu 컨트롤이 Controls 컬렉션의 멤버입니다. Visual Basic 2008에서는 이러한 컨트롤이 TimerMainMenu 또는 ContextMenu 구성 요소로 대체되었습니다. 이 구성 요소는 Control.ControlCollection 클래스의 멤버가 아닙니다.

포함된 컨트롤

Visual Basic 6.0 Controls 컬렉션은 컨테이너 컨트롤의 자식 컨트롤(예: Frame 컨트롤에 있는 컨트롤)을 포함하지만 Visual Basic 2008Control.ControlCollection 클래스는 이러한 컨트롤을 포함하지 않습니다. 폼의 모든 컨트롤에서 반복하려면 각 컨테이너 컨트롤의 Controls 클래스를 통해 재귀적으로 반복해야 합니다.

Controls 컬렉션에 대한 코드 변경 사항

다음 예제에서는 Visual Basic 6.0의 코딩 기술과 Visual Basic 2008의 코딩 기술이 어떻게 다른지 보여 줍니다.

Controls 추가하고 제거하는 방법에 대한 코드 변경 사항

다음 코드에서는 Visual Basic 6.0 Controls 컬렉션과 Visual Basic 2008Control.ControlCollection 클래스의 차이점을 보여 줍니다.

' Visual Basic 6.0
Private Sub Command1_Click()
    ' Declare a new Control variable.
    Dim c As Control
    ' Create and add the new control.
    Set c = Controls.Add("VB.TextBox", "Text1")
    ' Make the new control visible.
    c.Visible = True
    ' Set the initial text.
    c.Text = "Hello"
    ' Retrieve the text from the new TextBox.
    If Controls.Count > 1 Then
        MsgBox (Controls("Text1").Text)
    End If
    ' Remove the new control.
    Controls.Remove (Text1)
    ' The following line causes a compilation error.
    ' You cannot remove controls added at design time.
    Controls.Remove (Command1)
End Sub
' Visual Basic
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ' Create a new TextBox control.
    Dim TextBox1 As New System.Windows.Forms.TextBox
    TextBox1.Name = "TextBox1"
    ' Add the new control to the form's Controls collection.
    Me.Controls.Add(TextBox1)
    ' No need to set Visible property.
    ' Set the initial text.
    TextBox1.Text = "Hello"
    ' Retrieve the text from the new TextBox.
    If Me.Controls.Count > 1 Then
        MsgBox(Me.Controls("TextBox1").Text)
    End If
    ' Remove the new control.
    Me.Controls.Remove(TextBox1)
    ' Remove the control added at design time.
    Me.Controls.Remove(Button1)
End Sub

Controls 컬렉션을 통해 반복하는 방법에 대한 코드 변경 사항

다음 코드는 폼의 모든 컨트롤을 반복한 다음 CheckBox 컨트롤을 모두 지우는 함수를 보여 줍니다. 이 예제에서는 CheckBox 컨트롤이 폼이 아닌 GroupBox 또는 Panel 컨트롤에 있다고 가정합니다. Visual Basic 2008 예제에서 폼의 Controls 컬렉션은 폼에 직접 배치된 컨트롤만 포함하므로 함수는 자식이 있는 컨트롤에 대해 자신을 재귀적으로 호출합니다.

' Visual Basic 6.0
Private Sub ClearChecks()
    For Each Control in Me.Controls
        If TypeOf Control Is CheckBox Then
            Control.Value = vbUnchecked
        End If
    Next
End Sub
' Visual Basic
Private Sub ClearChecks(ByVal Container As Control)
    Dim ctl As Control
    Dim chk As CheckBox
    For Each ctl In Container.Controls
        If TypeOf ctl Is CheckBox Then
            chk = ctl
            chk.Checked = False
        End If
        ' Recursively call this function for any container controls.
        If ctl.HasChildren Then
            ClearChecks(ctl)
        End If
    Next
End Sub

업그레이드 참고 사항

Visual Basic 6.0과 Visual Basic 2008Controls 컬렉션의 차이로 인해 Add 메서드에 대한 호출이 업그레이드되지 않습니다. 새 Add 메서드를 사용하여 응용 프로그램 동작을 다시 만들려면 코드를 추가해야 합니다.

참고 항목

참조

Control.Controls

Form.ControlCollection