Deselect Row in ListBox

R

Ryan H

I have a MultiSelectSingle Listbox and an Edit Button. If the user selects a
line in the listbox it enables the Edit Button and highlights the line blue.
Is there a way to unhighlight the row if it is selected again? I have this
in the Listbox Click Event, but the event won't fire when I select the
highlighted row. Any ideas?

Private Sub lbxPreview_Click()

' load selected part info into controls
With lbxPreview
cboPartNumber = .List(.ListIndex, 0)
tbxQuantity = .List(.ListIndex, 2)
cboBilling = .List(.ListIndex, 4)

' enable or disable Edit button, and deselect listbox
If Not .ListIndex = -1 Then
cmbEditItem.Enabled = True
Else
.Value = Null
cmbEditItem.Enabled = False
End If
End With

' lock or unlock all controls except qty if items is going to be edited
cboPartNumber.Locked = cmbEditItem.Enabled
cboPartDescription.Locked = cmbEditItem.Enabled
cboBilling.Locked = cmbEditItem.Enabled

' only allow quantity to be changed
tbxQuantity.SetFocus

End Sub
 
D

Dave Peterson

Try using a different _event:

Option Explicit
Dim CurIndex As Long
Private Sub CommandButton2_Click()
Unload Me
End Sub
Private Sub lbxPreview_MouseUp(ByVal Button As Integer, _
ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)

With Me.lbxPreview
If CurIndex = .ListIndex Then
.ListIndex = -1
'reset anything you need to here
Else
'do the real work
End If
'prepare for next time
CurIndex = .ListIndex
End With
End Sub
Private Sub UserForm_Initialize()
Dim iCtr As Long
With Me.lbxPreview
.MultiSelect = fmMultiSelectSingle
For iCtr = 1 To 5
.AddItem "A" & iCtr
Next iCtr
.ListIndex = -1 'nothing selected
CurIndex = -1
End With
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top