473,399 Members | 2,278 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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 22254
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: MikeY | last post by:
Hopefully someone can help me on this. I am using C#, making Windows forms. I have created a listView with checkbox's. I have enabled the checkboxes under the properties, and all the data,...
2
by: George | last post by:
Hello, I want to update an item in a listview, to perform this I search the items collection to find the item with the corresponding text. Is there a more efficient way to perform this? In a...
4
by: Tee | last post by:
Hi, Need some help with listview, is there a function that can search the listview for a specified string and return true/false ? Thanks, Tee
1
by: Chris | last post by:
Hi all, I posted the following in microsoft.public.dotnet.framework.windowsforms but it seems that group has little traffic. Hi all, I have a listview box which is populated from methods of...
3
by: Michael.Suarez | last post by:
Is it me, or does it seem like they put no effort into creating the listview control in .Net. listview. A few gripes I have with .Net listview that aren't present in vb6: -Inability to set...
12
by: garyusenet | last post by:
I have had no replies to my previous post so perhaps I didn't write it good enough. Please excuse new thread but i wanted to break from the last thread hopefully this thread will be better. ...
4
by: spowel4 | last post by:
My form has a listview populated with the 50 states. When the user checks the checkbox within the listview for a particular state, I need to retrieve which state was checked (i.e. if AZ is checked...
4
by: Bill-R | last post by:
I'm trying to convert a vb6 program to vb.net (vb2008 express) I have text data in a Listview Control I use a Textbox to enter characters to search the Listview When a Match is found, I use...
0
by: =?Utf-8?B?TWlrZSBDb2xsaW5z?= | last post by:
I have a listview that when I select an item, it populates a details view. I want to show the item that was selected in the listview by changing it to yellow. Trouble is, the selected item does not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.