I am populating a listbox from an array. What I want is that when somebody clicks on an item on the listbox and then hits the delete button that it deletes that value in the array and in the listbox. How do I do that?
Thank You!
Printable View
I am populating a listbox from an array. What I want is that when somebody clicks on an item on the listbox and then hits the delete button that it deletes that value in the array and in the listbox. How do I do that?
Thank You!
Set the value to (Nothing)
That's a good quick solution, but you could run into runtime errors if you try to loop through the array (because it has a null value in it). You could right a loop to move every item after the deleted one back by one index to avoid this.
Note: This might not be fully accurate. I'd know for sure in C, but I'm still learning some of the finer (or not so fine) details of VB...
ZaNi, we already have two obsessive Aussies (JM and Penagate) on this forum, are you going to be a third? Well, good.
I think the best solution depends on the situation. If the array is of numbers, perhaps rather than setting it to Nothing, you could set it to an out of range value. For instance, if all the values are positive integers, then set it to -1. You can clean it up later, the -1 is just a placeholder for the time being. If you are using strings, then perhaps the empty string "" will do.
However, you might also consider how the listbox and the array will be used. Is it essential that they remain synched? Could several things be removed from the listbox before the array is used again? If so, the solution might be to simply wait until deletions have completed, then recreate the array from the listbox, rather than deleting each item as it comes up.
If you have to actually delete the items from the array, you will have the advantage that ListItem x will mean delete array(x). In that case, start from index x+1, copying Array(x+1) to Array(x) until x+1 > array indexMax. Hmmm, I was thinking that Array(x) = Array(x+1) wouldn't work for reference types, but it seems now that it should.
I believe setting objects to Nothing is dangerous because it can cause null reference exceptions... This is important to be aware of more than anything else. I see a major possibility in someone deciding to set things to nothing, then deciding to handle the exceptions that arise by putting an empty try/catch combination... This is a lazy and inefficient solution (I think you'll agree there).
So all of it loops back to good programming practice, and I believe it is good practice to not have null references as much as possible (I group it with "goto").
Thank you for noticing that I'm obsessive :) I've always believed I am, but I don't hear it often enough to confirm it (and I sleep a lot, so people think I'm lazy, which contradicts the obsessive paradigm).
Now I just have to wait for someone to notice that I'm neurotic :)
If possible I'd suggest using a collection instead of an array for the reason ZaNi quotes. Use a StringCollection, ArrayList, List or whatever is most appropriate.
Now we have two in one thread. Well, if you are as productive as JM and Penagate, there won't be much need of the rest of us around here.
To get back to the original question, which version of VS are you using? There are better ArrayList options in 2005. I'm not all that enamored of it in 2003, not for any solid reason, it just doesn't "feel" as type safe to me.
Just to make it three:
Why do you need to maintain your own array in the first place? You've already got the list of items in the listbox, always up to date. Maybe you could elaborate on the reasons behind what you're trying to achieve?
As above ^^ the dropdown list is an array in itself.
However, using a linked list would avoid this problem, I know VB6 didn't support linked lists, not sure if .NET does as I've not needed them recently.
In either case a simple linked list can be created with a 2D array, whereby the second part of each array element points to the next field, to delete an element just change it to point to another element, certainly this removes the problem of null values even if it over-complicates things.
There are no simple 2D arrays in VB, a 1D array of classes or structures can be done more easily. However, I posted some suggestion about something like that about a year ago, and several people chimed in to point out that there was a built-in feature that worked better.....I just forget what it was at the moment.
When I say 2D array I mean as this:
VB Code:
Dim StrArray(5,2) As Integer
Therefore the contents of which can be arranged as below, where the array item (n,2) is the pointer to the next item in the linked list. Although as you say chances are that there's now a function in VB that can do this.
Code:# | value | pointer
1 | 5 |
2 | 2 | 5
3 | 4 | 1
4 | 1 | 2
5 | 3 | 3
I understood what you meant, but because it is so painful to extend a 2D array in VB, I feel that a 1D array of structures or classes is almost always a better option. Your structure would look something like this:
VB Code:
Public structure Node Public Value as integer Public Pointer as integer End structure
Ah, sorry, I misunderstood, I can see how that would make things easier :)
.NET does have a LinkedList class but it is not required. The point of a linked list is to be able to get to the next, and possibly the previous, node from the current node. That is not required in this situation. If all that is required is that you be able to remove items then a simple collection is all that's required. That's all the Items property of the ListBox is anyway: a simple collection.
Basically there are two sensible choices here. If you do not need the data maintained separately to the ListBox then don't use a an array or collection or anything else. Just use the Items property of the ListBox itself. If you do need the data maintained in a location separate to the ListBox then you would use a simple collection. In .NET 1.x you would use a StringCollection, ArrayList or your own collection derived from CollectionBase. In .NET 2.0 you would use a List(Of T) where T is the type of the objects.