PDA

View Full Version : [SOLVED] Clear selection from listbox



Ken Puls
12-08-2004, 09:49 AM
Hi guys,

Ran into a small issue with a listbox that I can't figure out. I have two listboxes on a form, which list the column heading for the spreadsheet. I need the user to pick one in each listbox, but they have to be different.

I pulled the following routine together to ensure that if a user picks the same two values, that the listindex of the second box (lbSort) will be set back to -1. While this effectively resets the box, the line still appears as highlighted. I've played with the "selection" property but just can't seemed to clear it!

Code to date:


Private Sub Utility_Check_ListItems()
'Ensure that lbSort does not have same item selected as lbUnique
If lbUnique.ListIndex = lbSort.ListIndex Then
MsgBox "Sorry. You cannot choose the same column for both choices.", _
vbOKOnly + vbCritical, "Try again!"
lbSort.ListIndex = -1
End If
End Sub

I know that I can clear and recreate the listbox values, but this seems like it should be unnecessary. Any idea how to just unselect the selection?

Thanks,

Andy Pope
12-08-2004, 10:05 AM
Hi kpuls,

Try this, where the 2 listboxes are called listbox1 and listbox2.
Listbox2 can not be picked is listbox1 is already selected.



Private Sub ListBox2_AfterUpdate()

If ListBox2.ListIndex >= 0 Then
If ListBox1.Selected(ListBox2.ListIndex) Then
ListBox2.ListIndex = -1
End If
End If

End Sub

Ken Puls
12-08-2004, 10:16 AM
Andy,

That is absolutley perfect!:thumb Thanks a ton!