|
-
Sep 26th, 2006, 06:07 PM
#1
Thread Starter
Addicted Member
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.
-
Sep 26th, 2006, 06:10 PM
#2
PowerPoster
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.
-
Sep 26th, 2006, 06:11 PM
#3
Re: Deleting an item from a list
Close
VB Code:
List1.RemoveItem (List1.ListIndex)
-
Sep 26th, 2006, 06:12 PM
#4
PowerPoster
Re: Deleting an item from a list
 Originally Posted by lintz
Close
VB Code:
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.
-
Sep 26th, 2006, 06:27 PM
#5
Thread Starter
Addicted Member
Re: Deleting an item from a list
It works, thanks a lot ^__^
-
Sep 26th, 2006, 07:03 PM
#6
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
-
Sep 26th, 2006, 07:07 PM
#7
PowerPoster
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.
-
Sep 27th, 2006, 04:03 AM
#8
Thread Starter
Addicted Member
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
-
Sep 27th, 2006, 04:20 AM
#9
Thread Starter
Addicted Member
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?
-
Sep 27th, 2006, 05:39 AM
#10
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
-
Sep 27th, 2006, 05:47 AM
#11
Thread Starter
Addicted Member
Re: Deleting an item from a list
Ahh, thanks!
As for my last question, any ideas though? ^_^
-
Sep 27th, 2006, 05:51 AM
#12
Re: Deleting an item from a list
 Originally Posted by Arby
Ahh, thanks!
As for my last question, any ideas though? ^_^
??
-
Sep 27th, 2006, 05:57 AM
#13
Thread Starter
Addicted Member
Re: [RESOLVED] Deleting an item from a list
 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
-
Sep 27th, 2006, 06:02 AM
#14
Re: Deleting an item from a list
Doesn't the code from my reply in post #10 help?
-
Sep 27th, 2006, 06:03 AM
#15
Thread Starter
Addicted Member
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 ^__^
-
Sep 27th, 2006, 06:29 AM
#16
Thread Starter
Addicted Member
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
-
Sep 27th, 2006, 06:35 AM
#17
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
-
Sep 27th, 2006, 06:42 AM
#18
Thread Starter
Addicted Member
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
-
Sep 27th, 2006, 07:11 AM
#19
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|