当你点击它们时,列表框中的项目不突出 [英] List Box items not highlighted when clicking on them

查看:300
本文介绍了当你点击它们时,列表框中的项目不突出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个窗体列表框中。点击它会导致表跳到另一个纪录。它应该突出一个项目,并跳转到正确的记录。相反,它突出了,然后立即清除的选择,尽管它仍然跳转到该记录。当我使用标准的记录选择按钮,项正确突出显示。

我读所选项目从.ListIndex属性索引,因为选择()不以单一选择模式工作时,我测试选择哪个项目。然而,.ListIndex是只读属性,我用.Selected()来选择项目。

干杯!

编辑:下面的code:

 选项比较数据库
显式的选项

私人小组Form_Current()
    呼叫highlightListBox
结束小组

私人小组lbListBox_Click()
    昏暗的RS作为DAO.Recordset
    昏暗INDX只要

    设置RS = Me.RecordsetClone
    如果没有rs.BOF而不是RS.EOF然后
        rs.MoveFirst
        rs.FindFirst[ID] =放大器; CStr的(Me.lbListBox.ItemData(Me.lbListBox.ListIndex))
        如果不rs.NoMatch然后
            Me.Bookmark = rs.Bookmark
        结束如果
    结束如果

    rs.Close
    设置RS =什么
结束小组

私人小组highlightListBox()
    昏暗lngIndx只要
    昏暗lngI只要
    昏暗bNoMatch由于布尔
    lngIndx = 0
    bNoMatch = TRUE
    如果Me.NewRecord<> 0或者ISNULL(我!ID)然后
        对于lngI = 0〜Me.lbListBox.ListCount  -  1
            Me.lbListBox.Selected(lngI)= FALSE
        接下来lngI
    其他
        做
            lngIndx = lngIndx + 1
            如果CLng函数!(Me.lbListBox.ItemData(lngIndx  -  1))=我的ID,然后在
                bNoMatch = FALSE
            结束如果
        循环直到CLng函数!(Me.lbListBox.ItemData(lngIndx  -  1))=我的ID或lngIndx = Me.lbListBox.ListCount
    结束如果
    如果不bNoMatch然后
        Me.lbListBox.Selected(lngIndx  -  1)=真
    结束如果
结束小组
 

解决方案

我一直在考虑一个建议关于略有不同的问题的这里但由于 Remou 我整理了这一点。

新的code为以下内容:

 选项比较数据库
显式的选项

私人小组Form_Current()
    Me.lbListBox =我!ID
结束小组

私人小组lbListBox_Click()
    昏暗的RS作为DAO.Recordset
    昏暗INDX只要

    设置RS = Me.RecordsetClone
    如果没有rs.BOF而不是RS.EOF然后
        rs.MoveFirst
        rs.FindFirst[ID] =放大器; CStr的(Me.lbListBox.ItemData(Me.lbListBox.ListIndex))
        如果不rs.NoMatch然后
            Me.Bookmark = rs.Bookmark
        结束如果
    结束如果
    Me.lbListBox =我!ID

    rs.Close
    设置RS =什么
结束小组
 

我不知道我可以实际设置一个值,使用绑定列列表框。通过这样做,既突出和聚焦设置。我不知道,但我认为,多选,必须设置为0。就我而言,该行

  Me.lbListBox =我!ID
 

做这项工作:)

我希望这回答能帮助别人:)

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.

Cheers!

EDIT: The code below:

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

解决方案

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 :)

这篇关于当你点击它们时,列表框中的项目不突出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