Listview item select and deselect.. 
Author Message
 Listview item select and deselect..

I'm having trouble with programmatically selecting items in a listview
and being able to reset that and do it again.. here is a better
explanation. I have a combo box and a listview. The combo box is an
input field for a search of the listview. when I do the first search, it

works perfectly.. But, subsequent searches will not work until I
physically click an item with the mouse. i.e I'm using

Call SendMessage(lstview.hwnd, LVM_SETITEMSTATE, -1, LV) to clear it of
all selections then
Call SendMessage(lstview.hwnd, LVM_SETITEMSTATE, 0, LV) to select the
first item in the listview.

Is there something else I need to feed it to tell it that I did that? I
mean is it just a visual and not returning that something is actually
selected? My code for the whole process is long and convoluted, but I
can post key parts if it will help at all.. is there another way to do
this? I tried the listview.listitem.selected but I'm getting that it's
read-only at runtime.. that's why I opted to use the API route..

TIA

Matt



Thu, 03 Oct 2002 03:00:00 GMT  
 Listview item select and deselect..
Do you really need the API ? This works repeatedly on my system ....

   Dim strSearch As String
   Dim itmX As ListItem

   strSearch = Text1.Text  'sub a combo text if desired

  'search for partial matches in the
  'listview item text column. Could
  'change lvwPartial to lvwWholeWord
  'to force exact matches instead
   Set itmX = ListView1.FindItem(strSearch, lvwText, 1, lvwPartial)

  'if a match is found
   If Not itmX Is Nothing Then

     'clear any previous selection
      ListView1.SelectedItem.Selected = False

     'select the matching item
      itmX.Selected = True

     'and bring into view
      itmX.EnsureVisible
   End If

--

Randy Birch, MVP Visual Basic

http://www.mvps.org/vbnet/
http://www.mvps.org/ccrp/

Please correspond only using the newsgroups so all can benefit.



| I'm having trouble with programmatically selecting items in a listview
| and being able to reset that and do it again.. here is a better
| explanation. I have a combo box and a listview. The combo box is an
| input field for a search of the listview. when I do the first search, it
|
| works perfectly.. But, subsequent searches will not work until I
| physically click an item with the mouse. i.e I'm using
|
| Call SendMessage(lstview.hwnd, LVM_SETITEMSTATE, -1, LV) to clear it of
| all selections then
| Call SendMessage(lstview.hwnd, LVM_SETITEMSTATE, 0, LV) to select the
| first item in the listview.
|
| Is there something else I need to feed it to tell it that I did that? I
| mean is it just a visual and not returning that something is actually
| selected? My code for the whole process is long and convoluted, but I
| can post key parts if it will help at all.. is there another way to do
| this? I tried the listview.listitem.selected but I'm getting that it's
| read-only at runtime.. that's why I opted to use the API route..
|
| TIA
|
| Matt
|



Thu, 03 Oct 2002 03:00:00 GMT  
 Listview item select and deselect..
Randy,

    Your code is definitely more compact, but it doesn't do what I'm
trying to do. I need to search the main listitem and all subitems (only
2 per listitem) and have all of the matching items selected
simultaneously when the enter key is pressed (full row select is on).
Then the user can type in a new search with all previously selected
items being cleared and a new search performed and the process repeats.
I couldn't find a way to do it with regular code and that's why I tried
the API route. If you have any ideas to will get me around the API, I'd
be happy to try them

TIA

Matt



Sat, 05 Oct 2002 03:00:00 GMT  
 Listview item select and deselect..
Here ... toss into a command button ....

