是否可以在VB6中灰显ListView上的数据?

是否可以在VB6中灰显ListView上的数据?,listview,vb6,Listview,Vb6,我正在创建一个有搜索功能的程序。它很好用。如果我在搜索框中输入一个项目,它将显示该项目;如果我将其留空,它将显示所有项目等。 我的问题是,如果我要搜索某个特定的产品,ListView是否可能在列表的顶部显示该项目,并且如果可能,所有其他项目都会显示为灰色?我需要所有数据,以便将其复制到外部文件 这是我目前的代码。正如我所说,它可以正常工作,但这是可能的,因为我需要listview中的所有项目,以便我的程序可以正常运行。谢谢 Dim desc As String Dim barcode As

我正在创建一个有搜索功能的程序。它很好用。如果我在搜索框中输入一个项目,它将显示该项目;如果我将其留空,它将显示所有项目等。 我的问题是,如果我要搜索某个特定的产品,ListView是否可能在列表的顶部显示该项目,并且如果可能,所有其他项目都会显示为灰色?我需要所有数据,以便将其复制到外部文件

这是我目前的代码。正如我所说,它可以正常工作,但这是可能的,因为我需要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

不能将项目灰显(禁用),但可以将前景色更改为灰色,就像使用()将其禁用一样。

不能将项目灰显(禁用),但可以将前景色更改为灰色,就像使用()将其禁用一样。

如果只想将文本灰显,而不想禁用它,则以下操作将起作用:

' 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
”目的:如果找到了的匹配项,则第一个匹配项将移动到控件顶部,所有其他项将灰显。
Private Sub-HighlightTextInListView(以字符串形式引用文本)
将sKeyOfPreviousItem变暗为字符串
Dim oListItemAtTop作为列表项
作为ListItem的UndListItem的Dim
暗九倍长
设置oListItemAtTop=lvData.ListItems(1)
sKeyOfPreviousItem=oListItemAtTop.Tag“我们正在顶部项目的Tag属性中存储上一个项目的原始密钥。
'如果它设置为非空字符串,则我们知道它之前已被排序到顶部。
“在做其他事情之前先把它解开。
如果sKeyOfPreviousItem为“”,则
MoveItem oListItemAtTop,lvData.ListItems.Item(sKeyOfPreviousItem).Index+1
如果结束
如果_sText=”“,则
'如果搜索框为空,则取消搜索所有文本。
SetTextForeColor vbWindowText
其他的
'查找文本。
集合oundListItem=lvData.FindItem(the_sText,lvwText,lvwPartial)
如果OfUndlistItem为Nothing,则
SetTextForeColor vbWindowText
其他的
SetTextForeColor vbGrayText
'查找所选项目的上一个项目中的文本(如果没有,请使用“”)。
nIndex=oFoundListItem.Index
如果nIndex=1,则
sKeyOfPreviousItem=“”
其他的
sKeyOfPreviousItem=lvData.ListItems.Item(nIndex-1).Key
如果结束
'将此项目移动到列表顶部,并将其标记属性设置为指示上一个项目。
'请注意,此新项目不会有灰色文本。
MoveItem(OfUndListItem,1).Tag=sKeyOfPreviousItem
如果结束
如果结束
端接头
'目的:将新项目添加到列表视图。
私有子AddListItem(ByRef the_sKey As String,ByRef the_sdesdescription As String,ByU sBarCode As String,ByRef the_sQuantity As String,ByRef the_sDepartment)
使用lvData.ListItems
With.Add(,the_sKey,the_sdesdescription)
.子项(1)=sBarCode
.分项(2)=单位数量
.子项(3)=部门
以
以
端接头
'目的:在特定位置添加与上一个项目具有相同属性的新项目。
私有函数MoveItem(ByRef the_oListItem As ListItem,ByVal the_nNewPos As Long)作为ListItem
作为列表项的Dim oListItem
像线一样模糊
Set oListItem=lvData.ListItems.Add(newpos、oListItem.Text)
寡头子项(1)=寡头子项(1)
子项(2)=子项(2)
寡头子项(3)=寡头子项(3)
sKey=该键为_oListItem.Key
lvData.ListItems.Remove sKey
oListItem.Key=sKey
设置MoveItem=oListItem
端函数
'目的:设置每个列表项的前颜色。
专用子设置xtForeColor(将颜色替换为OLE颜色)
作为列表项的Dim oListItem
将oSubItem作为列表子项
对于lvData.ListItems中的每个oListItem
oListItem.ForeColor=颜色
对于oListItem.ListSubItems中的每个oSubItem
oSubItem.ForeColor=颜色
下一步
下乳石
端接头

只要在需要运行搜索时使用HighlightTextInListView()。基本上,我删除了该项并在顶部重新添加它。若要记住其旧位置,则上一个项目的“键”属性将存储在移动项目的“标记”属性中。要使此代码正常工作,您必须为您的项目提供唯一的键。

如果您只想灰显文本,而不是禁用文本,则以下操作将起作用:

' 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
”目的:如果找到了的匹配项,则第一个匹配项将移动到控件顶部,所有其他项将灰显。
Private Sub-HighlightTextInListView(以字符串形式引用文本)
将sKeyOfPreviousItem变暗为字符串
Dim oListItemAtTop作为列表项
作为ListItem的UndListItem的Dim
暗九倍长
设置oListItemAtTop=lvData.ListItems(1)
sKeyOfPreviousItem=oListItemAtTop.Tag“我们正在顶部项目的Tag属性中存储上一个项目的原始密钥。
'如果它设置为非空字符串,则我们知道它之前已被排序到顶部。
“在做其他事情之前先把它解开。
如果sKeyOfPreviousItem为“”,则
MoveItem oListItemAtTop,lvData.ListItems.Item(sKeyOfPreviousItem).Index+1
如果结束
如果_sText=”“,则
'如果搜索框为空,则取消搜索所有文本。
SetTextForeColor vbWindowText
其他的
'查找文本。
集合oundListItem=lvData.FindItem(the_sText,lvwText,lvwPartial)
如果OfUndlistItem为Nothing,则
SetTextForeColor vbWindowText
其他的
SetTextForeColor vbGrayText
'查找所选项目的上一个项目中的文本(如果没有,请使用“”)。