annandap
asked on
ListView - search on SubItem
I need help in trying to carryout a search on subitems in a listview, I can currently search both the main and subitems but when searching the subitem I have to provide the full text even though I specify partial in the finditem function to return a found value. Works fine on the main item, any help would be greatly apreciated.
If possible I would like to use the finditem function of the listview control to search for subitems with only supplying limited text. ie. Serach for Ground and only supply gro to the search function.
If possible I would like to use the finditem function of the listview control to search for subitems with only supplying limited text. ie. Serach for Ground and only supply gro to the search function.
Hi annandap,
I basically have a different problem but using the same object which is listview. I am wondering if you know of any method to select a row which have many colums. At the mean time i am only able to select the 1st column. Why?
Your help is really appreciated..and if u don't know what my question is the go to this url:
http://www.fortunecity.com/skyscraper/mondo/83
thank you.
I basically have a different problem but using the same object which is listview. I am wondering if you know of any method to select a row which have many colums. At the mean time i am only able to select the 1st column. Why?
Your help is really appreciated..and if u don't know what my question is the go to this url:
http://www.fortunecity.com/skyscraper/mondo/83
thank you.
FindItem Method (ListView Control)
Finds and returns a reference to a ListItem object in a ListView control. Syntax
object.FindItem (string, value, index, match)
The FindItem method syntax has these parts:
Part
Description
object
Required. An object expression that evaluates to a ListView control.
string
Required. A string expression indicating the ListItem object to be found.
value
Optional. An integer or constant specifying whether the string will be matched to the ListItem object's Text, Subitems, or Tag property, as described in Settings.
index
Optional. An integer or string that uniquely identifies a member of an object collection and specifies the location from which to begin the search. The integer is the value of the Index property; the string is the value of the Key property. If no index is specified, the default is 1.
match
Optional. An integer or constant specifying that a match will occur if the item's Text property is the same as the string, as described in Settings.
Settings
The settings for value are:
Constant
Value
Description
lvwText
0
(Default) Matches the string with a ListItem object's Text property.
lvwSubitem
1
Matches the string with any string in a ListItem object's SubItems property.
lvwTag
2
Matches the string with any ListItem object's Tag property.
The settings for match are:
Constant
Value
Description
lvwWholeWord
0
(Default) An integer or constant specifying that a match will occur if the item's Text property begins with the whole word being searched. Ignored if the criteria is not text.
lvwPartial
1
An integer or constant specifying that a match will occur if the item's Text property begins with the string being searched. Ignored if the criteria is not text.
Remarks
If you specify Text as the search criteria, you can use lvwPartial so that a match occurs when the ListItem object's Text property begins with the string you are searching for. For example, to find the ListItem whose text is "Autoexec.bat", use:
'Create a ListItem variable.
Dim itmX As ListItem
'Set the variable to the found item.
Set itmX = ListView1.FindItem("Auto", ,,lvwparti al)
FindItem Method Example
This example populates a ListView control with the contents of the Publishers table of the BIBLIO.MDB database. A ComboBox control is also populated with three options for the FindItem method. A CommandButton contains the code for the FindItem method; when you click on the button, you are prompted to enter the string to search for, and the FindItem method searches the ListView control for the string. If the string is found, the control is scrolled using the EnsureVisible method to show the found ListItem object. To try the example, place a ListView, ComboBox, and a CommandButton control on a form and paste the code into the form's Declarations section. Run the example and click on the command button.
Note the example will not run unless you add a reference to the Microsoft DAO 3.0 Object Library by using the References command from the Tools menu.
Private Sub Form_Load()
' Create an object variable for the ColumnHeader object.
Dim clmX As ColumnHeader
' Add ColumnHeaders. The width of the columns is the width
' of the control divided by the number of ColumnHeader objects.
Set clmX = ListView1.ColumnHeaders. _
Add(, , "Company", ListView1.Width / 3)
Set clmX = ListView1.ColumnHeaders. _
Add(, , "Address", ListView1.Width / 3)
Set clmX = ListView1.ColumnHeaders. _
Add(, , "Phone", ListView1.Width / 3)
ListView1.BorderStyle = ccFixedSingle ' Set BorderStyle property.
ListView1.View = lvwReport ' Set View property to Report.
Command1.Caption = "&FindItem"
' Label OptionButton controls with FindItem options.
Option1(0).Caption = "Text"
Option1(1).Caption = "SubItem"
Option1(2).Caption = "Tag"
ListView1.FindItem = 0 ' Set the ListView FindItem property to Text.
End With
' Populate the ListView control with database records.
' Create object variables for the Data Access objects.
Dim myDb As Database, myRs As Recordset
' Set the Database to the BIBLIO.MDB database.
Set myDb = DBEngine.Workspaces(0).Ope nDatabase( "BIBLIO.MD B")
' Set the recordset to the Publishers table.
Set myRs = myDb.OpenRecordset("Publis hers", dbOpenDynaset)
' While the record is not the last record, add a ListItem object.
' Use the reference to the new object to set properties.
' Set the Text property to the Name field (myRS!Name).
' Set SubItem(1) to the Address field (myRS!Address).
' Set SubItem(7) to the Phone field (myRS!Telephone).
While Not myRs.EOF
Dim itmX As ListItem ' A ListItem variable.
Dim intCount As Integer ' A counter variable.
' Use the Add method to add a new ListItem and set an object
' variable to the new reference. Use the reference to set
' properties.
Set itmX = ListView1.ListItems.Add(, , CStr(myRs!Name))
intCount = intCount + 1 ' Increment counter for the Tag property.
itmX.Tag = "ListItem " & intCount ' Set Tag with counter.
' If the Address field is not Null, set SubItem 1 to Address.
If Not IsNull(myRs!Address) Then
itmX.SubItems(1) = CStr(myRs!Address) ' Address field.
End If
' If the Phone field is not Null, set SubItem 2 to Phone.
If Not IsNull(myRs!Telephone) Then
itmX.SubItems(2) = myRs!Telephone ' Phone field.
End If
myRs.MoveNext ' Move to next record.
Wend
End Sub
Private Sub Command1_Click()
' FindItem method.
'Create an integer variable called intSelectedOption
' to store the index of the selected button
' Create a string variable called strFindMe. Use the InputBox
' to store the string to be found in the variable. Use the
' FindItem method to find the string. Option1 is used to
' switch the FindItem argument that determines where to look.
Dim intSelectedOption as Integer
Dim strFindMe As String
If Option1(0).Value = True then
strFindMe = InputBox("Find in " & Option1(0).Caption)
intSelectedOption = lvwText
End If
If Option1(1).Value = True then
strFindMe = InputBox("Find in " & Option1(1).Caption)
intSelectedOption = lvwSubItem
End If
If Option1(2).Value = True then
strFindMe = InputBox("Find in " & Option1(2).Caption)
intSelectedOption = lvwTag
End If
' FindItem method returns a reference to the found item, so
' you must create an object variable and set the found item
' to it.
Dim itmFound As ListItem ' FoundItem variable.
Set itmFound = ListView1. _
FindItem(strFindMe, intSelectedOption, , lvwPartial)
' If no ListItem is found, then inform user and exit. If a
' ListItem is found, scroll the control using the EnsureVisible
' method, and select the ListItem.
If itmFound Is Nothing Then ' If no match, inform user and exit.
MsgBox "No match found"
Exit Sub
Else
itmFound.EnsureVisible ' Scroll ListView to show found ListItem.
itmFound.Selected = True ' Select the ListItem.
' Return focus to the control to see selection.
ListView1.SetFocus
End If
End Sub
Private Sub ListView1_LostFocus()
' After the control loses focus, reset the Selected property
' of each ListItem to False.
Dim i As Integer
For i = 1 to ListView1.ListItems.Count
ListView1.ListItems.Item(i ).Selected = False
Next i
End Sub
Finds and returns a reference to a ListItem object in a ListView control. Syntax
object.FindItem (string, value, index, match)
The FindItem method syntax has these parts:
Part
Description
object
Required. An object expression that evaluates to a ListView control.
string
Required. A string expression indicating the ListItem object to be found.
value
Optional. An integer or constant specifying whether the string will be matched to the ListItem object's Text, Subitems, or Tag property, as described in Settings.
index
Optional. An integer or string that uniquely identifies a member of an object collection and specifies the location from which to begin the search. The integer is the value of the Index property; the string is the value of the Key property. If no index is specified, the default is 1.
match
Optional. An integer or constant specifying that a match will occur if the item's Text property is the same as the string, as described in Settings.
Settings
The settings for value are:
Constant
Value
Description
lvwText
0
(Default) Matches the string with a ListItem object's Text property.
lvwSubitem
1
Matches the string with any string in a ListItem object's SubItems property.
lvwTag
2
Matches the string with any ListItem object's Tag property.
The settings for match are:
Constant
Value
Description
lvwWholeWord
0
(Default) An integer or constant specifying that a match will occur if the item's Text property begins with the whole word being searched. Ignored if the criteria is not text.
lvwPartial
1
An integer or constant specifying that a match will occur if the item's Text property begins with the string being searched. Ignored if the criteria is not text.
Remarks
If you specify Text as the search criteria, you can use lvwPartial so that a match occurs when the ListItem object's Text property begins with the string you are searching for. For example, to find the ListItem whose text is "Autoexec.bat", use:
'Create a ListItem variable.
Dim itmX As ListItem
'Set the variable to the found item.
Set itmX = ListView1.FindItem("Auto",
FindItem Method Example
This example populates a ListView control with the contents of the Publishers table of the BIBLIO.MDB database. A ComboBox control is also populated with three options for the FindItem method. A CommandButton contains the code for the FindItem method; when you click on the button, you are prompted to enter the string to search for, and the FindItem method searches the ListView control for the string. If the string is found, the control is scrolled using the EnsureVisible method to show the found ListItem object. To try the example, place a ListView, ComboBox, and a CommandButton control on a form and paste the code into the form's Declarations section. Run the example and click on the command button.
Note the example will not run unless you add a reference to the Microsoft DAO 3.0 Object Library by using the References command from the Tools menu.
Private Sub Form_Load()
' Create an object variable for the ColumnHeader object.
Dim clmX As ColumnHeader
' Add ColumnHeaders. The width of the columns is the width
' of the control divided by the number of ColumnHeader objects.
Set clmX = ListView1.ColumnHeaders. _
Add(, , "Company", ListView1.Width / 3)
Set clmX = ListView1.ColumnHeaders. _
Add(, , "Address", ListView1.Width / 3)
Set clmX = ListView1.ColumnHeaders. _
Add(, , "Phone", ListView1.Width / 3)
ListView1.BorderStyle = ccFixedSingle ' Set BorderStyle property.
ListView1.View = lvwReport ' Set View property to Report.
Command1.Caption = "&FindItem"
' Label OptionButton controls with FindItem options.
Option1(0).Caption = "Text"
Option1(1).Caption = "SubItem"
Option1(2).Caption = "Tag"
ListView1.FindItem = 0 ' Set the ListView FindItem property to Text.
End With
' Populate the ListView control with database records.
' Create object variables for the Data Access objects.
Dim myDb As Database, myRs As Recordset
' Set the Database to the BIBLIO.MDB database.
Set myDb = DBEngine.Workspaces(0).Ope
' Set the recordset to the Publishers table.
Set myRs = myDb.OpenRecordset("Publis
' While the record is not the last record, add a ListItem object.
' Use the reference to the new object to set properties.
' Set the Text property to the Name field (myRS!Name).
' Set SubItem(1) to the Address field (myRS!Address).
' Set SubItem(7) to the Phone field (myRS!Telephone).
While Not myRs.EOF
Dim itmX As ListItem ' A ListItem variable.
Dim intCount As Integer ' A counter variable.
' Use the Add method to add a new ListItem and set an object
' variable to the new reference. Use the reference to set
' properties.
Set itmX = ListView1.ListItems.Add(, , CStr(myRs!Name))
intCount = intCount + 1 ' Increment counter for the Tag property.
itmX.Tag = "ListItem " & intCount ' Set Tag with counter.
' If the Address field is not Null, set SubItem 1 to Address.
If Not IsNull(myRs!Address) Then
itmX.SubItems(1) = CStr(myRs!Address) ' Address field.
End If
' If the Phone field is not Null, set SubItem 2 to Phone.
If Not IsNull(myRs!Telephone) Then
itmX.SubItems(2) = myRs!Telephone ' Phone field.
End If
myRs.MoveNext ' Move to next record.
Wend
End Sub
Private Sub Command1_Click()
' FindItem method.
'Create an integer variable called intSelectedOption
' to store the index of the selected button
' Create a string variable called strFindMe. Use the InputBox
' to store the string to be found in the variable. Use the
' FindItem method to find the string. Option1 is used to
' switch the FindItem argument that determines where to look.
Dim intSelectedOption as Integer
Dim strFindMe As String
If Option1(0).Value = True then
strFindMe = InputBox("Find in " & Option1(0).Caption)
intSelectedOption = lvwText
End If
If Option1(1).Value = True then
strFindMe = InputBox("Find in " & Option1(1).Caption)
intSelectedOption = lvwSubItem
End If
If Option1(2).Value = True then
strFindMe = InputBox("Find in " & Option1(2).Caption)
intSelectedOption = lvwTag
End If
' FindItem method returns a reference to the found item, so
' you must create an object variable and set the found item
' to it.
Dim itmFound As ListItem ' FoundItem variable.
Set itmFound = ListView1. _
FindItem(strFindMe, intSelectedOption, , lvwPartial)
' If no ListItem is found, then inform user and exit. If a
' ListItem is found, scroll the control using the EnsureVisible
' method, and select the ListItem.
If itmFound Is Nothing Then ' If no match, inform user and exit.
MsgBox "No match found"
Exit Sub
Else
itmFound.EnsureVisible ' Scroll ListView to show found ListItem.
itmFound.Selected = True ' Select the ListItem.
' Return focus to the control to see selection.
ListView1.SetFocus
End If
End Sub
Private Sub ListView1_LostFocus()
' After the control loses focus, reset the Selected property
' of each ListItem to False.
Dim i As Integer
For i = 1 to ListView1.ListItems.Count
ListView1.ListItems.Item(i
Next i
End Sub
You could try also this :
Private Sub Command1_Click()
For i = 1 To TreeView1.Nodes.Count
If TreeView1.Nodes(i).Text = text1 then
TreeView1.Nodes(i).Selecte d = true
TreeView1.Nodes(i).EnsureV isible
EndIf
Next
TreeView1.SetFocus
Private Sub Command1_Click()
For i = 1 To TreeView1.Nodes.Count
If TreeView1.Nodes(i).Text = text1 then
TreeView1.Nodes(i).Selecte
TreeView1.Nodes(i).EnsureV
EndIf
Next
TreeView1.SetFocus
ASKER
What you say is straight from the VB Help, if you program the example and test it you will find that it doesn't search on partial text in the second column(sub Item) as I state in the text of my request.
For Roughneck: You can use an API call to select the whole row, see MS Site or look as some of the old VB Questions on this site.
Regards ....
Peter Annandale.
For Roughneck: You can use an API call to select the whole row, see MS Site or look as some of the old VB Questions on this site.
Regards ....
Peter Annandale.
I use this function to find in a listbox, you should try it with listview, it will maybe work?
Private Const LB_FINDSTRING = &H18F
Private Const LB_FINDSTRINGEXACT = &H1A2
Private Declare Function SendMessageStr Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lparam As String) As Long
Public Function FindListboxItem(ctrl As Control, sStr As String) As Long
' *** Find listindex based on a partial code
On Error Resume Next
FindListboxItem = SendMessageStr(ctrl.hWnd, LB_FINDSTRING, 0&, (sStr))
End Function
Private Const LB_FINDSTRING = &H18F
Private Const LB_FINDSTRINGEXACT = &H1A2
Private Declare Function SendMessageStr Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lparam As String) As Long
Public Function FindListboxItem(ctrl As Control, sStr As String) As Long
' *** Find listindex based on a partial code
On Error Resume Next
FindListboxItem = SendMessageStr(ctrl.hWnd, LB_FINDSTRING, 0&, (sStr))
End Function
ASKER
I have found a solution by searching the item and subitem using the "like" operator and then assigning the whole item to a variable and feeding the variable into the finditem function. This works well and doesn't sem to add much more time for the search.
It would seem that you cannot search a subitem of a listbox with partial text, you need to supply the wholtext for it to gain a match (testing I carried out seems to justify this).
I thank you for your comments and time, and am willing to conceed the points to you.
It would seem that you cannot search a subitem of a listbox with partial text, you need to supply the wholtext for it to gain a match (testing I carried out seems to justify this).
I thank you for your comments and time, and am willing to conceed the points to you.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER