1

我正在创建一个具有搜索功能的程序。它工作正常。如果我在搜索框中输入一个项目,它会显示该项目,如果我将其留空,它将显示所有项目等。我的问题是,如果我要搜索特定产品,ListView 是否可以在列表顶部和之后的所有其他项目并尽可能变灰?我需要所有数据,以便可以将其复制到外部文件。

这是我目前的代码。正如我所说,它工作正常,但有可能因为我需要列表视图中的所有项目,以便我的程序可以正常运行。谢谢

 Dim desc As String
 Dim barcode As String
 Dim quantity As String
 Dim dept As String

 Dim listitm As ListItem
 Dim itm As ListItem
 Dim SearchStr As String
 Dim SearchChar As String
 Dim colhead As ColumnHeader

 ListView1.ListItems.Clear

 Open "E:\Latest VB\Export.CSV" For Input As #1
 Do Until EOF(1)
 Input #1, desc, barcode, quantity, dept
 If InStr(1, LCase(desc), txtProduct.Text, vbTextCompare) Then
 With ListView1
    .View = lvwReport
    .FullRowSelect = True
 Set itm = .FindItem(txtProduct.Text, lvwText, , lvwPartial)
 Set listitm = .ListItems.Add(, , desc)
 listitm.SubItems(1) = (barcode)
 listitm.SubItems(2) = (quantity)
 listitm.SubItems(3) = (dept)
 End With
 End If
 Loop
 Close #1
 End Sub
4

2 回答 2

1

您不能将项目变灰(禁用),但您可以将前景色更改为灰色,就像使用自定义绘图回调( zip ) 将其禁用一样。

于 2012-07-26T11:03:06.060 回答
0

如果您只想将文本变灰而不禁用它,那么以下操作将起作用:

' Purpose: If a match for <the_sText> is found, then the first item matching is moved to the top of the control, and all the other items are greyed out.
Private Sub HighlightTextInListView(ByRef the_sText As String)

    Dim sKeyOfPreviousItem  As String
    Dim oListItemAtTop      As ListItem
    Dim oFoundListItem      As ListItem
    Dim nIndex              As Long

    Set oListItemAtTop = lvData.ListItems(1)
    sKeyOfPreviousItem = oListItemAtTop.Tag         ' We are storing the original previous item's key in the top item's Tag property.

    ' If it is set to non-empty string, the we know that it has been previous sorted to the top.
    ' Undo this before anything else.
    If sKeyOfPreviousItem <> "" Then
        MoveItem oListItemAtTop, lvData.ListItems.Item(sKeyOfPreviousItem).Index + 1
    End If

    If the_sText = "" Then

        ' If the search box is empty, then ungrey all the text.
        SetTextForeColor vbWindowText

    Else

        ' Look for the text.
        Set oFoundListItem = lvData.FindItem(the_sText, lvwText, , lvwPartial)

        If oFoundListItem Is Nothing Then
            SetTextForeColor vbWindowText
        Else
            SetTextForeColor vbGrayText

            ' Find the text in the previous item to the selected item (if none, use "").
            nIndex = oFoundListItem.Index
            If nIndex = 1 Then
                sKeyOfPreviousItem = ""
            Else
                sKeyOfPreviousItem = lvData.ListItems.Item(nIndex - 1).Key
            End If

            ' Move this item to the top of the list, and set its Tag property to indicate the previous item.
            ' Note that this new item will not have grey text.
            MoveItem(oFoundListItem, 1).Tag = sKeyOfPreviousItem

        End If

    End If

End Sub

' Purpose: Adds a new item to the list view.
Private Sub AddListItem(ByRef the_sKey As String, ByRef the_sDescription As String, the_sBarCode As String, ByRef the_sQuantity As String, ByRef the_sDepartment)

    With lvData.ListItems
        With .Add(, the_sKey, the_sDescription)
            .SubItems(1) = the_sBarCode
            .SubItems(2) = the_sQuantity
            .SubItems(3) = the_sDepartment
        End With
    End With

End Sub

' Purpose: Adds a new item with the same properties as the previous, at a specific position.
Private Function MoveItem(ByRef the_oListItem As ListItem, ByVal the_nNewPos As Long) As ListItem

    Dim oListItem               As ListItem
    Dim sKey                    As String

    Set oListItem = lvData.ListItems.Add(the_nNewPos, , the_oListItem.Text)
    oListItem.SubItems(1) = the_oListItem.SubItems(1)
    oListItem.SubItems(2) = the_oListItem.SubItems(2)
    oListItem.SubItems(3) = the_oListItem.SubItems(3)

    sKey = the_oListItem.Key
    lvData.ListItems.Remove sKey
    oListItem.Key = sKey

    Set MoveItem = oListItem

End Function

' Purpose: Set the fore colour of each list item.
Private Sub SetTextForeColor(ByVal the_cForeColor As OLE_COLOR)

    Dim oListItem               As ListItem
    Dim oSubItem                As ListSubItem

    For Each oListItem In lvData.ListItems
        oListItem.ForeColor = the_cForeColor
        For Each oSubItem In oListItem.ListSubItems
            oSubItem.ForeColor = the_cForeColor
        Next oSubItem
    Next oListItem

End Sub

当您想要运行搜索时,只需使用 HighlightTextInListView() 即可。本质上,我删除了该项目并在顶部重新添加它。为了记住它的旧位置,前一个项目的 Key 属性存储在移动项目的 Tag 属性中。要使此代码起作用,您必须为您的项目提供唯一键。

于 2012-07-26T13:16:10.420 回答