例如:有一个Frame,里面有许多Textbox控件。运行时需要设置这些TextBox的属性。请问应该如何遍历这些控件?在From下可用For Each ...Next,在Frame中呢?

解决方案 »

  1.   

    Option Explicit
    Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As LongPrivate Sub Form_Load()
    Dim obj As Object
    For Each obj In Me.Controls
    If Me.Frame1.hwnd = GetParent(obj.hwnd) Then
    MsgBox obj.Name
    End If
    Next
    End Sub窗体一个  一个frame1  (我测试的,上面放2个textbox 1个command1)
      

  2.   

    Dim i As Long
        
        For i = 1 To Controls.Count - 1
            If Not TypeOf Controls(i) Is Frame Then
                MsgBox Controls(i).Name
            End If
        Next
      

  3.   

    用VB本身自带的功能就可以实现,不用声明API函数~~~~
      

  4.   

    yinweihong(真名:尹伟红) 的方法不错应该比下一个效率高
      

  5.   

    Dim objControl As Control
    For Each objControl In Me.Controls
     If objControl.Container.Name = "Frame1" Then
       objControl.Text = "12345"
     End If
    Next
      

  6.   

    利用 ContainedControls 集合
      

  7.   

    哪个效率高啊!?使用VB的原则是:
    1.能用VB本身的函数实现方法的不要使用API.2.能用API实现的不要使用外部控件.
      

  8.   

    谢谢各位:yinweihong(真名:尹伟红) 和 chendjin(走出Code...) 的解答已经实验通过。cuizm(射天狼)可能在问题的理解上有点偏差。ContainedControls没有试过,但仍然感谢。