Private Sub Command1_Click()

   Dim strSearch As String
   Dim itmX As ListItem
   Dim n As Long

   Dim LV As LVITEM
   Const LVM_FIRST = &H1000
   Const LVM_SETITEMSTATE = (LVM_FIRST + 43)
   Const LVM_GETSELECTEDCOUNT = (LVM_FIRST + 50)
   Const LVIF_STATE = &H8
   Const LVIS_SELECTED = &H2

  'clear existing selections
   With LV
      .mask = LVIF_STATE
      .state = False
      .stateMask = LVIS_SELECTED
   End With

   Call SendMessage(ListView1.hwnd, LVM_SETITEMSTATE, -1, LV)

  'what to find
   strSearch = Text2.Text

  'search for partial matches in the
  'listview item text column. Could
  'change lvwPartial to lvwWhole
  'to force exact matches instead

   'For Each itmX In ListView1.ListItems
   For n = 1 To ListView1.ListItems.Count

      Set itmX = ListView1.FindItem(strSearch, lvwText, n, lvwPartial)

      If itmX Is Nothing Then

        'subitems only support exact matches
         Set itmX = ListView1.FindItem(strSearch, lvwSubItem, n, lvwWhole)

      End If

     'if a match is found
      If Not itmX Is Nothing Then

        'select the item
         itmX.Selected = True

        'and bring into view
        'itmX.EnsureVisible

      End If

    '  Set itmX = Nothing

   Next

  'return the number of selected items
   Text3.Text = SendMessage(ListView1.hwnd, LVM_GETSELECTEDCOUNT, 0&, ByVal
0&)

End Sub

--

Randy Birch, MVP Visual Basic

http://www.mvps.org/vbnet/
http://www.mvps.org/ccrp/

Please correspond only using the newsgroups so all can benefit.



| Randy,
|
|     Your code is definitely more compact, but it doesn't do what I'm
| trying to do. I need to search the main listitem and all subitems (only
| 2 per listitem) and have all of the matching items selected
| simultaneously when the enter key is pressed (full row select is on).
| Then the user can type in a new search with all previously selected
| items being cleared and a new search performed and the process repeats.
| I couldn't find a way to do it with regular code and that's why I tried
| the API route. If you have any ideas to will get me around the API, I'd
| be happy to try them
|
| TIA
|
| Matt
|



Sat, 05 Oct 2002 03:00:00 GMT  
 Listview item select and deselect..
Thanks for all the help Randy.. it still doesn't work the way I need it
to. Your code checks to see if the item is the Listitem and if it is,
selects it. Only if it doesn't match the listitem will it then check the
subitems.. this is the problem I ran into with using the .Finditem
property as well. I need it to find EVERY instance of the search string
as the listitem or sub.. it doesn't matter which if the search string
is found in either, then select that row and move to the next.. if that
search string is found anywhere in that row, select it and move on to
the next one. So, after the search, all rows with that partial or whole
search string contained in it is selected. this is the function I'm
using to do my search now. MatchType is just an Enum with the 3 types
(lvwPartial, lvwText and lvwTag).. but this function is still not
working correctly if the selected item is in the middle of the list. I
was using this to select all the items and the API method to clear it
afterwards and set the selected item back to index 0.. which, should
work fine, but isn't.. I hope that explain things a bit better..

Public Function FindIt(LV As ListView, StringtoFind As String,
StringMatch As MatchType) As Integer

Dim lIndex As Long, lIndexSub As Long, sIndexItem As String, lreturn As
Long

    FindIt = 0

    If LV.ListItems.Count < 1 Then Exit Function

        LV.SelectedItem.Selected = False

        For lIndex = LV.SelectedItem.index  To LV.ListItems.Count

          sIndexItem = LV.ListItems.Item(lIndex).Text

            If InStr(sIndexItem, StringtoFind) > 0 Then
              FindIt = lIndex
              LV.ListItems.Item(lIndex).EnsureVisible
              LV.ListItems.Item(lIndex).Selected = True
            End If

            'subitems
            If LV.ColumnHeaders.Count > 1 Then

            For lIndexSub = 1 To LV.ColumnHeaders.Count - 1

               sIndexItem =
LV.ListItems.Item(lIndex).SubItems(lIndexSub)

              ' if found
              If InStr(sIndexItem, StringtoFind) > 0 Then
                FindIt = lIndex
                LV.ListItems.Item(lIndex).EnsureVisible
                LV.ListItems.Item(lIndex).Selected = True
              End If

            ' move to next subitem

            Next lIndexSub

        End If

    ' move to next row

