|
-
Oct 5th, 2000, 01:01 PM
#1
Thread Starter
Lively Member
How can i know if a control of my form is an array or not.
Thanx
-
Oct 5th, 2000, 01:06 PM
#2
Frenzied Member
??????????
Do you mean at design or at run time?
-
Oct 5th, 2000, 01:06 PM
#3
Hyperactive Member
oops only for Gurus
-
Oct 5th, 2000, 01:13 PM
#4
Thread Starter
Lively Member
Re: oops only for Gurus
-
Oct 5th, 2000, 01:26 PM
#5
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
-
Oct 5th, 2000, 01:29 PM
#6
Fanatic Member
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".
-
Oct 5th, 2000, 01:32 PM
#7
Hyperactive Member
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
-
Oct 5th, 2000, 01:40 PM
#8
Guru
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
-
Oct 5th, 2000, 02:02 PM
#9
Thread Starter
Lively Member
Hey, i only do it to keep your attention.
Sorry, and thanx for reply
-
Oct 5th, 2000, 02:03 PM
#10
Yonatan, you're not passing control array to the function my friend.
-
Oct 5th, 2000, 05:18 PM
#11
Hyperactive Member
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|