468,720 Members | 1,715 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,720 developers. It's quick & easy.

How do I do a ListView Search?

Using VB6 (for two weeks!)

I could get a ListBox search working perfectly but with a ListView it has
completely stumped me. I've not found any previous posts that have helped :(

The user enters a string into a text box (txtStreet) and as they type any
matching entry in the listview (lsvStreets) is highlighted and made visible.
The search highlights the first item starting with the text in txtStreet.

The listview contains 6,000 rows of 7 columns, only the first column is
relevent in any search.

With the ListBox I was using...

Private Declare Function SendMessage Lib "User32" _
Alias "SendMessageA" (ByVal _
hWnd As Long, _
ByVal wMsg As Integer, _
ByVal wParam As String, _
lParam As Any) As Long

Const LB_FINDSTRING = &H18F

And in the txtStreet_Change function:

MsgBox SendMessage(lsvStreets.hWnd, LB_FINDSTRING, txtStreet ByVal
txtStreet.Text)

I'm going round the web looking for examples, tutorials, etc but so far
nothing has helped or worked.

Thanks.
Jul 17 '05 #1
8 21447
Progress...I managed to finally get it working with the following code,
which leads me to a new problem :)

I would like the found item to be highlighted and also to be preferably the
top visible item, I would really appreciate any help anyone can give me with
this.

Thanks...
Jon R.

=== Code ===

Private Sub cmdSearch_Click()
Static lastIndex As Long
Dim nIndex As Long
Dim itmx As ListItem

If txtStreet <> "" Then
Set itmx = lsvStreets.FindItem(txtStreet, lvwText, 1, lvwPartial)
If Not itmx Is Nothing Then
itmx.Selected = True
itmx.EnsureVisible
lastIndex = itmx.Index
End If
End If
End Sub

Jul 17 '05 #2
Private Const LVM_FIRST = &H1000
Private Const LVM_GETCOUNTPERPAGE As Long = (LVM_FIRST + 40)

Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long

Private Sub Command1_Click()

Static lastIndex As Long
Dim nIndex As Long
Dim itmx As ListItem
Dim topIndex As Long

Set itmx = ListView1.FindItem(txtStreet.Text, lvwText, 1, lvwPartial)

If Not itmx Is Nothing Then
topIndex = SetListViewTopIndex(itmx.Index)
itmx.Selected = True
lastIndex = itmx.Index
End If

Label1.Caption = topIndex

End Sub
Private Function SetListViewTopIndex(lv As ListView, ByVal itemToTop As
Long) As Long

Dim lvItemsPerPage As Long
Dim lvNeededItems As Long

'determine if desired index + number
'of items in view will exceed total
'items in the control
lvItemsPerPage = GetListviewVisibleCount(lv.hwnd)
lvNeededItems = (itemToTop - lvItemsPerPage)

If (itemToTop + lvItemsPerPage) > lv.ListItems.Count Then

'yes, so scroll end of listview
'into view
lv.ListItems(lv.ListItems.Count).EnsureVisible
SetListViewTopIndex = (lv.ListItems.Count - lvItemsPerPage) + 1
Else

lv.ListItems((itemToTop + lvItemsPerPage) - 1).EnsureVisible
SetListViewTopIndex = itemToTop

End If

End Function
Private Function GetListviewVisibleCount() As Long

GetListviewVisibleCount = SendMessage(ListView1.hwnd, _
LVM_GETCOUNTPERPAGE, _
0&, _
ByVal 0&)

End Function

--

Randy Birch
MVP Visual Basic
http://vbnet.mvps.org/
Please respond only to the newsgroups so all can benefit.

There's no place like 127.0.0.1
"Jon Ripley" <ne**@stryker.freeserve.co.uk> wrote in message
news:NR**********************@news-text.cableinet.net...
: Progress...I managed to finally get it working with the following code,
: which leads me to a new problem :)
:
: I would like the found item to be highlighted and also to be preferably
the
: top visible item, I would really appreciate any help anyone can give me
with
: this.
:
: Thanks...
: Jon R.
:
: === Code ===
:
: Private Sub cmdSearch_Click()
: Static lastIndex As Long
: Dim nIndex As Long
: Dim itmx As ListItem
:
: If txtStreet <> "" Then
: Set itmx = lsvStreets.FindItem(txtStreet, lvwText, 1, lvwPartial)
: If Not itmx Is Nothing Then
: itmx.Selected = True
: itmx.EnsureVisible
: lastIndex = itmx.Index
: End If
: End If
: End Sub
:
:
:
Jul 17 '05 #3
....whoops ... the call to the topindex routine should have read:

topIndex = SetListViewTopIndex(ListView1, itmx.Index)

--