Next lIndex

End Function

TA

Matt



Sat, 05 Oct 2002 03:00:00 GMT  
 Listview item select and deselect..
Your code works here with the following change ....

Change "For lIndex = LV.SelectedItem.index To LV.ListItems.Count" to "For
lIndex = 1 To LV.ListItems.Count" (otherwise you never start at the top of
the list, but always after the last selected item.)

There's no workaround I know of for using VB's intrinsic FindItem to locate
partial matches against SubItems, nor to locate substrings within any item.
The FindItem method is designed to locate strings beginning with the first
letter.  Your solution, with the mod above, seems the best alternative.

--

Randy Birch, MVP Visual Basic

http://www.mvps.org/vbnet/
http://www.mvps.org/ccrp/

Please correspond only using the newsgroups so all can benefit.



| Thanks for all the help Randy.. it still doesn't work the way I need it
| to. Your code checks to see if the item is the Listitem and if it is,
| selects it. Only if it doesn't match the listitem will it then check the
| subitems.. this is the problem I ran into with using the .Finditem
| property as well. I need it to find EVERY instance of the search string
| as the listitem or sub.. it doesn't matter which if the search string
| is found in either, then select that row and move to the next.. if that
| search string is found anywhere in that row, select it and move on to
| the next one. So, after the search, all rows with that partial or whole
| search string contained in it is selected. this is the function I'm
| using to do my search now. MatchType is just an Enum with the 3 types
| (lvwPartial, lvwText and lvwTag).. but this function is still not
| working correctly if the selected item is in the middle of the list. I
| was using this to select all the items and the API method to clear it
| afterwards and set the selected item back to index 0.. which, should
| work fine, but isn't.. I hope that explain things a bit better..
|
| Public Function FindIt(LV As ListView, StringtoFind As String,
| StringMatch As MatchType) As Integer
|
| Dim lIndex As Long, lIndexSub As Long, sIndexItem As String, lreturn As
| Long
|
|     FindIt = 0
|
|     If LV.ListItems.Count < 1 Then Exit Function
|
|         LV.SelectedItem.Selected = False
|
|         For lIndex = LV.SelectedItem.index  To LV.ListItems.Count
|
|           sIndexItem = LV.ListItems.Item(lIndex).Text
|
|             If InStr(sIndexItem, StringtoFind) > 0 Then
|               FindIt = lIndex
|               LV.ListItems.Item(lIndex).EnsureVisible
|               LV.ListItems.Item(lIndex).Selected = True
|             End If
|
|             'subitems
|             If LV.ColumnHeaders.Count > 1 Then
|
|             For lIndexSub = 1 To LV.ColumnHeaders.Count - 1
|
|                sIndexItem =
| LV.ListItems.Item(lIndex).SubItems(lIndexSub)
|
|               ' if found
|               If InStr(sIndexItem, StringtoFind) > 0 Then
|                 FindIt = lIndex
|                 LV.ListItems.Item(lIndex).EnsureVisible
|                 LV.ListItems.Item(lIndex).Selected = True
|               End If
|
|             ' move to next subitem
|
|             Next lIndexSub
|
|         End If
|
|     ' move to next row
|
| Next lIndex
|
| End Function
|
|
| TA
|
| Matt
|



Sat, 05 Oct 2002 03:00:00 GMT  
 Listview item select and deselect..
It did have to be something simple didn't it? <g>

thanks a million Randy!!

Matt



Mon, 07 Oct 2002 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. MS ListView control deselects selected items when it regains focus

2. MS ListView control deselects selected items when it regains focus

3. Help cant deselect item in listview

4. how to deselect all items in a listview?

5. jump listview to the hilight item (selected item)...

6. Why does the previous selected item in a ListView get selected for a short moment

7. ListView control - Keeping selected items looking selected

8. ListView Control - Removing selected items from the list

9. List of selected items in a listview

10. ListView - How to programatically select an item

11. Select automatically an item in a ListView

12. How to select all items in a ListView (searching for a faster way)

 

 
Powered by phpBB® Forum Software