CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Dec 2004
    Posts
    35

    check if element of a control array exists

    hi.

    i'd like to know, is there a way of checking, if an element of a control array exists, before i perform a Load or Unload.

    i know, i can set On Error - but i guess that's not very elegant solution.

    the thing is, that i'd like to create a control array in runtime, but the indexes, are not going up constantly.

    for example i create an control array like this:
    Load ArrayCtl(1)
    Load ArrayCtl(68)
    Load ArrayCtl(23)
    Load ArrayCtl(45)

    what i want is to check if element with given index (23 for example) exists, before i do anything with it.

    so? what do you think?

  2. #2
    Join Date
    Sep 2005
    Posts
    50

    Re: check if element of a control array exists

    I know you're looking for something else, but I would use On Error. You don't have to just resume next if you feel that it is an inelegant solution.

    You can On Error Resume Next and then access a property of a control that may or not be in the array, and then check to see if err.number<>0 - that would tell you that the item does not exist. You could then clear the error & go on to the next one. If err.number=0 then the control exists & you can perform your operation.

    Obviously if you're only performing one operation & don't need to track whether the control exists or not, it would be simpler just to protect from errors and run through them all without regard to whether or not the operation did anything.

  3. #3
    Join Date
    May 2005
    Location
    Sunny Adelaide
    Posts
    66

    Re: check if element of a control array exists

    Code:
    If (Not ArrayCtl(x) Is Nothing) Then
    You are here.

  4. #4
    Join Date
    Dec 2004
    Posts
    35

    Re: check if element of a control array exists

    to bad... it dosent rise an error, but it always returns the same value, no matter if the control exists or not...
    Last edited by mensch; September 6th, 2005 at 03:47 PM.

  5. #5
    Join Date
    Dec 2002
    Location
    London, UK
    Posts
    1,569

    Re: check if element of a control array exists

    try to create the object.. if it gives you an error then an object exists in that location.
    you would use "On Error Resume Next" and "On Error Goto 0".

    Code:
    On Error Resume Next
        Load ArrayCtl(23)
        If (Err.Number <> 0) Then
            'Clear the error
            Err.Clear
            Resume
            'Do what ever you would want if the object did not exist
            ...
            ...
        Else
            'Do what ever you would want if the object did exist
            ...
            ...
        End If
    On Error Goto 0
    You can also simply access the object instead of attempting to create it. The same approach can be used for both.
    Mike

  6. #6
    Join Date
    Dec 2001
    Posts
    6,332

    Re: check if element of a control array exists

    Quote Originally Posted by penagate
    Code:
    If (Not ArrayCtl(x) Is Nothing) Then
    That really looks like it would either work, or raise an error, but it does neither.

    My question is why can't you load and unload in numeric order? That would really solve the problem without using On Error, which I avoid also.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  7. #7
    Join Date
    May 2005
    Location
    Sunny Adelaide
    Posts
    66

    Re: check if element of a control array exists

    Quote Originally Posted by WizBang
    That really looks like it would either work, or raise an error, but it does neither.
    How terribly apathetic.

    I tried various other things, but none of them were able to distinguish between loaded and unloaded array elements. Strange.
    You are here.

  8. #8
    Join Date
    Dec 2001
    Posts
    6,332

    Re: check if element of a control array exists

    I even tried ObjPtr(), and was surprised to see it returning a value for non-existant control array elements.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  9. #9
    Join Date
    May 2005
    Location
    Sunny Adelaide
    Posts
    66

    Re: check if element of a control array exists

    Quote Originally Posted by WizBang
    I even tried ObjPtr(), and was surprised to see it returning a value for non-existant control array elements.
    I tried that too! And VarPtr(). It seems the array is redimensioned and the objects instantiated, but they don't go into the proper "loaded" state. Very strange.
    You are here.

  10. #10
    Join Date
    Apr 2009
    Posts
    2

    Re: check if element of a control array exists

    Using Vartype(Check1(6)) <> 9 seems to work without the need of error handling. When the array element exists it returns apparently the type of the default property of the control while when it does not exist returns 9 (vbObject) without raising a error. But yet very weird.
    Attached Files Attached Files

  11. #11
    Join Date
    Apr 2003
    Posts
    1,755

    Re: check if element of a control array exists

    Using VarType will work IF the control has a default member and that default member returns value other than vbObject. If the control doesn't have a default member, it will always return false.

    One solution is to enumerate the items in the control array and compare the Index of each item.
    Code:
    Function CheckCtrlIndex(ctrlArray As Object, ByVal idx As Integer) As Boolean
       Dim result As Boolean, ctrl As Control
       For Each ctrl In ctrlArray
          If ctrl.Index = idx Then
             result = True
             Exit For
          End If
       Next ctrl
       CheckCtrlIndex = result
    End Function
    Hope it will help you

  12. #12
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: check if element of a control array exists

    Since the question was asked about 4 years ago I doubt the poster is still looking for an answer.

  13. #13
    Join Date
    Apr 2003
    Posts
    1,755

    Re: check if element of a control array exists

    What the ....????? I should have checked the OP, not the latest post

  14. #14
    Join Date
    Apr 2009
    Posts
    2

    Re: check if element of a control array exists

    Yes, my code really don't worked in some situations..
    Hey, the topic has a very functional answer now that helps a lot..
    thanks man

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

Featured