把自制控件放在VB的容器控件中,在自制控件CODE中有什么办法获取该容器控件?

解决方案 »

  1.   

    UserControl.Parent返回的是该控件所在的FORM
      

  2.   

    UserControl.Extender.Container我试过了,该方法不支持,应该如何返回它的容器控件,在迭代的时候如何判断是控件本身?
      

  3.   

    UserControl.Extender.Container应该是一个指针,类似于指向容器控件的地址,并不能返回其所在的容器控件
      

  4.   

    如果放在picture1里
        Dim i As Long
        For i = 0 To UserControl.ParentControls.Count - 1
            MsgBox UserControl.ParentControls.Item(i).Name
        Next   Form1
       Picture1
       自己
       其它1
       其它2
       ………如果把pictur1放在Frame1
       Form1
       Frame1
       Picture1
       自己
       其它1
       其它2
       ………
       
    看到上面的规律了吧,呵呵呵
    UserControl.ParentControls 集合从0开始 ,也是从最上层的容器开始,祝你好运!
      

  5.   

    Dim i As Long
        For i = 0 To UserControl.ParentControls.Count - 1
            MsgBox UserControl.ParentControls.Item(i).Name
        Next    此代码在控件内部,可以自己感受一下!
      

  6.   

    那么在控件CODE内部迭代的时候如何判断是自己呢?用UserControl.Extender其中的属性吗?
      

  7.   

    Dim i As Long
        For i = 0 To UserControl.ParentControls.Count - 1
            MsgBox UserControl.ParentControls.Item(i).Name
        Next这些就在控件内部啊,我不大明白你的意思了
      

  8.   

    我在控件CODE内部迭代的时候发现控件自己并不是紧接着父控件。
    是这样的,大家知道,在VB中有一个OPTION控件,如果OPTION控件在一个窗口或容器控件不只一个,在设置他的VALUE属性为TRUE时,其它OPTION控件的VALUE属性自动变为FALSE。
     所以需要迭代控件所在父控件所包含的所有同类控件,并排除自己,同时设置同类控件的相关属性。
      

  9.   

    当自己被点击了是吧???你
        For i = 0 To UserControl.ParentControls.Count - 1
            'MsgBox UserControl.ParentControls.Item(i).Name
            判断是不是同类,是同类就把某一属性设为 false(包括自己)
        Next
        再把 me 的属性设置为true这样不就解决了吗?因为Me 就是当前被点击的一个呀!!!
      

  10.   

    不知道你看明白没有,呵呵呵拿OPTION这来说吧(假设都在同一容器内)option1,option2,option3 option1 被点击了,控件的点击事件发生,这时候在控件内部code中 me 就是option1是吧
    通过 UserControl.ParentControls可以把所有的同类包括自己全找到并修改属性,修改完了
    再在内部code中修改Me的属性!
      

  11.   

    是啊,我的思路也是这样的,只不过实现的时候碰到了麻烦,一开始的时候代码进入死循环,导致堆栈溢出,呵呵!
    现在已经实现了,以下是我的CODE,好象有点繁,请指教!
    Public Property Let Value(ByVal new_value As Boolean)
     SetOtherChecks new_value
     mValue = new_value
     DrawCheck
     PropertyChanged ("Value")
    End PropertyPrivate Sub SetOtherChecks(ByVal nowvalue As Boolean)
     Dim i As Integer
     Dim j As Integer
     Dim k As Integer
     If nowvalue = True Then
      For i = 0 To UserControl.ParentControls.Count - 1
      If UserControl.ParentControls.Item(i).Name = UserControl.Extender.Name Then
       If UserControl.ParentControls.Item(i).Index = UserControl.Extender.Index Then
        For j = i - 1 To 0 Step -1
         If UserControl.ParentControls.Item(j) Is UserControl.ParentControls.Item(i).Container Then
            For k = j + 1 To i - 1
             If TypeName(UserControl.ParentControls.Item(k)) = UserControl.Name Then
              If UserControl.ParentControls.Item(k).Value = True Then
               UserControl.ParentControls.Item(k).Value = False
              End If
             End If
            Next
            For k = i + 1 To UserControl.ParentControls.Count - 1
             If UserControl.ParentControls.Item(k).Container Is UserControl.ParentControls.Item(i).Container Then
              If TypeName(UserControl.ParentControls.Item(k)) = UserControl.Name Then
               If UserControl.ParentControls.Item(k).Value = True Then
                UserControl.ParentControls.Item(k).Value = False
               End If
              End If
             Else
              'Exit For
             End If
            Next
          Exit For
         End If
        Next
        Exit For
       End If
      End If
      Next
     End If
    End Sub
      

  12.   

    是啊,我的思路也是这样的,只不过实现的时候碰到了麻烦,一开始的时候代码进入死循环,导致堆栈溢出,呵呵!  
    现在已经实现了,以下是我的CODE,好象有点繁,请指教!  
    Public  Property  Let  Value(ByVal  new_value  As  Boolean)  
     SetOtherChecks  new_value  
     mValue  =  new_value  
     DrawCheck  
     PropertyChanged  (  "Value  ")  
    End  Property  
     
    Private Sub SetOtherChecks(ByVal nowvalue As Boolean)
     Dim i As Integer
     Dim j As Integer
     Dim k As Integer
     If nowvalue = True Then
      For i = 0 To UserControl.ParentControls.Count - 1
      If UserControl.ParentControls.Item(i).Name = UserControl.Extender.Name Then
       If UserControl.ParentControls.Item(i).Index = UserControl.Extender.Index Then '判断是否是控件自身
        For j = i - 1 To 0 Step -1 '往回寻找父控件
         If UserControl.ParentControls.Item(j) Is UserControl.ParentControls.Item(i).Container Then '判断是否为父控件
            For k = j + 1 To i - 1 '设置控件自身前面的控件(在父控件中)属性
             If TypeName(UserControl.ParentControls.Item(k)) = UserControl.Name Then
              If UserControl.ParentControls.Item(k).Value = True Then
               UserControl.ParentControls.Item(k).Value = False
              End If
             End If
            Next
            For k = i + 1 To UserControl.ParentControls.Count - 1 '设置控件自身后面的控件(在父控件中)属性
             If UserControl.ParentControls.Item(k).Container Is UserControl.ParentControls.Item(i).Container Then
              If TypeName(UserControl.ParentControls.Item(k)) = UserControl.Name Then
               If UserControl.ParentControls.Item(k).Value = True Then
                UserControl.ParentControls.Item(k).Value = False
               End If
              End If
             Else
              'Exit For
             End If
            Next
          Exit For
         End If
        Next
        Exit For
       End If
      End If
      Next
     End If
    End Sub
      

  13.   

    Public  Property  Let  Value(ByVal  new_value  As  Boolean)  
     SetOtherChecks  new_value  
     mValue  =  new_value  
     DrawCheck  
     PropertyChanged  (  "Value  ")  
    End  Property这样写不溢出和怪呢,为什么你不想想,你给每个同类修改属性就会无限的循环下去嘛
    增加一个Public  Property  Let  Value2(ByVal  new_value  As  Boolean)  
     mValue  =  new_value  
     DrawCheck  
     PropertyChanged  (  "Value  ")  
    End  Property再把下面的修改属性的赋值用 object.value2 = asdfasdfPrivate Sub SetOtherChecks(ByVal nowvalue As Boolean)
     Dim i As Integer
     Dim j As Integer
     Dim k As Integer
     If nowvalue = True Then
      For i = 0 To UserControl.ParentControls.Count - 1
      If UserControl.ParentControls.Item(i).Name = UserControl.Extender.Name Then
       If UserControl.ParentControls.Item(i).Index = UserControl.Extender.Index Then '判断是否是控件自身
        For j = i - 1 To 0 Step -1 '往回寻找父控件
         If UserControl.ParentControls.Item(j) Is UserControl.ParentControls.Item(i).Container Then '判断是否为父控件
            For k = j + 1 To i - 1 '设置控件自身前面的控件(在父控件中)属性
             If TypeName(UserControl.ParentControls.Item(k)) = UserControl.Name Then
              If UserControl.ParentControls.Item(k).Value = True Then
               UserControl.ParentControls.Item(k).Value2 = False
              End If
             End If
            Next
            For k = i + 1 To UserControl.ParentControls.Count - 1 '设置控件自身后面的控件(在父控件中)属性
             If UserControl.ParentControls.Item(k).Container Is UserControl.ParentControls.Item(i).Container Then
              If TypeName(UserControl.ParentControls.Item(k)) = UserControl.Name Then
               If UserControl.ParentControls.Item(k).Value = True Then
                UserControl.ParentControls.Item(k).Value2 = False
               End If
              End If
             Else
              'Exit For
             End If
            Next
          Exit For
         End If
        Next
        Exit For
       End If
      End If
      Next
     End If
    End Sub
      

  14.   

    Public  Property  Let  Value(ByVal  new_value  As  Boolean) 
     SetOtherChecks  new_value  
     mValue  =  new_value  
     DrawCheck  
     PropertyChanged  (  "Value  ")  
    End  Property你的就相当于下面了,在一个属性赋值语句中给属性赋值,这不是循环赋值?你说会不会溢出?
    Public  Property  Let  Value(ByVal  new_value  As  Boolean) 
        me.Value = new_value  
    End  Property你的代码还真繁啊,呵呵呵,你考虑到了同一容器中的同类是吧,那样是麻烦一点,如果不考虑这种情况就容易多,但也不用这么麻烦Private Sub SetOtherChecks(ByVal nowvalue As Boolean)
        Dim i As Integer, j As Integer, k As Integer
        'If nowvalue = True Then 为什么false就不作任何操作,呵呵呵话外题了,如果真不操作就加上这条咯
            For i = 0 To UserControl.ParentControls.Count - 1
                If UserControl.ParentControls.Item(i).Name = UserControl.Extender.Name Then
                    If UserControl.ParentControls.Item(i).Index = UserControl.Extender.Index Then Exit For '判断是否是控件自身
                End If
            Next
            
            '往回寻找父控件
            For j = i - 1 To 0 Step -1
                If UserControl.ParentControls.Item(j) Is UserControl.ParentControls.Item(i).Container Then '判断是否为父控件
                '说明 UserControl.ParentControls.Item(j) 是自己的父在容器,这个 j 很重要
                Exit For
            Next
                
            For i = 0 To UserControl.ParentControls.Count - 1
                '是不是同类
                If TypeName(UserControl.ParentControls.Item(i)) = UserControl.Name Then
                    '找到一个同类,可能包括自己,不管,判断它是不是在和自己为同一父容器
                    If UserControl.ParentControls.Item(j) Is UserControl.ParentControls.Item(i).Container Then
                        '是属性Value2喔
                        UserControl.ParentControls.Item(k).Value2 = Not nowvalue
                    End If
                End If
            Next
     'End If
    End Sub
      

  15.   

    没有死循环啊,因为在设置VALUE属性的时候,把SETOTHERCHECKS放在了前面,并且只有在VALUE为TRUE的时候才进入SETOTHERCHECKS的主CODE,也就是说只有控件自身才运行SETOTHERCHECKS。