Results 1 to 8 of 8

Thread: Check if control is a part of an array

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Location
    ja
    Posts
    82

    Resolved Check if control is a part of an array

    Is there a way to check if a control is a part of an array? I want to do this at runtime.
    Last edited by john42; Jan 16th, 2005 at 01:47 PM.
    Hej på dej!

  2. #2
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657

    Re: Check if control is a part of an array

    Looping thru the controls on the form, if referencing the Index property does not generate an error, then the control is part of a control array:
    VB Code:
    1. Private Sub Command1_Click()
    2.    
    3.     Dim objCtl  As Control
    4.     Dim intX    As Integer
    5.    
    6.     On Error Resume Next
    7.    
    8.     For Each objCtl In Form1.Controls
    9.         intX = objCtl.Index
    10.         If Err.Number = 0 Then
    11.             Debug.Print objCtl.Name & " is a control array."
    12.         Else
    13.             Debug.Print objCtl.Name & " is NOT a control array."
    14.             Err.Clear
    15.         End If
    16.     Next
    17.  
    18. End Sub
    "It's cold gin time again ..."

    Check out my website here.

  3. #3
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Check if control is a part of an array

    There is a very little documented TypeName() function than does the trick:
    VB Code:
    1. If TypeName(Command1) = "Object" Then
    2.         MsgBox "Control is member of control array"
    3.     Else
    4.         MsgBox "Control is not member of control array"
    5.     End If

    EDIT: for in-depth explanations visit Randy's site .
    Last edited by RhinoBull; Jan 15th, 2005 at 10:25 AM.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Location
    ja
    Posts
    82

    Re: Check if control is a part of an array

    Thanks for the help guys!
    RhinoBull: That was what I was looking for!
    Hej på dej!

  5. #5
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Check if control is a part of an array

    Excellent!

  6. #6
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: Check if control is a part of an array

    Quote Originally Posted by RhinoBull
    There is a very little documented TypeName() function than does the trick
    Brilliant!!
    I'd previously used something similar to BruceG's method. It works but not very elegant. If any snippet of code deserves to be in the CodeBank, this is it!

    Cheers
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Check if control is a part of an array

    My way (which I've modified to fit Bruce's code) is a tiny bit simpler.

    VB Code:
    1. Private Sub Command1_Click(Index As Integer)
    2.  
    3.     Dim objCtl  As Control
    4.    
    5.     On Error Resume Next
    6.    
    7.     For Each objCtl In Form1.Controls
    8.         If objCtl.Index > -1 Then
    9.             Debug.Print objCtl.Name & " is a control array."
    10.         Else
    11.             Debug.Print objCtl.Name & " is NOT a control array."
    12.             Err.Clear
    13.         End If
    14.     Next
    15.  
    16. End Sub

    BTW, John now that we've helped you, please help us and indicate that you have your answer by editing your first post in this thread and adding then green checkmark.

  8. #8
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Check if control is a part of an array

    Quote Originally Posted by pnish
    Brilliant!!
    I'd previously used something similar to BruceG's method. It works but not very elegant. If any snippet of code deserves to be in the CodeBank, this is it!

    Cheers
    Thanks.

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