Solved

ListView - search on SubItem

Posted on 1998-09-08
8
3,602 Views
Last Modified: 2011-02-15
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.
0
Comment
Question by:annandap
  • 4
  • 3
8 Comments
 

Author Comment

by:annandap
Comment Utility
Edited text of question
0
 

Expert Comment

by:roughneck
Comment Utility
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.
0
 
LVL 14

Expert Comment

by:waty
Comment Utility
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",,,lvwpartial)

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).OpenDatabase("BIBLIO.MDB")
    ' Set the recordset to the Publishers table.
    Set myRs = myDb.OpenRecordset("Publishers", 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

0
 
LVL 14

Expert Comment

by:waty
Comment Utility
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).Selected = true
     TreeView1.Nodes(i).EnsureVisible
   EndIf
Next

TreeView1.SetFocus

0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:annandap
Comment Utility
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.
0
 
LVL 14

Expert Comment

by:waty
Comment Utility
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

0
 

Author Comment

by:annandap
Comment Utility
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.
0
 
LVL 14

Accepted Solution

by:
waty earned 200 total points
Comment Utility
Thanks a lot.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now