Results 1 to 19 of 19

Thread: Deleting an item from a list

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    Deleting an item from a list

    Hi all,

    I'm currently writing a program where you can add items to a listbox, and there is a button to delete the highlighted item. I have tried to do it similar to:

    Code:
    Private Sub Delete_Click()
    Dim DelMe as string
    
    DelMe = List1.selected
    List1.remove(DelMe)
    End Sub
    Well, that's really the pseudocode for what I'm trying to do. Obviously it won't work, since I must specify an index. However, I will not know the index, since it depends on which item the user clicks.

    How do I do this?

    Thanks
    Last edited by BubbleLife; Sep 27th, 2006 at 06:25 AM.

  2. #2
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: Deleting an item from a list

    "delme = List1.ListIndex" will give you the index for the item you've clicked

    and I believe "List1.RemoveItem delme" will do what you want...not sure, this is offhand

    Most of us, when we're not sure, type the "list1." and look through the possibilities...it's removeSOMETHING I am sure :-)
    Well, everyone else has been doing it :-)
    Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
    Expect more to come in future
    If I have helped you, RATE ME! :-)

    I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!

    And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.

  3. #3
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: Deleting an item from a list

    Close

    VB Code:
    1. List1.RemoveItem (List1.ListIndex)

  4. #4
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: Deleting an item from a list

    Quote Originally Posted by lintz
    Close

    VB Code:
    1. List1.RemoveItem (List1.ListIndex)
    (tap)(tap)

    Ahem!
    Well, everyone else has been doing it :-)
    Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
    Expect more to come in future
    If I have helped you, RATE ME! :-)

    I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!

    And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    Re: Deleting an item from a list

    It works, thanks a lot ^__^

  6. #6
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: [RESOLVED] Deleting an item from a list

    Bear in mind that this will produce an error if .ListIndex will equal -1 (that is when non of the list items is selected - usually on load, if ListBox is not focused). This can be prevented:
    VB Code:
    1. If List1.ListIndex > -1 Then
    2.     List1.RemoveItem (List1.ListIndex)
    3. End If

  7. #7
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: [RESOLVED] Deleting an item from a list

    I never thought of that :-)

    Well, I never do...I rarely ever put in error protection code unless I notice a possible error point, which is rarely if ever...I'm lazy :-)
    Well, everyone else has been doing it :-)
    Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
    Expect more to come in future
    If I have helped you, RATE ME! :-)

    I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!

    And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    Re: [RESOLVED] Deleting an item from a list

    Hehe, gavio - I figured that out after putting the code in.
    I'm quite big on validation, so when I put in code I like to test it and the extremes of data.. In this case I was wondering what would happen if nothing was selected, so I tried it and it gave an error. So I had it MsgBox back the listindex, which wa s-1, so I just put it in a If List1.ListIndex <> -1 if ^_^

    Thanks for the advice though

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    Re: [RESOLVED] Deleting an item from a list

    Oh, another quick question about the same program

    I have 3 ListBoxes, and I want it so that when the user clicks an item in one, it highlights the equiv. in all 3. This is the code I tried, to give you an idea what's going on:

    Code:
    Private Sub ShopListItems_Click()
    Dim LI As Integer
    
    LI = ShopListItems.ListIndex()
    Price.Selected (LI)
    End Sub
    "ShopListItems" is the main listbox, and "Price" is the second one. I also tried just having: "Price.Selected(ShopListItems.ListIndex)", but that didn't work either. Any ideas?

  10. #10
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Deleting an item from a list

    <> is not neccessary... > is enough to determine if it's bigger then -1
    VB Code:
    1. Private Sub ShopListItems_Click()
    2.     If ShopListItems.ListIndex > -1 And (Price.ListCount > ShopListItems.ListIndex) Then
    3.         Price.ListIndex = ShopListItems.ListIndex
    4.     End If
    5. End Sub

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    Re: Deleting an item from a list

    Ahh, thanks!
    As for my last question, any ideas though? ^_^

  12. #12
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Deleting an item from a list

    Quote Originally Posted by Arby
    Ahh, thanks!
    As for my last question, any ideas though? ^_^
    ??

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    Re: [RESOLVED] Deleting an item from a list

    Quote Originally Posted by Arby
    Oh, another quick question about the same program

    I have 3 ListBoxes, and I want it so that when the user clicks an item in one, it highlights the equiv. in all 3. This is the code I tried, to give you an idea what's going on:

    Code:
    Private Sub ShopListItems_Click()
    Dim LI As Integer
    
    LI = ShopListItems.ListIndex()
    Price.Selected (LI)
    End Sub
    "ShopListItems" is the main listbox, and "Price" is the second one. I also tried just having: "Price.Selected(ShopListItems.ListIndex)", but that didn't work either. Any ideas?
    This one

  14. #14

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    Re: Deleting an item from a list

    *blinks* How did I manage to miss that? XD

    Sorry, I missed it. Works fine! Thanks a lot ^__^

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    Re: Deleting an item from a list

    Two more little problems:

    1. I have a delete button to delete the entries from all ListBoxes. When this is clicked, I want focus to return to the top of the listbox (ListIndex 0, I guess). I have tried:
    Code:
    If ShopListItems.ListIndex <> -1 Then
        ShopListItems.Selected (0)
    End If
    and
    [code]
    If ShopListItems.ListIndex <> -1 Then
    ShopListItems.ListIndex = 0
    End If
    With no luck. Any ideas?

    And 2:
    I have some code which adds up all the values in the "Price" listbox to get a total. The code is:

    Code:
    MaxList = Price.ListCount
    Rerun:
    Price.ListIndex = ValsRepeat
    CalcSub = CalcSub + Price.List(ValsRepeat)
    ValsRepeat = ValsRepeat + 1
    If ValsRepeat > MaxList - 1 Then
        GoTo Now1
    End If
    GoTo Rerun
    
    Now1:
    However, I find that if I delete ALL items on the listbox, and then try to add a new item, on the line:
    Price.ListIndex = ValsRepeat

    I get an error. I can't seem to sort this one out, any ideas?

    Thanks!


    EDIT:My program so far, In case you need the code.
    I know there are other problems with it, like the form not refreshing when items are deleted. I will sort this out soon

  17. #17
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Deleting an item from a list

    When you delete all items from a ListBox, .ListIndex property equals -1. Perhaps you can solve this with a condition:
    VB Code:
    1. If Price.ListCount > 0 Then 'Price ListBox has at least one item
    2.     'your code...
    3. End If

  18. #18

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    Re: Deleting an item from a list

    Well, it's in a seperate form. But If I do that, then if there are no items in the listbox, it will not add anything to the listbox

  19. #19

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    Re: Deleting an item from a list

    Now it's stopped calculating the totals entierly >_<
    http://www.arbital24.com/MakingProg/

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