List Box items not highlighted when clicking on them

和自甴很熟 提交于 2019-12-13 04:47:10

问题


I have a list box in a form. Clicking on it causes the form to jump to another record. It supposed to highlight an item and jump to the correct record. Instead, it highlights, and then instantly clears the selection, although it still jumps to the record. When I use standard record selection buttons, items are correctly highlighted.

I read the index of selected item from .ListIndex property because Selected() does not work in a Single Selection mode when I test which item is selected. However, .ListIndex is read-only property and I use .Selected() to highlight the item.

Option Compare Database
Option Explicit

Private Sub Form_Current()
    Call highlightListBox
End Sub

Private Sub lbListBox_Click()
    Dim rs As DAO.Recordset
    Dim indx As Long

    Set rs = Me.RecordsetClone
    If Not rs.BOF And Not rs.EOF Then
        rs.MoveFirst
        rs.FindFirst "[ID]=" & CStr(Me.lbListBox.ItemData(Me.lbListBox.ListIndex))
        If Not rs.NoMatch Then
            Me.Bookmark = rs.Bookmark
        End If
    End If

    rs.Close
    Set rs = Nothing
End Sub

Private Sub highlightListBox()
    Dim lngIndx As Long
    Dim lngI As Long
    Dim bNoMatch As Boolean
    lngIndx = 0
    bNoMatch = True
    If Me.NewRecord <> 0 Or IsNull(Me!ID) Then
        For lngI = 0 To Me.lbListBox.ListCount - 1
            Me.lbListBox.Selected(lngI) = False
        Next lngI
    Else
        Do
            lngIndx = lngIndx + 1
            If CLng(Me.lbListBox.ItemData(lngIndx - 1)) = Me!ID Then
                bNoMatch = False
            End If
        Loop Until CLng(Me.lbListBox.ItemData(lngIndx - 1)) = Me!ID Or lngIndx = Me.lbListBox.ListCount
    End If
    If Not bNoMatch Then
        Me.lbListBox.Selected(lngIndx - 1) = True
    End If
End Sub

回答1:


I have been given a suggested about slightly different problem here but thanks to Remou I sorted this out.

The new code is following:

Option Compare Database
Option Explicit

Private Sub Form_Current()
    Me.lbListBox = Me!ID
End Sub

Private Sub lbListBox_Click()
    Dim rs As DAO.Recordset
    Dim indx As Long

    Set rs = Me.RecordsetClone
    If Not rs.BOF And Not rs.EOF Then
        rs.MoveFirst
        rs.FindFirst "[ID]=" & CStr(Me.lbListBox.ItemData(Me.lbListBox.ListIndex))
        If Not rs.NoMatch Then
            Me.Bookmark = rs.Bookmark
        End If
    End If
    Me.lbListBox = Me!ID

    rs.Close
    Set rs = Nothing
End Sub

I did not realise I could actually set a value to a list box using BoundColumn. By doing so, both highlighting and focusing is set. I am not sure but I think that MultiSelection has to be set to 0. In my case, the line

Me.lbListBox = Me!ID

does the job :)

I hope this answer can help someone else :)



来源:https://stackoverflow.com/questions/24629102/list-box-items-not-highlighted-when-clicking-on-them

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!