Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding item at certain position in ListBox 1

Status
Not open for further replies.

MrSandman666

Programmer
Feb 5, 2001
54
DE
Hello everybody!

I got a little problem here and I hope you can help me. I have a list box into which the user can add items during runtime. Now, lets say we have our brainless whiny user Bob. Now, Bob sits down and starts to work and enter items into the listbox, using my form for input. Now, since Bill isn't all that smart, he remembers that he wanted to add an item waaaay up in the list although he is already waaaay down. Deleting every item up to the position where he forgot to enter the item would destroy most of Bob's hard work so he wants to just go up and insert the forgotten item without having to delete everything up to there. He can't though, since ListBox.AddItem adds new items only to the bottom of the list. Now Bob comes to me, the programmer, and tells me to fix that, so that I can do some work in order to save some work for Bob. My problem is: I have no idea how I would go about this. How could I add an item at the top of a ListBox without having to store all the data, delete everything up to the insertion point and then add all the items again?

Thanx a lot for helping Bob and me out.
 
The ListBox.AddItem method has an Index parameter, which you can use to insert a new item in any required position in the list.

Of course, you have to develop a mechanism by which the user identifies where in the list he wants the item inserted, the simplest of which is to get them to select an item in the list before which the new item will get inserted:

List1.AddItem "New Item", List1.ListIndex

Regards,
Mike
 
Wow! Amazing! Thanks, that worked! I never thought that VB updates the indices automatically. I come from a C++ background, you must understand :) I'm just not used to such a level of comfort and automatization.
I thought the index is just kinda like a handle to access every element since the names of the elements can have duplicates and that an element would keep its index when you move it. So I thought that the index is independent from position in the list.

Well, thanks!
 
Geeez, there's always something wrong...
Now I know how to add an item at the position where the user clicked (thanks again) but what if I want to add the item at the bottom of the list again? The program always adds the item where the current selection is but afaik there's no way to "unselect" an item in a ListBox.
Any ideas how to approach this?
 
Sure. Here's one approach:

Have two buttons on your form, one captioned "Insert", one captioned "Append"

Sub cmdInsert_Click()
List1.AddItem "Inserted Item", List1.ListIndex
End Sub

Sub cmdAppend_Click()
List1.AddItem "Appended Item"
' or, if you happen to like using the Index parameter
' List1.AddItem "Appended Item", List1.ListCount
End Sub

Additionally, to unselect an item, try

List1.ListIndex = -1
 
I really don't want to have any more buttons on my form. It's loaded enough the way it is. I thought about the ListIndex = -1 already, too, but I'm not quite sure yet where to put it. I might execute it everytime the user changes something in the ListBox but that can get extremely annoying when you have to insert larger blocks of data, since you would have to click on the position in the ListBox every single time you want to insert something.
 
Well, how do you determine at present whether you want to insert at a user-selected location or at the end?
 
When there's no item selected, the new item is appended to the end of the list. When there IS an item selected, the new item is inserted before the selected one. Now, in the beginning there is no item selected so you can keep chugging away and just shoving data into the list until you realize you missed something. Then you click at the point where you want to insert something and enter the data. The problem is that you can't unselect the item anymore from now on. There's always one item selected (unless you delete it) and thus the program will always squeeze the new item in front of the selection and never at the end of the list.
 
Well, you've now got more-or-less all the information you need to solve this yourself, but here's some more:

' Selects and unselects item in a list box
' Note: this is non-standard behaviour, and possibly
' counterintuitive
Private Sub List1_Click()
Static mvarLastSelected As Long
If List1.ListIndex = mvarLastSelected Then
List1.ListIndex = -1
End If
mvarLastSelected = List1.ListIndex
End Sub

and in your add item routine

If List1.ListIndex = -1 Then
List1.AddItem "Appended Item"
Else
List1.Additem "Inserted Item", List1.ListIndex
End If
 
Now THAT is what I call a programmer! Why didn't i think of something like that earlier? Oh well, it's been a long day. Thank's a lot! You saved me once again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top