Results 1 to 12 of 12

Thread: Listbox Array problem

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Listbox Array problem

    I have nn listboxes (List2) indexed 1, 2, 3, 4, 5,...,etc etc (index 0 not part of this)

    I want to click on any item in List2 that currently is shown (all boxes in the array are one behind the other) and have it display the next listbox. It's fairly simple to do as long as all listboxes are still loaded. However any one or more of the listboxes can be unloaded and this is what is causing a problem. There is another listbox (List1) that keeps track of which listboxes are loaded and which are unloaded. If List2(5), for example, is unloaded then List1.List(4) will be unchecked otherwise List1.List(4) is checked

    Below is code that I had which worked perfectly as long as all boxes still loaded

    Code:
    Private Sub List2_Click(Index As Integer)
       '
       '
     If Index + 1 > List2.Count - 1 Then Index = 0
     List2(Index + 1).ZOrder 0
       '
       '
    End Sub
    Now I had to modify the above code because it crashed on an Array not loaded error so the below code is how I modified it

    Code:
    Private Sub List2_Click(Index As Integer)
       '
       '
    If Index + 1 > List2.Count - 1 Then Index = 0
     
    If Not List1.Selected(Index) Then
      Index = Index + 1
      If Index > List2.Count - 1 Then Index = 0
    End If
     
     
    List2(Index + 1).ZOrder 0
       '
       '
    End Sub
    But it doesn't work. I need it to work like below:

    1) Click on any list item in the currently shown listbox

    2) Show the next listbox in the array

    3) If the next listbox in the array is unloaded then skip to the next listbox after that

    4) If next listbox afte that is also unloaded then skip to next. etc


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  2. #2
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Listbox Array problem

    Pardon my ignorance, but I have never heard of a "loaded" or "unloaded" list box unless the list boxes are on separate forms. Is that the case here, or are you talking about the list box's visibility, having the focus, or something else?
    Doctor Ed

  3. #3
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: Listbox Array problem

    I am a bit confused on how to replicate your situation. But you could always add an extra function to check if array does exist, so if it does error in the extra function you return the function as true or false if error.
    This way your main function will not error out, instead your extra function will return True (for good "Loaded") or False (for error "Unloaded").

    As for the other questions your asking I'm not 100% sure.

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Listbox Array problem

    Quote Originally Posted by Code Doc View Post
    Pardon my ignorance, but I have never heard of a "loaded" or "unloaded" list box unless the list boxes are on separate forms. Is that the case here, or are you talking about the list box's visibility, having the focus, or something else?
    I think he is using dynamic controls loading additional list into the control array and then unloading them.

    Not sure why there would be so many lists though, if only one will ever be visible I think i would use a different method and only 1 list.

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Listbox Array problem

    The loading and unloading has to do with whether a server is running or the server has disconnected.

    When a server connects to the master server a listbox is loaded and when that server disconnects the listbox is unloaded. I use an array because I want a listbox for each server to hold the list of users on that server. So, if there are 10 servers running then there are 10 listboxes loaded. Each listbox contains the list of users on any particular server. Only one listbox shows at a time simply because I don't want a bunch of them side by side because the master server has a small UI so it's a matter of space. If I want to look at all the listboxes to see what users are on which server I click on the listbox to show the next listbox and this goes on in a cycle from List2(1) to List2(nn) and then starts over again. As long as all servers are running I don't have a problem (Ie as shown in my 1st code snippet which works perfectly). Now if for example server 5 is no longer running I unload the listbox that corresponds the server 5 which is List2(5). Now when there is one or more servers not running my code in example 1 won't work because that logic trys to show List2(5) which no longer exists so I get an array element does not exist error. So, I tried to modify the code as shown in the 2nd code snippet but it doesn't work. I have made other attempts but so far I have not been successful. I can catch whether a server is running or not it's just that I can't seem to get the logic correct so that it cycles through array skipping over any listbox that no longer exists and go to the next one. I can get it to work up to a point then it starts behaving wrong and it wont show a listbiox that does exists. Here's another code snippet I tried and it worked for awhile then it too failed when two servers disconnected.

    Code:
     Do While True
       If (Index + 1) > List2.Count Then Index = 0
       
       If Not List1.Selected(Index) Then
         Index = Index + 1
       Else
         Exit Do
       End If
     Loop
    
     List2(Index + 1).ZOrder 0


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  6. #6
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: Listbox Array problem

    First off, I would use arrays instead of listboxes.

    Since you are asking with listboxes try this for a test.

    Add 2 textbox any where any size to an empty form (new empty project)

    "Text1" - NOT ARRAY
    "Text2(0)" - ARRAY

    Add this code:
    Code:
    Private Function ListboxExist(List_Box As ListBox) As Boolean
    On Error GoTo ArrayError
    
      If List_Box.ListCount >= 0 Then ListboxExist = True
      Exit Function
      
    ArrayError:
      ListboxExist = False
    End Function
    
    Private Sub Form_Load()
    Dim i As Integer
    
      Me.Move 3000, 3000, 4000, 3800
      List1.Move 200, 100, 1500, 3000
      List2(0).Move 2000, 100, 1500, 3000
      List2(0).AddItem "Template"
      
      For i = 1 To 100
        Load List2(i)
          With List2(0) 'Template
            List2(i).Move .Left, .Top, .Width, .Height
            List2(i).Visible = True
            List2(i).AddItem "New Listbox" & i
          End With
        List1.AddItem "Server" & i
      Next
      
      Unload List2(5) 'Let's assume Server5 disconnected
    End Sub
    
    Private Sub List1_Click()
    Dim Li As Integer 'List index
      Li = (List1.ListIndex + 1)
        If ListboxExist(List2(Li)) Then List2(Li).ZOrder 0 Else List2(0).ZOrder 0
    End Sub
    Now Run project. Click on the first listbox items 1-5, the 5th item should normally error out.
    If you check with a custom function if the listbox exists, then it will not error out. (See function ListboxExist)

    This would be one way.


    I would recommend to re-think your User Interface.
    Use arrays instead of Listboxes.(less memory)
    Arrays will have the ability to handle (store) more users then a Listbox can and will be faster.

    With array (even possible with listbox) you could ReDim Preserve and always transfer the server/users over (when unloaded) so the array is never empty the empty ones will move to the end and get erased! This would you would need to keep track of your users, and Server Number (#).

  7. #7
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Listbox Array problem

    Quote Originally Posted by Max187Boucher View Post
    First off, I would use arrays instead of listboxes.
    Max: This has already been suggested in another thread and OP doesn't to want to use an Array.

    @JM: Have you thought about establishing a Do Loop with in-line Error Handling to trap the "Control array element 'n' doesn't exist" (340) error and incrementing Index until you don't get the error?
    Last edited by Doogle; Mar 10th, 2013 at 04:19 AM.

  8. #8
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Listbox Array problem

    Here's an example of what I mean
    Code:
    Private Sub Form_Load()
    '
    ' Assumes List2(1) is drawn on the Form
    '
    Dim intI As Integer
    List2(1).AddItem "List2(1)"
    For intI = 2 To 10 Step 2
        Load List2(intI)
        List2(intI).Top = List2(1).Top   ' Make sure that each
        List2(intI).Left = List2(1).Left ' ListBox is on top of element 1
        List2(intI).AddItem "List2(" & CStr(intI) & ")"
        List2(intI).Visible = True
    Next intI
    End Sub
    
    Private Sub List2_Click(Index As Integer)
    Dim intI As Integer
    Dim intErr As Integer
    If List2.Count > 1 Then    ' Change this to 2 if there's an element 0 that's not involved
        intI = Index
        Do
            intI = intI + 1
            If intI > List2.UBound Then intI = List2.lbound ' If there is an element 0 
                                                            ' and you don't want it involved
                                                            ' then intI = List2.lbound + 1
            On Error Resume Next
            List2(intI).ZOrder 0
            intErr = Err.Number ' No need to check the value of the error
                                ' since the only eror that could be raised
                                ' by setting the ZOrder is 340
            On Error GoTo 0
        Loop Until intErr = 0
    End If
    End Sub
    Clicking on a ListBox item will cause the next loaded element of the ListBox Control Array to be displayed, clicking on an item in the last loaded element will cause the first loaded element to be displayed. Does nothing if there's only 1 loaded element.

    EDIT:
    If you don't want the selected items to remain hilighted you can add this at the top of the Click event after the 'Dim' statements:
    Code:
    Static binNoEntry As Boolean
    binNoEntry = Not binNoEntry
    If Not binNoEntry Then Exit Sub
    List2(Index).Selected(List2(Index).ListIndex) = False
    Last edited by Doogle; Mar 10th, 2013 at 06:33 AM. Reason: Added some comments

  9. #9
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Listbox Array problem

    This can be done without error handling.
    Code:
    Option Explicit
    
    Private Sub Form_Load()
        ' default listbox
        List2(1).AddItem "List2(1)"
        
        ' add a few more for testing
        Load List2(5)
        List2(5).Visible = True
        List2(5).AddItem "List2(5)"
    
        Load List2(4)
        List2(4).Visible = True
        List2(4).AddItem "List2(4)"
    
        Load List2(7)
        List2(7).Visible = True
        List2(7).AddItem "List2(7)"
    
        Load List2(2)
        List2(2).Visible = True
        List2(2).AddItem "List2(2)"
        
    End Sub
    
    Private Sub List2_Click(Index As Integer)
    Dim ub As Integer
    Dim lb As Integer
    Dim i As Integer
        
        lb = List2.LBound
        ub = List2.UBound
        
        ' clear the selection in the current listbox
        If List2(Index).ListIndex > -1 Then
            List2(Index).ListIndex = -1
        End If
        
        If Index = ub Then
            ' If at the ubound of the array then start over
            List2(lb).ZOrder 0
        Else
            ' if not the ubound then loop through the array to find the next index
            For i = Index + 1 To ub
                If ControlIndexExists(List2, i) Then
                    List2(i).ZOrder 0
                    Exit For
                End If
            Next i
        End If
        
    End Sub
    
    Private Function ControlIndexExists(CtlArray As Object, ByVal Index As Integer) As Boolean
        ControlIndexExists = (VarType(CtlArray(Index)) <> vbObject)
    End Function

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Listbox Array problem

    Quote Originally Posted by Doogle View Post
    Max: This has already been suggested in another thread and OP doesn't to want to use an Array.

    @JM: Have you thought about establishing a Do Loop with in-line Error Handling to trap the "Control array element 'n' doesn't exist" (340) error and incrementing Index until you don't get the error?
    Yes. I made a slight modification and added a Do....Loop and allowed the error to take place using On Error GoTo but even then when I incremented the index to the next control and looped back the problem then was that it totally skipped the last control in the array.

    I have decided to do away with this checking for an unloaded control. Now, I just use .Clear instead of Unload. This way I can use my original two-line code to flip through the control array and when a server has disconnected I just have a blank Listbox. I thought Loading and Unloading was the better way to go but now I don't think that and having a blank Listbox is OK and it saves a lot of extra coding.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  11. #11
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Listbox Array problem

    Did my code not do what you asked for?

  12. #12

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Listbox Array problem

    Quote Originally Posted by MarkT View Post
    Did my code not do what you asked for?
    It works exactly the way I wanted it. Thanks, Mark. Now, the only thing is for me to make up my mind whether I want to Load and Unload listboxes or just clear them out based on if servers are running or not.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

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