Results 1 to 16 of 16

Thread: [RESOLVED] Listbox refresh

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    11

    Resolved [RESOLVED] Listbox refresh

    Hy everyone,I'm new here and also I'm VB6 newbie who needs some help.
    I'm working at a project with 2 listboxes,when I select a item from a listbox I want few items from the second listbox to dissappear,this worked by removeitem method.
    Anyway after selecting another option I get Runtime error 5 & refresh command doesn't restore initial items.

    Screenshots:
    Intially:

    Selecting second option:

    Clicking on first option to restore intially listbox items-nothing happens:



    Code:
    Private Sub lstDB_Click()
        Select Case lstDB.Text
        Case "All"
            lstCid.Refresh
        Case "2000"
            lstCid.RemoveItem 8
            lstCid.RemoveItem 7
            lstCid.RemoveItem 6
            lstCid.RemoveItem 5
        Case "2000 PDA/Smartphone"
            lstCid.RemoveItem 2
            lstCid.RemoveItem 3
        End Select
    Hope I'm clear enough!Thanks!

  2. #2
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Listbox refresh

    Once you remove the items,they're gone forever. If you want them back, you would have to add them again. I suggest adding them in code if you're not doing it already. Put it in a sub and you can call the sub under Case "All" and it will re-populate the listbox. Don't forget to clear it in the first step.
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Listbox refresh

    Also, when you remove items from a listbox the listindex for the remaining listbox items change....something to keep in mind when hardcoding listindex numbers.

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

    Re: Listbox refresh

    Welcome aboard!

    Removing several items from a list box that may have to be added back to that list box means that you have to store the removed items in an array. It gets even more complicated when more items are removed later with other events. As Hack has implied, adding those items back to the list where they were removed in some sort of hierarchical order will likely drive you bonkers.

    Things can be simplified somewhat if the second list being operated on by the first one is a sorted list so that as removed items are added back, they are automatically sorted within it. Give that a try to maintain your sanity.
    Doctor Ed

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    11

    Re: Listbox refresh

    Quote Originally Posted by MarMan View Post
    Once you remove the items,they're gone forever. If you want them back, you would have to add them again. I suggest adding them in code if you're not doing it already. Put it in a sub and you can call the sub under Case "All" and it will re-populate the listbox. Don't forget to clear it in the first step.
    I've added them in code and it's OK now.Thanks

    There is a sample:
    Code:
    Private Sub lstDB_Click()
        Select Case lstDB.Text
        Case "All"
            lstCid.Clear
            With lstCid
                .AddItem "All"
                .AddItem "Item 1"
                .AddItem "Item 2"
                .AddItem "Item 3"
            End With
        Case "2000"
            lstCid.Clear
            With lstCid
                .AddItem "All"
                .AddItem "Item 1"
                .AddItem "Item 2"
                .AddItem "Item 3"
            End With
        Case "2000 PDA/Smartphone"
            lstCid.Clear
            With lstCid
                .AddItem "All"
            End With
        Case "2001"
            lstCid.Clear
            With lstCid
                .AddItem "All"
            End With
        Case "2010"
            lstCid.Clear
            With lstCid
                .AddItem "All"
                .AddItem "Item 1"
                .AddItem "Item 2"
                .AddItem "Item 3"
            End With
        Case "2012"
            lstCid.Clear
            With lstCid
                .AddItem "All"
                .AddItem "Item 1"
                .AddItem "Item 2"
                .AddItem "Item 3"
            End With
        Case "3200"
            lstCid.Clear
            With lstCid
                .AddItem "All"
                .AddItem "Item 1"
                .AddItem "Item 2"
                .AddItem "Item 3"
            End With
        Case "PNX5230"
            lstCid.Clear
            With lstCid
                .AddItem "All"
                .AddItem "Item 1"
                .AddItem "Item 2"
                .AddItem "Item 3"
            End With
        Case "ODM"
            lstCid.Clear
            With lstCid
                .AddItem "All"
            End With
        End Select
    End Sub
    
    Private Sub lstDB_GotFocus()
        With lstDB
             .AddItem "All"
            .AddItem 2000
            .AddItem "2000 PDA/Smartphone"
            .AddItem 2001
            .AddItem 2010
            .AddItem 2012
            .AddItem 2020
            .AddItem 3150
            .AddItem 3200
            .AddItem 3210
            .AddItem 3350
            .AddItem "PNX5230"
            .AddItem "AVR / DB1000"
            .AddItem "ODM"
        End With
    End Sub
    Now I've added a third listbox where I want to appear items depending of what I selected in first & second listbox.For example if I Select "All" from first listbox and "Item 1" from second listbox I want some items to appear in third listbox,if I select "All" from first listbox and "Item 2" form second listbox I want others items to appear in the third listbox.I need some advice for doing this in a simpe way.Using select case and if?


    Thanks!

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

    Re: Listbox refresh

    Build a form with three list boxes. Set the MultiSelect property of the first two to 1-Simple. Then apply this code:
    Code:
    Private Sub Form_Load()
    'Sample Data
    For I = 1 To 50
        List1.AddItem I
        List2.AddItem Chr$(I + 32)
    Next
    End Sub
    
    Private Sub List1_Click()
    PopList3
    End Sub
    
    Private Sub List2_Click()
    PopList3
    End Sub
    
    Public Sub PopList3()
    List3.Clear
    For I = 0 To List1.ListCount - 1
        If List1.Selected(I) = True Then List3.AddItem List1.List(I)
    Next
    For I = 0 To List2.ListCount - 1
        If List2.Selected(I) = True Then List3.AddItem List2.List(I)
    Next
    End Sub
    As you select items from either of the first two, the third list box will contain all of the selected items.
    Doctor Ed

  7. #7

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    11

    Re: Listbox refresh

    Quote Originally Posted by Code Doc View Post
    Build a form with three list boxes. Set the MultiSelect property of the first two to 1-Simple. Then apply this code:
    Code:
    Private Sub Form_Load()
    'Sample Data
    For I = 1 To 50
        List1.AddItem I
        List2.AddItem Chr$(I + 32)
    Next
    End Sub
    
    Private Sub List1_Click()
    PopList3
    End Sub
    
    Private Sub List2_Click()
    PopList3
    End Sub
    
    Public Sub PopList3()
    List3.Clear
    For I = 0 To List1.ListCount - 1
        If List1.Selected(I) = True Then List3.AddItem List1.List(I)
    Next
    For I = 0 To List2.ListCount - 1
        If List2.Selected(I) = True Then List3.AddItem List2.List(I)
    Next
    End Sub
    As you select items from either of the first two, the third list box will contain all of the selected items.
    Thanks but the third listbox should contain different items than the first & second ones,not the selected items from these.Other ideas?

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

    Re: Listbox refresh

    I guess I misread your earlier post. Here's what you said in post #5:

    "Now I've added a third listbox where I want to appear items depending of what I selected in first & second list box."

    That's the code that I wrote. So try again. What determines what items appear in the third list box?
    Doctor Ed

  9. #9
    Junior Member
    Join Date
    Aug 2010
    Posts
    30

    Re: Listbox refresh

    hmm,try using timer i think...

  10. #10

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    11

    Re: Listbox refresh

    Quote Originally Posted by Code Doc View Post
    I guess I misread your earlier post. Here's what you said in post #5:

    "Now I've added a third listbox where I want to appear items depending of what I selected in first & second list box."

    That's the code that I wrote. So try again. What determines what items appear in the third list box?
    For example:
    first listbox items:
    All
    Nokia
    Motorola
    Sony Ericsson


    second listbox items:
    All
    Low-end
    Middle-end
    High-end


    In the third listbox I want to appear some items based on what I choose in the first and second listboxes.
    For example in third listbox should appear items:
    1100
    1101
    others

    only if Nokia is selected from first listbox and Low-end from second listbox.
    This is what i mean,hope the examples are clear.

  11. #11
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Listbox refresh

    Well you can put the code in s sub to fill the third listbox and call it from the click events from either list boxes:
    Code:
        Sub Fill3rdListBox
    
                If lstDB.SelectedIndex > -1 And lstCid.SelectedIndex > -1 Then
                    Select Case lstDB.Items(lstDB.SelectedIndex)
                        Case "Nokia"
                            Select Case lstCid.Items(lstCid.SelectedIndex)
                                Case "Low-end"
                                Case "Middle-end"
                                Case "High-end"
                            End Select
                    End Select
                End If
    
        End Sub
    You get the idea?
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  12. #12

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    11

    Re: Listbox refresh

    Quote Originally Posted by MarMan View Post
    Well you can put the code in s sub to fill the third listbox and call it from the click events from either list boxes:
    Code:
        Sub Fill3rdListBox
    
                If lstDB.SelectedIndex > -1 And lstCid.SelectedIndex > -1 Then
                    Select Case lstDB.Items(lstDB.SelectedIndex)
                        Case "Nokia"
                            Select Case lstCid.Items(lstCid.SelectedIndex)
                                Case "Low-end"
                                Case "Middle-end"
                                Case "High-end"
                            End Select
                    End Select
                End If
    
        End Sub
    You get the idea?
    Not really...



    Uploaded with ImageShack.us

  13. #13
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Listbox refresh

    I incorrectly posted .NET code. Modify it for VB6. Instead of lst.SelectedIndex change it to ListIndex I believe. Its been awhile since I used VB6. But its main purpose is to illustrate a concept. Apply the concept to your code and like always, pot questions.
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  14. #14

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    11

    Re: Listbox refresh

    Quote Originally Posted by MarMan View Post
    I incorrectly posted .NET code. Modify it for VB6. Instead of lst.SelectedIndex change it to ListIndex I believe. Its been awhile since I used VB6. But its main purpose is to illustrate a concept. Apply the concept to your code and like always, pot questions.
    Modified:
    Code:
    Private Sub lstcid_Click()
                    If lstDB.ListIndex > -1 And lstCid.ListIndex > -1 Then
                    Select Case lstDB.List(lstDB.ListIndex)
                        Case "Nokia"
                            Select Case lstCid.List(lstCid.ListIndex)
                                Case "Low-end"
                                    lstPhones.Clear
                                    lstPhones.AddItem "1100"
                                    lstPhones.AddItem "1101"
                                    lstPhones.AddItem "1110"
                                Case "Middle-end"
                                    lstPhones.Clear
                                    lstPhones.AddItem "5200"
                                Case "High-end"
                                   'Will be added
                            End Select
                    End Select
                End If
    
    End Sub
    Thanks again,it seems to work,post rated!
    If I have others questions can I post them here or I should start a new topic?

  15. #15
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Listbox refresh

    Unless the new questions are explicitly extensions of the original question in this thread, you should create a new thread - it saves time and confusion for many people.


    If your question for this thread has been answered to your satisfaction, please mark the thread as Resolved.
    (it saves time reading for those of us who like to answer questions, and also helps those who search to find answers)

    You can do it by clicking on "Thread tools" just above the first post in this thread, then "Mark thread resolved". (like various other features of this site, you need JavaScript enabled in your browser for this to work).

  16. #16

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    11

    Re: Listbox refresh

    Quote Originally Posted by si_the_geek View Post
    Unless the new questions are explicitly extensions of the original question in this thread, you should create a new thread - it saves time and confusion for many people.


    If your question for this thread has been answered to your satisfaction, please mark the thread as Resolved.
    (it saves time reading for those of us who like to answer questions, and also helps those who search to find answers)

    You can do it by clicking on "Thread tools" just above the first post in this thread, then "Mark thread resolved". (like various other features of this site, you need JavaScript enabled in your browser for this to work).
    Done!

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