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

Problems with sliding items in a list box 1

Status
Not open for further replies.

1todd1

Programmer
Jan 19, 2002
17
NZ
Im trying to create a listbox in which you can slide elements of the contents up and down the list with you mouse. The code works however if you click the mouse button quickly an error appears, caused on the line - llt1.AddItem Text2.Text, List1.ListIndex -
Err no 5 invalid procedure or argument – could some one explain what is happening, or arrive at some better code.

The code is a follows


Private Sub Form_Load()

For i = 0 To 5
List1.AddItem "number " & i
Next i

End Sub
Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

a = List1.ListIndex
Text1.Text = List1.ListIndex
Text2.Text = List1.Text

End Sub
Private Sub List1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)

On Error GoTo Simple_Error

If List1.ListIndex = -1 Then
Exit Sub
End If

List1.RemoveItem Text1.Text
List1.AddItem Text2.Text, List1.ListIndex - 1

Text1.Text = vbNull
Text2.Text = vbNull

Exit Sub
Simple_Exit:
Exit Sub

Simple_Error:
MsgBox "You raised error: " & _
Err & ", " & Err.Description & _
vbCrLf & _
"At program location: " & _
Err.Source
Resume Simple_Exit
End Sub

Thanks for your time Todd :)
 
change
List1.AddItem Text2.Text, List1.ListIndex - 1
to
List1.AddItem Text2.Text
List1.ListIndex = List1.ListIndex - 1

 
Firstly I’d like to thank you for your speedy reply Malorey, however it doesn’t seem to work. The error is still present and doesn’t resolve the problem. Try the code yourself and debug it. There is a form with a listbox called list1 and two text boxes called text1 and text2. The Option Explicit line doesn’t exits.

Thanks once again for any thoughts or suggestions :)
 
The problem occurs when you remove the item from a listbox that the current ListIndex is pointing to. Under those circumstances, ListIndex is reset to -1. Which is an illegal index key to use when adding an item. So, without going into the rest of your code, here is one way of fixing this specific problem:

[tt]
Public a

Private Sub Form_Load()

For i = 0 To 5
List1.AddItem "number " & i
Next i

End Sub
Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

a = List1.ListIndex
Text1.Text = List1.ListIndex
Text2.Text = List1.Text

End Sub
Private Sub List1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)

On Error GoTo Simple_Error

If List1.ListIndex = -1 Or List1.ListIndex = a Then
Exit Sub
End If

List1.RemoveItem Text1.Text
List1.AddItem Text2.Text, List1.ListIndex + 1

Text1.Text = vbNull
Text2.Text = vbNull

Exit Sub
Simple_Exit:
Exit Sub

Simple_Error:
MsgBox "You raised error: " & _
Err & ", " & Err.Description & _
vbCrLf & _
"At program location: " & _
Err.Source
Resume Simple_Exit
End Sub

 
replace the following lines in your mouseup event:

If List1.ListIndex = -1 Then
Exit Sub
End If

List1.RemoveItem Text1.Text
List1.AddItem Text2.Text, List1.ListIndex - 1



with:

Dim b As Integer
b = List1.ListIndex
If b = -1 Then
Exit Sub
End If

List1.RemoveItem Text1.Text
List1.AddItem Text2.Text, b
 
Thanks guys

Actually had tried something similar however was performing the assignment of the list index after the removal as didn’t realise the listindex was set to -1 – good point Strongman. Thanks also sjravee thats a nice solution.

Thank for everyone’s time with suggestions for this problem
;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top