我正在创建一个具有搜索工具的程序。它工作正常。如果我在搜索框中输入一个项目,则显示该项目,如果我将其留空,则显示该项目等。 我的问题是,如果我要搜索特定的产品,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
答案 0 :(得分:1)
您不能灰显(禁用)项目,但您可以使用Custom Drawing callbacks(zip)将forecolor更改为灰色,就好像它已被禁用一样。
答案 1 :(得分: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属性中。要使此代码生效,您必须为商品提供唯一的密钥。