Randy Birch
MVP Visual Basic
http://vbnet.mvps.org/
Please respond only to the newsgroups so all can benefit.

There's no place like 127.0.0.1
"Randy Birch" <rg************@mvps.org> wrote in message
news:d3*****************@news01.bloor.is.net.cable .rogers.com...
: Private Const LVM_FIRST = &H1000
: Private Const LVM_GETCOUNTPERPAGE As Long = (LVM_FIRST + 40)
:
: Private Declare Function SendMessage Lib "user32" _
: Alias "SendMessageA" _
: (ByVal hwnd As Long, _
: ByVal wMsg As Long, _
: ByVal wParam As Long, _
: lParam As Any) As Long
:
:
:
: Private Sub Command1_Click()
:
: Static lastIndex As Long
: Dim nIndex As Long
: Dim itmx As ListItem
: Dim topIndex As Long
:
: Set itmx = ListView1.FindItem(txtStreet.Text, lvwText, 1, lvwPartial)
:
: If Not itmx Is Nothing Then
: topIndex = SetListViewTopIndex(itmx.Index)
: itmx.Selected = True
: lastIndex = itmx.Index
: End If
:
: Label1.Caption = topIndex
:
: End Sub
:
:
: Private Function SetListViewTopIndex(lv As ListView, ByVal itemToTop As
: Long) As Long
:
: Dim lvItemsPerPage As Long
: Dim lvNeededItems As Long
:
: 'determine if desired index + number
: 'of items in view will exceed total
: 'items in the control
: lvItemsPerPage = GetListviewVisibleCount(lv.hwnd)
: lvNeededItems = (itemToTop - lvItemsPerPage)
:
: If (itemToTop + lvItemsPerPage) > lv.ListItems.Count Then
:
: 'yes, so scroll end of listview
: 'into view
: lv.ListItems(lv.ListItems.Count).EnsureVisible
: SetListViewTopIndex = (lv.ListItems.Count - lvItemsPerPage) + 1
: Else
:
: lv.ListItems((itemToTop + lvItemsPerPage) - 1).EnsureVisible
: SetListViewTopIndex = itemToTop
:
: End If
:
: End Function
:
:
: Private Function GetListviewVisibleCount() As Long
:
: GetListviewVisibleCount = SendMessage(ListView1.hwnd, _
: LVM_GETCOUNTPERPAGE, _
: 0&, _
: ByVal 0&)
:
: End Function
:
:
:
: --
:
: Randy Birch
: MVP Visual Basic
: http://vbnet.mvps.org/
: Please respond only to the newsgroups so all can benefit.
:
: There's no place like 127.0.0.1
:
:
: "Jon Ripley" <ne**@stryker.freeserve.co.uk> wrote in message
: news:NR**********************@news-text.cableinet.net...
: : Progress...I managed to finally get it working with the following code,
: : which leads me to a new problem :)
: :
: : I would like the found item to be highlighted and also to be preferably
: the
: : top visible item, I would really appreciate any help anyone can give me
: with
: : this.
: :
: : Thanks...
: : Jon R.
: :
: : === Code ===
: :
: : Private Sub cmdSearch_Click()
: : Static lastIndex As Long
: : Dim nIndex As Long
: : Dim itmx As ListItem
: :
: : If txtStreet <> "" Then
: : Set itmx = lsvStreets.FindItem(txtStreet, lvwText, 1, lvwPartial)
: : If Not itmx Is Nothing Then
: : itmx.Selected = True
: : itmx.EnsureVisible
: : lastIndex = itmx.Index
: : End If
: : End If
: : End Sub
: :
: :
: :
:
:
Jul 17 '05 #4
Damn ... hang on, that only works if the index to set is further down the
list....

--

Randy Birch
MVP Visual Basic
http://vbnet.mvps.org/
Please respond only to the newsgroups so all can benefit.

