Results 1 to 11 of 11

Thread: Only for Guru's

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2000
    Location
    México, D.F.
    Posts
    64

    Talking

    How can i know if a control of my form is an array or not.

    Thanx


  2. #2
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606

    Question ??????????

    Do you mean at design or at run time?

  3. #3
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    Red face oops only for Gurus

    See ya
    Paul Lewis

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 2000
    Location
    México, D.F.
    Posts
    64

    Talking Re: oops only for Gurus

    at runtime

  5. #5
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    You can do something like this:
    Code:
    Public Function IsControlArray(pControl As Control) As Boolean
        On Error Resume Next
        
        IsControlArray = pControl.Index
    End Function
    Then call this function like this:
    Code:
    Private Sub Command1_Click()
        If IsControlArray(Command1) Then
            MsgBox "Control is a part of control array"
        Else
            MsgBox "Control is not a part of control array"
        End If
    End Sub

  6. #6
    Fanatic Member
    Join Date
    Feb 2000
    Location
    The Netherlands
    Posts
    715
    Originally posted by Erick de la Torre
    Only for gurus
    Give your threads a meaningfull subject. And never something like "easy question" or "only for gurus".
    Oetje
    [email protected]
    93606776
    Visual Basic 6, Windows 2000

    Never pet a burning dog

  7. #7
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    Wink

    Give your threads a meaningfull subject. And never something like "easy question" or "only for gurus".
    I agree. Although Erick only lost 20 minutes between the time I could have answered and when Serge did.

    For the first time (that I recall) I have been rude in a post... But you really need to post message for everyone to consider and respond to... There are only a limited supply of Guru's you know

    Cheers
    Paul Lewis

  8. #8
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    Actually, Serge, with your code, if I have an array of 5 CommandButtons, and I did something like this:
    Code:
    Set MyControl = MyCommandButtonArray(0) ' First button in array
    IsItAnArray = IsControlArray(MyControl)
    Then IsItAnArray would be False, even though it should be True.
    Here's a better way:
    Code:
    Function IsItAnArray(pControl As Control) As Boolean
        On Error Resume Next
        
        ' Dummy comparison, to see if the Index property is accessible
        If pControl.Index = pControl.Index Then IsItAnArray = (Err.Number = 0)
    End Function

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Aug 2000
    Location
    México, D.F.
    Posts
    64

    Talking

    Hey, i only do it to keep your attention.

    Sorry, and thanx for reply

  10. #10
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    Yonatan, you're not passing control array to the function my friend.

  11. #11
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    Question A few more tires since this morning it seems

    Serge's example is not 100% because of the false reporting
    that Yonatan noticed plus the error you will get if you use
    the code from a click event of a button that is part of a
    control array. You cannot pass a control array as a
    parameter if a Control is expected.

    Yonatan's code works, but if you already know the index of
    the control you want to pass, well why bother at all?

    The rather long example below will provide several things.
    One is a test to see if a control is a part of a control
    array (assuming you are enumerating all controls of a form
    for example). The other is rather pointess I think, but it
    is there for completeness. It will test if an Object
    happens to be a Control Array.

    I know the example is long, but if you really want to find
    out ... it may assist you...

    I hope this sparks interest from Yonatan and Serge (and
    others) to find a better way of testing

    Regards

    Code:
    Option Explicit
    
    Private Sub Command2_Click()
      Dim myControl As Control
      Set myControl = Command1(0)
      
      ' test 1: is a control, a part of a control array?
      Debug.Print IsPartOfControlArray(myControl) 'true
      
      Set myControl = Command2
      ' test 2: is a control, a part of a control array?
      Debug.Print IsPartOfControlArray(myControl) 'false
      
      ' test 3: is the Object a control array?
      Debug.Print IsControlArray(Command1) 'true
      'Debug.Print IsControlArray2(Command1) 'true
      
      ' test 4: is the Object a control array?
      Debug.Print IsControlArray(Command2) 'false
      
      ' test 5: try to trick the test
      Dim myCol As New Collection
      myCol.Add (Command1(0))
      myCol.Add (Command1(1))
      Debug.Print IsControlArray(myCol) 'false
      
      ' test 5: try to trick the test with an array
      Dim myControls(3) As Object
      Set myControls(0) = Command1    ' control array object
      Set myControls(1) = Command2    ' control object
      Set myControls(2) = Command1(0) ' control object
      Debug.Print IsControlArray(myControls) 'false
      
      ' test 7: check all of the controls on the form
      ' I have a control array of Command1 plus a few other controls
      For Each myControl In Controls
        Debug.Print myControl.Name, TypeName(myControl), IsPartOfControlArray(myControl), IsControlArray(myControl)
        ' Should be true,false for all controls that are part of controlarray
        ' should be false,false for all normal controls
      Next
      
    End Sub
    
    Private Function IsPartOfControlArray(aControl As Control) As Boolean
      ' simple test of a Control to see if it is part of a control array
      On Error GoTo exit_func
      If aControl.Index = aControl.Index Then IsPartOfControlArray = True
    exit_func:
    End Function
    
    Private Function IsControlArray(anObject As Variant) As Boolean
      ' it is unlikely you can trick this code into returning true
      ' for anything other than a control array.
      Dim lastname As String
      Dim lasttype As String
      On Error GoTo exit_func
      ' do something that a collection would respond to
      ' Control Arrays are LIKE collections, but they are Objects according
      ' to typename.  You can do the same sort of things as with a collection
      If anObject.Count <> -1 And TypeName(anObject) = "Object" Then
        'Debug.Print "Possibly a Collection"
        Dim myobj As Control
        For Each myobj In anObject
          ' see if the name of the last item matches this one
          If lastname <> "" And lastname <> myobj.Name Then
            Exit Function
          End If
          
          ' see if the type of the last item matches this one
          If lasttype <> "" And lasttype <> TypeName(myobj) Then
            Exit Function
          End If
          
          lastname = myobj.Name
          lasttype = TypeName(myobj)
        Next
        IsControlArray = True
      End If
      Exit Function
    exit_func:
    End Function
    Paul Lewis

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width