[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?
Re: Adding and removing items
How do you currently determine which values get inserted into listbox2 when a checkbox is checked in listbox1
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.
Re: Adding and removing items
VB Code:
Private Sub List1_ItemCheck(Item As Integer)
If List1.Selected(Item) = True Then
List2.AddItem (List1.Text)
Else
List2.ListIndex = 0
Do Until List2.ListIndex > List2.ListCount + 1 Or List2.ListIndex = -1
If List2.Text = List1.Text Then
List2.RemoveItem (List2.ListIndex)
End If
List2.ListIndex = List2.ListIndex + 1
Loop
End If
End Sub
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.
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?
Re: Adding and removing items
I might do this:
VB Code:
Private Sub List1_Click()
Dim lngX As Long
List2.Clear
For lngX = 0 To List1.ListCount - 1
If List1.Selected(lngX) Then
List2.AddItem List1.List(lngX)
End If
Next
End Sub
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.
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.
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.
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.
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.
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.
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?
Re: Adding and removing items
Could you explain again the movement of the data with actual data as sample?
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?
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.
Re: Adding and removing items
Quote:
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:
' Modified BruceG's code [url]http://vbforums.com/showpost.php?p=2780248&postcount=7[/url]
Private Sub List1_Click()
Dim lngX As Long
Dim Flag As Boolean
Flag = True
List2.Clear
For lngX = 0 To List1.ListCount - 1
'I am using only three if conditions because I have added only three items to
'List1. Please modify the code for your original project.
If List1.Selected(lngX) Then
If lngX = 0 Then
'this will ensure that the data is picked from under category1
For i = 1 To 5
List2.AddItem List3.List(i)
Next i
ElseIf lngX = 1 Then
'this will ensure that the data is picked from under category2
For j = 8 To 12
List2.AddItem List3.List(j)
Next
ElseIf lngX = 2 Then
'this will ensure that the data is picked from under category3
For k = 15 To 19
List2.AddItem List3.List(k)
Next
End If
End If
Next
End Sub
'Hack's code from [url]http://vbforums.com/showpost.php?p=2717426&postcount=7[/url]
Private Sub SaveLoadListbox(plstLB As ListBox, pstrFileName As String, _
pstrSaveOrLoad As String)
Dim strListItems As String
Dim i As Long
Select Case pstrSaveOrLoad
Case "save"
Open pstrFileName For Output As #1
For i = 0 To plstLB.ListCount - 1
plstLB.Selected(i) = True
Print #1, plstLB.List(plstLB.ListIndex)
Next
Close #1
Case "load"
plstLB.Clear
Open pstrFileName For Input As #1
While Not EOF(1)
Line Input #1, strListItems
plstLB.AddItem strListItems
Wend
Close #1
End Select
End Sub
Private Sub Form_Load()
Call SaveLoadListbox(List3, "C:\Category.txt", "load")
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Call SaveLoadListbox(List3, "C:\Category.txt", "save")
End Sub
Hope this helps...
edit: Adding the file. Remember to place the category.txt in C:\
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.
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:
Dim i As Long
For i = 0 To List1.ListCount - 1
List1.Selected(i) = False
Next
List1.ListIndex = -1
i put that in the form_load command but it doesnt work. Any ideas?
Re: [RESOLVED] Adding and removing items
Quote:
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.