There's no place like 127.0.0.1
"Randy Birch" <rg************@mvps.org> wrote in message
news:%3*****************@news01.bloor.is.net.cable .rogers.com...
: ...whoops ... the call to the topindex routine should have read:
:
: topIndex = SetListViewTopIndex(ListView1, itmx.Index)
:
: --
:
: Randy Birch
: MVP Visual Basic
: http://vbnet.mvps.org/
: Please respond only to the newsgroups so all can benefit.
:
: There's no place like 127.0.0.1
:
:
: "Randy Birch" <rg************@mvps.org> wrote in message
: news:d3*****************@news01.bloor.is.net.cable .rogers.com...
: : Private Const LVM_FIRST = &H1000
: : Private Const LVM_GETCOUNTPERPAGE As Long = (LVM_FIRST + 40)
: :
: : Private Declare Function SendMessage Lib "user32" _
: : Alias "SendMessageA" _
: : (ByVal hwnd As Long, _
: : ByVal wMsg As Long, _
: : ByVal wParam As Long, _
: : lParam As Any) As Long
: :
: :
: :
: : Private Sub Command1_Click()
: :
: : Static lastIndex As Long
: : Dim nIndex As Long
: : Dim itmx As ListItem
: : Dim topIndex As Long
: :
: : Set itmx = ListView1.FindItem(txtStreet.Text, lvwText, 1, lvwPartial)
: :
: : If Not itmx Is Nothing Then
: : topIndex = SetListViewTopIndex(itmx.Index)
: : itmx.Selected = True
: : lastIndex = itmx.Index
: : End If
: :
: : Label1.Caption = topIndex
: :
: : End Sub
: :
: :
: : Private Function SetListViewTopIndex(lv As ListView, ByVal itemToTop As
: : Long) As Long
: :
: : Dim lvItemsPerPage As Long
: : Dim lvNeededItems As Long
: :
: : 'determine if desired index + number
: : 'of items in view will exceed total
: : 'items in the control
: : lvItemsPerPage = GetListviewVisibleCount(lv.hwnd)
: : lvNeededItems = (itemToTop - lvItemsPerPage)
: :
: : If (itemToTop + lvItemsPerPage) > lv.ListItems.Count Then
: :
: : 'yes, so scroll end of listview
: : 'into view
: : lv.ListItems(lv.ListItems.Count).EnsureVisible
: : SetListViewTopIndex = (lv.ListItems.Count - lvItemsPerPage) + 1
: : Else
: :
: : lv.ListItems((itemToTop + lvItemsPerPage) - 1).EnsureVisible
: : SetListViewTopIndex = itemToTop
: :
: : End If
: :
: : End Function
: :
: :
: : Private Function GetListviewVisibleCount() As Long
: :
: : GetListviewVisibleCount = SendMessage(ListView1.hwnd, _
: : LVM_GETCOUNTPERPAGE, _
: : 0&, _
: : ByVal 0&)
: :
: : End Function
: :
: :
: :
: : --
: :
: : Randy Birch
: : MVP Visual Basic
: : http://vbnet.mvps.org/
: : Please respond only to the newsgroups so all can benefit.
: :
: : There's no place like 127.0.0.1
: :
: :
: : "Jon Ripley" <ne**@stryker.freeserve.co.uk> wrote in message
: : news:NR**********************@news-text.cableinet.net...
: : : Progress...I managed to finally get it working with the following
code,
: : : which leads me to a new problem :)
: : :
: : : I would like the found item to be highlighted and also to be
preferably
: : the
: : : top visible item, I would really appreciate any help anyone can give
me
: : with
: : : this.
: : :
: : : Thanks...
: : : Jon R.
: : :
: : : === Code ===
: : :
: : : Private Sub cmdSearch_Click()
: : : Static lastIndex As Long
: : : Dim nIndex As Long
: : : Dim itmx As ListItem
: : :
: : : If txtStreet <> "" Then
: : : Set itmx = lsvStreets.FindItem(txtStreet, lvwText, 1, lvwPartial)
: : : If Not itmx Is Nothing Then
: : : itmx.Selected = True
: : : itmx.EnsureVisible
: : : lastIndex = itmx.Index
: : : End If
: : : End If
: : : End Sub
: : :
: : :
: : :
: :
: :
:
:
Jul 17 '05 #5
Many thanks, got it working now :)

Jon R.
Jul 17 '05 #6
Here's the revised code ... the demo uses a listview, label and three
command buttons....

Option Explicit

Private Const LVM_FIRST = &H1000
Private Const LVM_GETTOPINDEX = (LVM_FIRST + 39)
Private Const LVM_GETCOUNTPERPAGE As Long = (LVM_FIRST + 40)

Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Private Function ListView_SetTopIndex(lv As ListView, ByVal itemToTop As
Long) As Long

Dim lvItemsPerPage As Long
Dim lvNeededItems As Long
Dim lvCurrentTopIndex As Long

'determine if desired index + number
'of items in view will exceed total
'items in the control
lvCurrentTopIndex = ListView_GetTopIndex(lv.hwnd) + 1 '0-based!
lvItemsPerPage = ListView_GetVisibleCount(lv.hwnd)
lvNeededItems = (itemToTop - lvItemsPerPage)

'is current index above or below
'desired index?
If lvCurrentTopIndex > itemToTop Then

'it is above the desired index, so
'scroll up. The item will automatically
'be positioned at the top
lv.ListItems((itemToTop)).EnsureVisible

Else

'it's below, so based on whether there
'are sufficient items to set to the topindex ...
If (itemToTop + lvItemsPerPage) > lv.ListItems.Count Then

