PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] Listview.FindItem within SPECIFIC lsvSubItem column-VBForums
Results 1 to 10 of 10

Thread: [RESOLVED] Listview.FindItem within SPECIFIC lsvSubItem column

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    Resolved [RESOLVED] Listview.FindItem within SPECIFIC lsvSubItem column

    Is there a way to do the find based off a specific column?

    Besides checking the contents of each sub column in the found listitem?
    Please RATE posts, click the RATE button to the left under the Users Name.

    Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.


    "As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." 1998 Jeremy J Swartwood

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,135

    Re: Listview.FindItem within SPECIFIC lsvSubItem column

    FindItem method should do it - just specify where to search (lvwSubItem):
    Code:
    Private Sub btnFidnSubItem_Click()
    Dim itm As MSComctlLib.ListItem
    
        Set itm = ListView1.FindItem("subitemtext", lvwSubItem)
        'for partial search use this:
        '''Set itm = ListView1.FindItem("partial text goes here", lvwSubItem, , lvwPartial)
        If Not itm Is Nothing Then
            itm.EnsureVisible
            itm.Selected = True
            ListView1.SetFocus
        End If
    
    End Sub

  3. #3
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Candon City, Ilocos Sur, Phils.
    Posts
    11,038

    Re: Listview.FindItem within SPECIFIC lsvSubItem column

    If memory doesn't fail me then I believe using lvwSubItem will search all subitems in all the columns and you cannot search for a specific column, if you want to search a specific column then you will have to use a loop.

    What did you mean by "Besides checking the contents of each sub column in the found listitem?"?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  4. #4
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,135

    Re: Listview.FindItem within SPECIFIC lsvSubItem column

    Quote Originally Posted by dee-u View Post
    If memory doesn't fail me then I believe using lvwSubItem will search all subitems in all the columns and you cannot search for a specific column...
    No, that is not correct - you can search specific column as well: one of the arguments is actually referring to column index.
    Code:
    'to search the whole text use this:
    Set itm = ListView1.FindItem("some text goes here", lvwSubItem, 2)
    
    'to search the partial text use this:
    Set itm = ListView1.FindItem("some text goes here", lvwSubItem, 2, lvwPartial)
    Note: in this case 2 means Column 2 wich corresponds to first subitem (Column 1 is the item).

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    Re: Listview.FindItem within SPECIFIC lsvSubItem column

    Quote Originally Posted by dee-u View Post
    If memory doesn't fail me then I believe using lvwSubItem will search all subitems in all the columns and you cannot search for a specific column, if you want to search a specific column then you will have to use a loop.

    What did you mean by "Besides checking the contents of each sub column in the found listitem?"?
    I meant exactly what you suggested, to loop through the subitems.


    Quote Originally Posted by RhinoBull View Post
    No, that is not correct - you can search specific column as well: one of the arguments is actually referring to column index.
    Hmm, I thought that as well, but when performing the search with Index, it throws an error.

    I did a little reading and my interpretation of it, is that the Index is the Row # you want to start searching at. Not which column index of the sub items to look in.

    Here is taken from Microsoft
    http://msdn.microsoft.com/en-us/libr...32(VS.60).aspx

    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.
    If you do not specify an index, it will not look in the first column alone. It sounds like Index is the row # of the all the rows/items.
    Please RATE posts, click the RATE button to the left under the Users Name.

    Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.


    "As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." 1998 Jeremy J Swartwood

  6. #6
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,135

    Re: Listview.FindItem within SPECIFIC lsvSubItem column

    Not sure how you can get an error - index in this context means "column index" but remember that all indexing in the listview control starts with 1.
    So, if you want to search withing first subitem then you need to specify 2 as third argument.
    If you omit that then search will be done on the first column regardless of second argument's value.

    It's pretty straight forward. All my test searches returned proper rows.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    Re: Listview.FindItem within SPECIFIC lsvSubItem column

    First I wanted to thank you for answering this question, and helping me with this.

    Quote Originally Posted by RhinoBull View Post
    index in this context means "column index" but remember that all indexing in the listview control starts with 1.
    So, if you want to search withing first subitem then you need to specify 2 as third argument.
    The code I provide below returns an error, but when I read your statement above It sounds like it should not be returning an error. (Err: 380, Invalid Property Value)

    If you add 7 columns, and 4 rows of data, and try to search the column with "INDEX" 5, you should either get a result, or not. However I get an error. This leads me to belive that you are infact searching starting at the Nth item. Where "INDEX" refers (Similar to Mid,Instr, etc) to the Row position to start searching at.

    Quote Originally Posted by RhinoBull View Post
    If you omit that then search will be done on the first column regardless of second argument's value.

    It's pretty straight forward. All my test searches returned proper rows.
    If you do a search with my code below, but remove the Index, it returns the first record. Which there is no "California" in the word "Bob".


    Create a new VB6 project and add 1 listview, 2 command buttons.
    vb Code:
    1. Private Sub Command1_Click()
    2.     Dim oItem As ListItem
    3.     'Add some test items
    4.     Set oItem = Me.ListView1.ListItems.Add
    5.     oItem.Text = "1"
    6.     With oItem.ListSubItems
    7.         .Add , , "Bob"
    8.         .Add , , "California"
    9.         .Add , , "4"
    10.         .Add , , "col5"
    11.         .Add , , "col6"
    12.         .Add , , "col7"
    13.     End With
    14.     Set oItem = Me.ListView1.ListItems.Add
    15.     oItem.Text = "2"
    16.     With oItem.ListSubItems
    17.         .Add , , "Tom"
    18.         .Add , , "Florida"
    19.         .Add , , "3"
    20.         .Add , , "col5"
    21.         .Add , , "col6"
    22.         .Add , , "col7"
    23.     End With
    24.     Set oItem = Me.ListView1.ListItems.Add
    25.     oItem.Text = "3"
    26.     With oItem.ListSubItems
    27.         .Add , , "Sue"
    28.         .Add , , "Alaska"
    29.         .Add , , "2"
    30.         .Add , , "col5"
    31.         .Add , , "col6"
    32.         .Add , , "col7"
    33.     End With
    34.     Set oItem = Me.ListView1.ListItems.Add
    35.     oItem.Text = "4"
    36.     With oItem.ListSubItems
    37.         .Add , , "Jennifer"
    38.         .Add , , "Hawaii"
    39.         .Add , , "1"
    40.         .Add , , "col5"
    41.         .Add , , "col6"
    42.         .Add , , "col7"
    43.     End With
    44.     If Not oItem Is Nothing Then Set oItem = Nothing
    45. End Sub
    46.  
    47. Private Sub Command2_Click()
    48.     Dim oItem As ListItem
    49.     Set oItem = Me.ListView1.FindItem("California", lvwSubItem, 5)
    50.     If Not oItem Is Nothing Then
    51.         MsgBox "The Record ID returned is: " & oItem.Text
    52.         Set oItem = Nothing
    53.     Else
    54.         MsgBox "No Items Found"
    55.     End If
    56. End Sub
    57.  
    58. Private Sub Form_Load()
    59.     With Me.ListView1
    60.         'Set up listview
    61.         .View = lvwReport
    62.         .LabelEdit = lvwManual
    63.         .AllowColumnReorder = True
    64.         .GridLines = True
    65.         .FullRowSelect = True
    66.         .HideSelection = False
    67.         'Setup Headers
    68.         .ColumnHeaders.Clear
    69.         .ColumnHeaders.Add , , "ID", 700
    70.         .ColumnHeaders.Add , , "Name", 1440
    71.         .ColumnHeaders.Add , , "Location", 1440
    72.         .ColumnHeaders.Add , , "False ID", 700
    73.         .ColumnHeaders.Add , , "Col5", 900
    74.         .ColumnHeaders.Add , , "Col6", 900
    75.         .ColumnHeaders.Add , , "Col7", 900
    76.     End With
    77. End Sub
    Last edited by rack; Jan 5th, 2010 at 06:50 PM.
    Please RATE posts, click the RATE button to the left under the Users Name.

    Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.


    "As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." 1998 Jeremy J Swartwood

  8. #8
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,135

    Re: Listview.FindItem within SPECIFIC lsvSubItem column

    Well, I see where the error comes from - apparently integer is evaluated vs number of items in the list instead of being applied to column index.
    The problem that I have with msdn is the fact that there is no mention about Index being applied to Item.Index property even though we are explicitely search within subitems - in my mind index should be applied to ColumnHeaders coleection instead (in this case).
    Coincidentally I always had number of items much greater then number of columns so I've never seen that error...

    Another observation - index is kind of useless...
    If you try this:
    Code:
        Set oItem = Me.ListView1.FindItem("Alaska", lvwSubItem, 1)
        If Not oItem Is Nothing Then
            'MsgBox "The Record ID returned is: " & oItem.Text
            oItem.Selected = True
            ListView1.SetFocus
        Else
            MsgBox "No Items Found"
        End If
    you will see that proper item is being selected although index suggests to search item1.
    As far as I understand index is applied only if there are multiple items with identical subitems - if index is omitted then first in the list gets selected.

    Go figure... Sorry for the confusion.

  9. #9
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Candon City, Ilocos Sur, Phils.
    Posts
    11,038

    Re: Listview.FindItem within SPECIFIC lsvSubItem column

    Since it looks like it is not possible to search a particular column, an alternative is to store the data in the tag property of the listitem and you will be able to search for that using the lvwTag parameter in the FindMethod.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    Re: Listview.FindItem within SPECIFIC lsvSubItem column

    Quote Originally Posted by dee-u View Post
    Since it looks like it is not possible to search a particular column, an alternative is to store the data in the tag property of the listitem and you will be able to search for that using the lvwTag parameter in the FindMethod.
    Thats a good alternative since I'm basically concerned with 2 IDs. One could be the text property and the other the tag.

    RhinoBull, I appreciated all your help on this, I appreciate the time/effort.
    Please RATE posts, click the RATE button to the left under the Users Name.

    Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.


    "As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." 1998 Jeremy J Swartwood

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width