Results 1 to 21 of 21

Thread: [RESOLVED] Adding and removing items

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    93

    Resolved [RESOLVED] Adding and removing items

    Hi, with my program, i am trying to make it so when i select a check in a listbox "list1", several items are added to another listbox "list2". If it was ordinary checkboxes i would have no problem, just with the listbox with the style set to checkbox it gets complicated. In list1, there are seven items loaded at form load. And each item in list1, when selected will add several items to list2. When a check in list1 is deselected, the items added by it when it was first selected are removed from list 2. Any ideas on how to do this?

  2. #2
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    376

    Re: Adding and removing items

    How do you currently determine which values get inserted into listbox2 when a checkbox is checked in listbox1

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    93

    Re: Adding and removing items

    im not that far along yet. "I have a dream". I know that i would need to remove the items according to their values, but i have to put them in the list2 first.

  4. #4
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    376

    Re: Adding and removing items

    VB Code:
    1. Private Sub List1_ItemCheck(Item As Integer)
    2.  
    3. If List1.Selected(Item) = True Then
    4.     List2.AddItem (List1.Text)
    5. Else
    6.     List2.ListIndex = 0
    7.     Do Until List2.ListIndex > List2.ListCount + 1 Or List2.ListIndex = -1
    8.         If List2.Text = List1.Text Then
    9.         List2.RemoveItem (List2.ListIndex)
    10.         End If
    11.         List2.ListIndex = List2.ListIndex + 1
    12.     Loop
    13. End If
    14. End Sub

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    93

    Re: Adding and removing items

    thats close to what i want but not quite... With that it adds the text of the check selected. What i want is to be able to load a list of words related to the check. like list1 has flooring, and when i check it, carpet, hardwood,tiles and so on is added to list2. And this has to be done for seven different items in list1.

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

    Re: Adding and removing items

    Quote Originally Posted by stevevb6
    thats close to what i want but not quite... With that it adds the text of the check selected. What i want is to be able to load a list of words related to the check. like list1 has flooring, and when i check it, carpet, hardwood,tiles and so on is added to list2. And this has to be done for seven different items in list1.
    Where is this information being stored?

    Where would your program go to get carpet, hardwood,tiles if Flooring was checked?

  7. #7
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657

    Re: Adding and removing items

    I might do this:
    VB Code:
    1. Private Sub List1_Click()
    2.  
    3.     Dim lngX As Long
    4.  
    5.     List2.Clear
    6.  
    7.     For lngX = 0 To List1.ListCount - 1
    8.         If List1.Selected(lngX) Then
    9.            List2.AddItem List1.List(lngX)
    10.         End If
    11.     Next
    12.  
    13. End Sub
    "It's cold gin time again ..."

    Check out my website here.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    93

    Re: Adding and removing items

    i would like to have the information stored in an invisible listbox, with one list box per item in list1.

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

    Re: Adding and removing items

    Quote Originally Posted by stevevb6
    i would like to have the information stored in an invisible listbox, with one list box per item in list1.
    Thats doable, but what if you need to update the invisible listbox? You would have to recode it, recompile your program, and reroll it out to your users.

    It would be better to store this in a database which you can update without having to rewrite any code.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    93

    Re: Adding and removing items

    I dont need to update code, so im guess that a listbox is the easiest option, The thing is im new to vb6 and cant figure out how to do this. Maybe store it in a txt file? Whatever is easiest.

  11. #11
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Adding and removing items

    Use a listview with two columns instead of two listboxes... that way he can select directly carpet, hardwood,tiles and so on (on columns two), and the grouping info (column 1) can be determined easily.

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    93

    Re: Adding and removing items

    I have not thought about it like that. Its a good idea. Im wondering now if when selected, there are anyways to make the items hardwoord, tiles and ect to go into another list, list3 which is invisible, so i can randomize the items in the list3.

  13. #13
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Adding and removing items

    For what prupose is the randomization? If your just gonna reference the selected items in the listview might as well just maintain an array/collection of the listview indices sorted in random (for array randomize by swapping array index of elements, for collection insert at random index in collection) order since your planning to make list3 invisible anyway, you'll save memory that way.

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    93

    Re: Adding and removing items

    i need it to display a random word from list 3 into a label. I a;readu have that done. I just need to know how i can have it so when i select an item in the lsitbox, it will add items to a different list. Each thing in the list addes a different list. Maybe i should use individual checks? This seems to be pretty complicated. Maybe there is an easier way to do this?

  15. #15
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Adding and removing items

    Could you explain again the movement of the data with actual data as sample?

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    93

    Re: Adding and removing items

    i want to have a list of seven items "flooring", "windows", "paint" ect. When one of these items are selected, they add to a second list subcatorgories, like "hardwood, tiles, carpet" for flooring. "Vinyl, wood, metal," for windows and "orange, green, blue" for paint. Maybe i can have each list in a textfile? So when i select an item in the first list, ie "Flooring" the items in the txt file
    hardwood, tiles, carpet" are loaded in the second list. All the catogories will load the subcatogories into the same list so the second list would look like "hardwood, tiles, carpet, Vinyl, wood, metal , orange, green, blue" if all were selected. If only flooring and paint was selected, the list would be
    "hardwood, tiles, carpet, orange, green, blue" I would need it so when the item is deselected from list one, flooring for example is selected so "hardwood, tiles, carpet" is added to the second list. When flooring is deselected, the items are removed from the second list. Any ideas?

  17. #17
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Adding and removing items

    Rather than moving them around just maintain one listview (masterlist)

    column1 - column2
    flooring - hardwood
    flooring - tiles
    flooring - carpet
    window - vinyl
    window - wood
    window - metal
    paint - orange
    paint - green
    paint - blue

    Listview items can have checkboxes, so you can use that for the selection. Listview can sort on the columns, there are sample codes for that. You can also do a manual sort to "bubble" checked items to the start of the list... you can also use the checked property to transfer listitems to another listview (eg. selected items).

    Forget about synchronizing two listboxes.

  18. #18
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Adding and removing items

    I would like to have the information stored in an invisible listbox,
    Hi

    I think this is what you want. If you would have searched the forums then you would have got it

    Ok do this...

    Create three list boxes say List1, List2 and List3. List3 will be invisible.

    List1 will have three option let's say Option1, Option2 and Option3.

    Create a Text File say Category.txt and copy this data into it and then save it in
    say c:\

    Category1
    item1a
    item1b
    item1c
    item1d
    item1e

    Category2
    item2a
    item2b
    item2c
    item2d
    item2e

    Category3
    item3a
    item3b
    item3c
    item3d
    item3e

    Place this code in the form
    VB Code:
    1. ' Modified BruceG's code [url]http://vbforums.com/showpost.php?p=2780248&postcount=7[/url]
    2. Private Sub List1_Click()
    3.     Dim lngX As Long
    4.     Dim Flag As Boolean
    5.     Flag = True
    6.  
    7.     List2.Clear
    8.    
    9.     For lngX = 0 To List1.ListCount - 1
    10.    
    11.     'I am using only three if conditions because I have added only three items to
    12.     'List1. Please modify the code for your original project.  
    13.  
    14.         If List1.Selected(lngX) Then
    15.             If lngX = 0 Then
    16.                 'this will ensure that the data is picked from under category1
    17.                 For i = 1 To 5
    18.                     List2.AddItem List3.List(i)
    19.                 Next i
    20.             ElseIf lngX = 1 Then
    21.                 'this will ensure that the data is picked from under category2
    22.                 For j = 8 To 12
    23.                     List2.AddItem List3.List(j)
    24.                 Next
    25.             ElseIf lngX = 2 Then
    26.                 'this will ensure that the data is picked from under category3            
    27.                 For k = 15 To 19
    28.                     List2.AddItem List3.List(k)
    29.                 Next
    30.             End If
    31.         End If
    32.     Next
    33. End Sub
    34.  
    35. 'Hack's code from [url]http://vbforums.com/showpost.php?p=2717426&postcount=7[/url]
    36. Private Sub SaveLoadListbox(plstLB As ListBox, pstrFileName As String, _
    37. pstrSaveOrLoad As String)
    38.  
    39. Dim strListItems As String
    40. Dim i As Long
    41.  
    42. Select Case pstrSaveOrLoad
    43.    Case "save"
    44.     Open pstrFileName For Output As #1
    45.     For i = 0 To plstLB.ListCount - 1
    46.         plstLB.Selected(i) = True
    47.         Print #1, plstLB.List(plstLB.ListIndex)
    48.     Next
    49.     Close #1
    50.  
    51.    Case "load"
    52.    plstLB.Clear
    53.     Open pstrFileName For Input As #1
    54.     While Not EOF(1)
    55.       Line Input #1, strListItems
    56.       plstLB.AddItem strListItems
    57.     Wend
    58.     Close #1
    59. End Select
    60.  
    61. End Sub
    62.  
    63. Private Sub Form_Load()
    64. Call SaveLoadListbox(List3, "C:\Category.txt", "load")
    65. End Sub
    66.  
    67. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    68. Call SaveLoadListbox(List3, "C:\Category.txt", "save")
    69. End Sub

    Hope this helps...

    edit: Adding the file. Remember to place the category.txt in C:\
    Last edited by Siddharth Rout; Mar 25th, 2007 at 09:01 AM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  19. #19

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    93

    Re: Adding and removing items

    WOW! That works perfect! Sorry for not searching, i didnt know exactually what to search for. Thank you very very much.

  20. #20

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    93

    Re: [RESOLVED] Adding and removing items

    Just one problem, i want to have all of the list1 in the example you gave me selected at load. I tried
    VB Code:
    1. Dim i As Long
    2.  For i = 0 To List1.ListCount - 1
    3.  List1.Selected(i) = False  
    4. Next  
    5. List1.ListIndex = -1
    i put that in the form_load command but it doesnt work. Any ideas?

  21. #21
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: [RESOLVED] Adding and removing items

    Category1
    item1a
    item1b
    item1c
    item1d
    item1e

    Category2
    item2a
    item2b
    item2c
    item2d
    item2e

    Category3
    item3a
    item3b
    item3c
    item3d
    item3e
    Laying out the data in row,column format such as a CSV would be a lot better since its closer to the DB as data source approach.

    Honestly, you spending too much time on synchronizing/maintaining all those controls, time you could have invested elsewhere in your project, when in the end its just the selections (for further processing) that matter. And can you imagine how that implementation will scale when you increase the categories, sub categories, items count? Have you considered how your gonna implement a search/find feature? What if you need to add another sub-category? You will end up extensively update everything from data access all the way to presentation layer rather than just adding another column and updating data.
    Last edited by leinad31; Feb 19th, 2007 at 12:44 PM.

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