Results 1 to 6 of 6

Thread: control arrays problem

  1. #1
    haris-raheem
    Guest

    Exclamation control arrays problem

    hello
    i am having a problem with control arrays
    i am dynamically loading and unloading the controls
    but an error occurs when a element which is unloaded is refenced
    i want to apply a condition to know that wether a element at this
    index exists or not.
    can any one help me.

  2. #2
    sunnyl
    Guest
    Can you try and just catch the element does not exist error instead?

    VB Code:
    1. 'assumes a Label1 with Index set to 0
    2. Private Sub Form_Load()
    3.     Load Label1(1)
    4.     Load Label1(2)
    5.    
    6.     Unload Label1(1)
    7.    
    8.     On Error Resume Next
    9.     MsgBox Label1(1).Caption
    10.    
    11.     If Err.Number = 340 Then
    12.         MsgBox "element does not exist"
    13.     End If
    14. End Sub

  3. #3
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    Perhaps you should have a public variable that keeps track of the largest possible element. You can incremenet/decrement at will.

  4. #4
    haris-raheem
    Guest

    Exclamation description

    the place where i am using this control array is critical and cannot rely on these errorr detection codes so i want to know that when a control is unloaded then whats is the thing that resides at that index please send an immediate answer

  5. #5
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    hi
    i am sure u could find a way to put error detection in any sub that accesses a nonexistent control but if u dont want to go that way u could use a piggy back variable that records the status of each control eg
    VB Code:
    1. Private Sub Command1_Click(index As Integer)
    2.     Dim IExist() As Boolean
    3.    
    4.     ReDim IExist(4)
    5.     For x = 1 To 4
    6.         Load Command1(x) 'Create at runtime
    7.         IExist(x) = True 'record as existing
    8.     Next
    9.    
    10.     Unload Command1(2) 'unload 2
    11.     IExist(2) = False 'Set corresponding var to false
    12.    
    13.     For x = 1 To 4
    14.         If IExist(x) Then Debug.Print x
    15.     Next
    16. End Sub
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  6. #6
    Tygur
    Guest
    Here is a function I just made real quick. It should be what you're looking for. Give it the control array and the index value you want to check for. It returns true if the control is there and false if it's not:
    VB Code:
    1. Private Function IsLoaded(CtrlArray, Index As Long) As Boolean
    2. Dim tVar
    3. On Local Error GoTo GotErr
    4. tVar = CtrlArray(Index).Tag
    5. IsLoaded = True
    6. Exit Function
    7. GotErr:
    8. IsLoaded = False
    9. End Function
    10.  
    11. 'Usage
    12. If IsLoaded(Command1, 1) Then
    13.     MsgBox Command1(1).Caption
    14. End If

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