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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with highlighting an item in listbox 1

Status
Not open for further replies.

PrograMan

Programmer
Jul 21, 2001
59
US
Ok, I'm writing a program that consists of a listbox and adding strings to it. At some point I want to be able to delete one of those items and highlight the item that falls in its place. Here's the actual coding I've been using:

Private Sub delCmd_Click()
Dim count

Dim c As Integer
c = -1 '*Finding the actual length of 'the list that's not null*

Do '*Finding the number of items in the list that are not null*
c = c + 1
Loop Until List1.List(c) = ""

If List1.Text = "" Then
MsgBox "Nothing selected or list is empty!"
Else
For count = 0 To c - 1
List1.RemoveItem count
If List1.List(count) > "" Then
List1.Selected(count) = True
End If
Next count
End If

End Sub

This compiles just fine, but when working with the running program, I get a Run time error 5 where there's an invalid call procedure or argument and the line "List1.RemoveItem count" is highlighted. Can anyone help me with that? If it matters, I'm working on WinXP.
 
your second loop is removing all the items that it counted in the first loop. as it removes each item the number of items gets less, but you keep trying to remove the next one. eventually you get to a point where you are trying to remove an item at an index that is already gone.

First you could use the .ListCount to find out how many items are in the list.

Second, if you just want to clear the whole list use .Clear

now if you just want to delete the selected item, then first find the selected item ...

In the SelecteIndexChanged event
selectedItemVar = ListBox.SelectedItem

then use the remove item method, then just reselect the item that is now at the old index.


Becca



Somtimes, the easy answer is the hardest to find. :)
 
Strange, the books that I've read of deleting an item say that .Remove will only delete what's highlighted, and since my program shows that when it finds and deletes the highlighted item, it goes on to look at the next item spot, which will not be highlighted.

I'm not trying to delete the entire list. That's the easy part. The hard part is trying to delete and then highlight once again. I've already tried using List1.ListCount - 1, but the program sees any blank item spaces under the actual list as items as well, and it screws stuff up for me.

I never heard of the SelecteIndexChanged event. By the way, is it called SelectedIndexChanged? I've been looking around trying to find all the possible events for this object, but with limited luck. Any suggestions?
 
PrograMan,

This code should work when user presses Delete button

Private Sub List1_KeyDown(KeyCode As Integer, Shift As Integer)
Dim intIndex As Integer

If KeyCode = vbKeyDelete Then
intIndex = List1.ListIndex

If intIndex = -1 Then
MsgBox "Please select item first."
ElseIf intIndex = List1.ListCount - 1 Then
List1.RemoveItem (intIndex)
Else
List1.RemoveItem (intIndex)
List1.Selected(intIndex) = True
End If

End If
End Sub

Vladk
 
There was one little thing I had to adjust, but your reply did the trick, Vladk! Thanks so much :). I'm sure you'll be seeing more of my posts in the near future.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top