Can anyone provide me with code on how to Delete a row from a list box??

lstReorder.RemoveItem (index)

I believe is the right code. However this does not actually do anything by itself. How do i tell the program what the index refers to? ie what the index is? for example

lstReorder.RemoveItem (index) does nothing
lstReorder.RemoveItem (0) deletes the first item from the list box

any ideas?? and also how do i delete the full contents of a list box??
lstReorder.clear doesnt seem to want to work :s

Any help is much appreciated

You could iterate through the list box items in a for loop, keep a variable that changes by one each time (usually i) and use lst.Reorder.RemoveItem(i), replace i with whatever you use, and if you want to delete only a specific item, check the value of each on with an if statement

How would i differentiate between what row was selected, and which one wasnt?

What would be the indicator teling the for statement to stop?

using i, as you go through the loop you increment i, (0, 1, 2, 3, etc...) and that will increment the row that you are on. If you want a specific text then using the if statement you check to see what text is in the current row use lstReorder.Items.Item(i).ToString() and compare that to whatever string you want, if it is what you want - do what you want to do to it, otherwise go on

I'm not searching for a specific result. The user will select a row which they wish to delete, and then press the delete button. is there a command which i can use to indicate the row the user has selected?

Just place this behind the delete button.

lstReorder.RemoveItem (lstReorder.ListIndex)

As long as the user has selected a line in the list box, this will work.

-Mike

commented: Very Helpful :) Thanks for the VB code and advice +1

You had me excited them mike. still no luck. it doesnt error or do anything. that indicates to me that
lstReorder.ListIndex
doesnt bring back any value.
is there a method of viewing the code working step by step and looking at the values that are being processed by the code??

.listindex is the items index in the list. You could try setting a variable to it.

a = lstReorder.ListIndex

then do:

lstReorder.RemoveItem (a)

I have attached the project that I have tested it with, and it seems to work. Maybe this will help you.

-mike

I'm actually using Access 2000. The code works fine in visual basic... but then when i move it over to access it just doesnt work. Its very confusing... I even tried to store index as a variable and then use that to delete a row. Bit still it doesnt work. It doesnt error or have any problems, it just doesnt delete a row as it is supposed to.

I think its a setting on the list box itself that is causing the problem. Ive had to set Row Source Type to Value List in order to add items to the list box. Will that cause a problem?

any ideas?? i'm determined to get this done haha if it kills me!!

So your list box is in access, is it linked to a table or query?

the list box is not linked. but 2 text boxes are linked to a querey, and the data from those text boxes is added to the list box when the user clicks the add button

i tried this with 2 listbox and 1 button. i was moved item on list2 to list5 and remove current item after moved.

Private Sub Form_Load()
With List2
    .AddItem ("1")
    .AddItem ("2")
    .AddItem ("3")
End With
Private Sub Command4_Click()
List5.AddItem List2.ItemData(List2.ListIndex)
List2.RemoveItem List2.ItemData(List2.ListIndex)
End Sub
End Sub
commented: always very helpfull:) +1
commented: helpfull +1

Look up the Selected property for the ListBox control.


Dim i as integer

For i = o to lstBox.ListCount
If lstBox.Selected(i) then
lstBox.RemoveItem i
Exit For
Endif
Next i

commented: Very Helpful, Solved my problem straight away :) thankyou +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.