Results 1 to 26 of 26

Thread: [RESOLVED] check box to remove items from list?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    93

    Resolved [RESOLVED] check box to remove items from list?

    I have made this progam that picks random things from two lists and displays them on a form. I was wondering if there is a way i can have it so when i check a check box labled one of the items in one of the lists will not show up anymore.

  2. #2
    Hyperactive Member
    Join Date
    Dec 2005
    Location
    Toronto, Canada
    Posts
    357

    Re: check box to remove items from list?

    How are you displaying the items??? In a listbox, combobox ....????
    Hey... If you found this post helpful please rate it.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    93

    Re: check box to remove items from list?

    im displaying them in a lable.

  4. #4
    Hyperactive Member
    Join Date
    Dec 2005
    Location
    Toronto, Canada
    Posts
    357

    Re: check box to remove items from list?

    Well since you are using a label, it gets complicated.
    1. First you gotta use the InStr function to find where the string you want to remove is at in the labels caption.
    2. Once you get the start number of that string, you use the Left function to take apart everything from before the string you want to remove and assign it to a temporary variable
    3. You can use the Len function to find the length of the string you want to remove and add that to the starting value from Step 2 and then use the Right function to take everything from after the string you want to remove and assign it to another temporary variable.
    4. Now combine the first and the second variable and display in the label.

    I'll try to post the code... but if you can do it on your own... good for you

    Khanjan
    Last edited by khanjan_a2k; Jan 30th, 2007 at 07:16 PM.
    Hey... If you found this post helpful please rate it.

  5. #5
    Hyperactive Member
    Join Date
    Dec 2005
    Location
    Toronto, Canada
    Posts
    357

    Re: check box to remove items from list?

    I think this should work... however I am missing a space somewhere in the final output... anyways this is a sample, you maybe be able to use it...
    VB Code:
    1. Dim strLabel As String
    2.     strLabel = "This is the sample"
    3.     Dim tmp1 As String
    4.     Dim tmp2 As String
    5.     Dim intLen As String
    6.     Dim strSearch As String
    7.     strSearch = "the"
    8.     intLen = InStr(1, strLabel, strSearch)
    9.     tmp1 = Left(strLabel, intLen - 1)
    10.     tmp2 = Right(strLabel, Len(strLabel) - Len(tmp1) - Len(strSearch))
    11.     MsgBox tmp1 & tmp2

    Or you could try loading each and every item into an array and then load it into the label. And if you want to remove an item, just don't display that string array. Its much easier!!!!!!!! Ask if you want a sample
    Hey... If you found this post helpful please rate it.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    93

    Re: check box to remove items from list?

    can i have a smaple of the easier way, im pretty new to this and the first example goes waaaay over my head. thanks for you help though.

  7. #7
    Addicted Member
    Join Date
    Jan 2007
    Location
    England
    Posts
    234

    Re: check box to remove items from list?

    step through that code, it will become clearer

  8. #8

  9. #9
    Hyperactive Member
    Join Date
    Dec 2005
    Location
    Toronto, Canada
    Posts
    357

    Re: check box to remove items from list?

    Well... I did come up with the code but there is a little or big bug in there somewhere... I am losing the count of the arrays in there somewhere.... However, if you don't really care about each of those items throughout the rest of the program and really aren't tracking each item individually, then just use the first piece of code... the second one is if you want to track each item individually....
    Anyways I have attached the second "prototype"... see if you can find the bug... I bet steve will appreciate it

    Khanjan
    Attached Files Attached Files
    Hey... If you found this post helpful please rate it.

  10. #10
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: check box to remove items from list?

    Do you have to use a label to display the array? Why not a Listbox? In any case please tell me the exact steps to reproduce the problem and specifically what the problem is.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    93

    Re: check box to remove items from list?

    Thats a neat little sample, thanks. However it is not really what im looking for. I never really explained it well enough. It is a randomizing program that when you click the "GO!" button, it will pick one word from one list, and one word from another list. The first word will appear in the first label, and the second word in the second label. I want to have a list of check boxes that are labeled the words in the list. ie each word has its own check box, then when the check box is checked, the word it represents is not an option to come up in the respective label. I think that is a much better explanation.

  12. #12
    Hyperactive Member
    Join Date
    Dec 2005
    Location
    Toronto, Canada
    Posts
    357

    Re: check box to remove items from list?

    Quote Originally Posted by stevevb6
    Thats a neat little sample, thanks. However it is not really what im looking for. I never really explained it well enough. It is a randomizing program that when you click the "GO!" button, it will pick one word from one list, and one word from another list. The first word will appear in the first label, and the second word in the second label. I want to have a list of check boxes that are labeled the words in the list. ie each word has its own check box, then when the check box is checked, the word it represents is not an option to come up in the respective label. I think that is a much better explanation.
    I understand everything upto the "in the second label." After that I get a little lost... can you be more specific please.... I know it it frustrating but I really can't help unless I understand you...

    Khanjan
    Hey... If you found this post helpful please rate it.

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    93

    Re: check box to remove items from list?

    There are 2 lists and 2 labels. i'll call them "list a" and "list b". The lists are in the code and the user cannot see them. The program will select one word from "list a" and one word from "list b" and put them in "label a" and "label b". I want to make it so i have a checkbox for each word from "list a" and a check box for each word in "list b", and when the user clicks the checkbox, it disables one word from the list, so it will not appear in the label. i think that should help. if not ill put my whole project there.

  14. #14
    Hyperactive Member
    Join Date
    Dec 2005
    Location
    Toronto, Canada
    Posts
    357

    Re: check box to remove items from list?

    Ok about the lists... I'll tell you what... the easiest and most effiecient way for this is to use a listbox control, change its Style property to 1 - Checkbox... This gives you are checkbox for each of the items in the listbox... The rest... I hope you know how to use a listbox lol...

    Khanjan
    Hey... If you found this post helpful please rate it.

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    93

    Re: check box to remove items from list?

    Thanks, i have an idea and i think it may work. Thanks for all your help.

  16. #16
    Addicted Member
    Join Date
    Jan 2007
    Location
    England
    Posts
    234

    Re: check box to remove items from list?

    could you not use checkbox on change
    then put
    VB Code:
    1. if checkbox.value = true then
    2.   label.text = "Whatever you want to display here"
    3. end if

    or am i missing the point?

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    93

    Re: check box to remove items from list?

    Quote Originally Posted by Mitch_s_s
    could you not use checkbox on change
    then put
    VB Code:
    1. if checkbox.value = true then
    2.   label.text = "Whatever you want to display here"
    3. end if

    or am i missing the point?
    That is similar to the idea i have, however i want it so when the checkbox.value = true and the checkbox is for a certain word in the list, and the word comes up as the label.text, it randomizes again. but i cant get it to work.

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    93

    Re: check box to remove items from list?

    im not really sure what i should do.
    VB Code:
    1. if checkbox.value = true ??????????
    2. if label1.text = "xxxxx" then
    3. Dim i As Integer
    4.    
    5.     Randomize
    6.     i = Rnd(List1.ListCount) * List1.ListCount
    7.     Label4.Caption = List1.List(i)
    8. end if
    im not sure if that is how i should do it. im not sure what i need to put in the area with the "????" or if this is the best way of doing this, or even if this way is possible.
    i also just tried this but it didnt work.
    VB Code:
    1. If chkboxuf1.Value = True Then
    2.     With List1
    3.         .RemoveItem "AAA"
    4.         End With
    5.         End If

    I have the lists load with the form, like this
    VB Code:
    1. Private Sub Form_Load()
    2. With List1
    3.     .AddItem "AAA"
    4.     .AddItem "BBB"
    5.     .AddItem "CCC"
    6.     .AddItem "DDD"
    7.    
    8.     End With
    Last edited by stevevb6; Jan 31st, 2007 at 12:07 PM.

  19. #19
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: check box to remove items from list?

    You can't remove an item from a listview based solely on it's value. You need to know the index of the value. The following does that. I go through the list backward in case there is more than one entry with the same value.

    VB Code:
    1. Dim lngIndex As Long
    2.    
    3.     For lngIndex = List1.ListCount - 1 To 0 Step -1
    4.         If List1.List(lngIndex) = "AAA" Then
    5.             List1.RemoveItem lngIndex
    6.         End If
    7.     Next

  20. #20

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    93

    Re: check box to remove items from list?

    Do i just replace what i have with that?

    VB Code:
    1. If chkboxuf1.Value = True Then
    2.     Dim lngIndex As Long
    3.    
    4.     For lngIndex = List1.ListCount - 1 To 0 Step -1
    5.         If List1.List(lngIndex) = "AAA" Then
    6.             List1.RemoveItem lngIndex
    7.         End If
    8.     Next

  21. #21

  22. #22

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    93

    Re: check box to remove items from list?

    it isnt going to work for what i want it for. that code removes the item from the list, and when the checkbox is unchecked, it does not come back. i need something to make the program randomize again when the label1.caption = the caption of the check box, only when the checkbox.value = true.
    Last edited by stevevb6; Jan 31st, 2007 at 01:11 PM.

  23. #23

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    93

    Re: check box to remove items from list?

    how can i add an item to the list by clicking the check box,
    like this?
    VB Code:
    1. If chkboxuf1.Value = True Then
    2.       Dim lngIndex As Long
    3.     End If
    4.     For lngIndex = List1.ListCount - 1 To 0 Step -1
    5.         If List1.List(lngIndex) = "UF1" Then
    6.             List1.AddItem lngIndex
    7.                End If
    8.     Next

  24. #24
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: check box to remove items from list?

    Try something like this
    VB Code:
    1. Option Explict
    2. mstrSave As String
    3.  
    4.  
    5. If chkboxuf1.Value = True Then
    6.     Dim lngIndex As Long
    7.    
    8.     For lngIndex = List1.ListCount - 1 To 0 Step -1
    9.         If List1.List(lngIndex) = "AAA" Then
    10.             [HL="#FFFF80"]mstrSave = List1.List(lngIndex).List[/HL]
    11.             List1.RemoveItem lngIndex
    12.         End If
    13.     Next

    And then in the Click sub

    List1.AddItem mstrSave

  25. #25

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    93

    Re: check box to remove items from list?

    http://hx.pri.ee/dsrc/dice.php
    this is actually very close to the program i want to make. It is for a game that i play, some guy wanted it on the forum of the site and i figured it is a good way to get some vb6 skills.
    Last edited by stevevb6; Jan 31st, 2007 at 03:49 PM.

  26. #26

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    93

    Re: check box to remove items from list?

    i got this one on my own. i used this code.
    VB Code:
    1. Dim s As Integer
    2.  
    3. For s = 0 To List1.ListCount - 1
    4. If List1.Selected(s) Then
    5. List3.AddItem List1.List(s)
    6. End If
    7. Next
    And then
    VB Code:
    1. Dim i As Integer
    2.    
    3.     Randomize
    4.     i = Rnd(List1.ListCount) * List3.ListCount
    5.     Label4.Caption = List3.List(i)
    Ironed out a few wrinckles, and all is well. Thanks for all your help!
    Last edited by stevevb6; Feb 2nd, 2007 at 08:36 AM.

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