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 :)
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 :-)
Re: Deleting an item from a list
Close :)
VB Code:
List1.RemoveItem (List1.ListIndex)
Re: Deleting an item from a list
Quote:
Originally Posted by lintz
Close :)
VB Code:
List1.RemoveItem (List1.ListIndex)
(tap)(tap)
Ahem! :afrog:
Re: Deleting an item from a list
It works, thanks a lot ^__^
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:
If List1.ListIndex > -1 Then
List1.RemoveItem (List1.ListIndex)
End If
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 :-)
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 :)
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?
Re: Deleting an item from a list
<> is not neccessary... > is enough to determine if it's bigger then -1 :)
VB Code:
Private Sub ShopListItems_Click()
If ShopListItems.ListIndex > -1 And (Price.ListCount > ShopListItems.ListIndex) Then
Price.ListIndex = ShopListItems.ListIndex
End If
End Sub
Re: Deleting an item from a list
Ahh, thanks!
As for my last question, any ideas though? ^_^
Re: Deleting an item from a list
Quote:
Originally Posted by Arby
Ahh, thanks!
As for my last question, any ideas though? ^_^
?? :confused:
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
Re: Deleting an item from a list
Doesn't the code from my reply in post #10 help?
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 ^__^
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 :)
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:
If Price.ListCount > 0 Then 'Price ListBox has at least one item
'your code...
End If
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 :(
Re: Deleting an item from a list
Now it's stopped calculating the totals entierly >_<
http://www.arbital24.com/MakingProg/