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 に答える