'it is below but it can't be set to
'the top as the control has insufficient
'items, so just scroll to the end of listview
lv.ListItems(lv.ListItems.Count).EnsureVisible

Else

'it is below, and since a listview
'always moves the item just into view,
'have it instead move to the top by
'faking item we want to 'EnsureVisible'
'the item lvItemsPerPage -1 below the actual
'index of interest.
lv.ListItems((itemToTop + lvItemsPerPage) - 1).EnsureVisible

End If

End If

'return a 1-based top index
'as sign of success.
ListView_SetTopIndex = ListView_GetTopIndex(lv.hwnd) + 1

End Function
Private Function ListView_GetTopIndex(hwndlv As Long) As Long

ListView_GetTopIndex = SendMessage(hwndlv, _
LVM_GETTOPINDEX, _
0&, _
ByVal 0&)

End Function
Private Function ListView_GetVisibleCount(ByVal hwndlv As Long) As Long

ListView_GetVisibleCount = SendMessage(hwndlv, _
LVM_GETCOUNTPERPAGE, _
0&, _
ByVal 0&)

End Function
Private Sub Command1_Click()

Static lastIndex As Long
Dim nIndex As Long
Dim itmx As ListItem
Dim topIndex As Long

Set itmx = ListView1.FindItem("main item100", lvwText, 1, lvwPartial)

If Not itmx Is Nothing Then
topIndex = ListView_SetTopIndex(ListView1, itmx.Index)
itmx.Selected = True
lastIndex = itmx.Index
End If

Label1.Caption = topIndex

End Sub

Private Sub Command2_Click()

Static lastIndex As Long
Dim nIndex As Long
Dim itmx As ListItem
Dim topIndex As Long

Set itmx = ListView1.FindItem("main item197", lvwText, 1, lvwPartial)

If Not itmx Is Nothing Then
topIndex = ListView_SetTopIndex(ListView1, itmx.Index)
itmx.Selected = True
lastIndex = itmx.Index
End If

Label1.Caption = topIndex

End Sub

Private Sub Command3_Click()

Static lastIndex As Long
Dim nIndex As Long
Dim itmx As ListItem
Dim topIndex As Long

Set itmx = ListView1.FindItem("main item2", lvwText, 1, lvwPartial)

If Not itmx Is Nothing Then
topIndex = ListView_SetTopIndex(ListView1, itmx.Index)
itmx.Selected = True
lastIndex = itmx.Index
End If

Label1.Caption = topIndex
End Sub
Private Sub Form_Load()

Dim itmx As ListItem
Dim cnt As Long

With ListView1
.ColumnHeaders.Add , , "main"
.ColumnHeaders.Add , , "sub 1"
.ColumnHeaders.Add , , "sub 2"
.ColumnHeaders.Add , , "sub 3"

For cnt = 1 To 200
Set itmx = .ListItems.Add(, , "main item" & CStr(cnt))
itmx.SubItems(1) = "subitem 1," & CStr(cnt)
itmx.SubItems(2) = "subitem 3," & CStr(cnt)
itmx.SubItems(3) = "subitem 4," & CStr(cnt)
Next

.SortKey = 0
.Sorted = False
.View = lvwReport
.FullRowSelect = True
.LabelEdit = lvwManual

End With

Command1.Caption = "mid-way"
Command2.Caption = "item 197"
Command3.Caption = "item 2"

End Sub

Private Sub ListView1_ColumnClick(ByVal ColumnHeader As ColumnHeader)

'sort the items
ListView1.SortKey = ColumnHeader.Index - 1
ListView1.SortOrder = Abs(Not ListView1.SortOrder = 1)
ListView1.Sorted = True

End Sub
--

Randy Birch
MVP Visual Basic
http://vbnet.mvps.org/
Please respond only to the newsgroups so all can benefit.

There's no place like 127.0.0.1
"Jon Ripley" <ne**@stryker.freeserve.co.uk> wrote in message
news:sr**********************@news-text.cableinet.net...
: Many thanks, got it working now :)
:
: Jon R.
:
:
Jul 17 '05 #7
method
6
Randy Birch Thank u for u nice code. could tell me how i can search external listview usiing your code?Thanks
Jun 17 '06 #8
method
6
want to subscribe
Jun 17 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by MikeY | last post: by
2 posts views Thread by George | last post: by
1 post views Thread by Chris | last post: by
3 posts views Thread by Michael.Suarez | last post: by
12 posts views Thread by garyusenet | last post: by
4 posts views Thread by spowel4 | last post: by
4 posts views Thread by Bill-R | last post: by
reply views Thread by =?Utf-8?B?TWlrZSBDb2xsaW5z?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.