473,225 Members | 577 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,225 software developers and data experts.

Finding Items in ListView

Hey anyone knows how to find an item in a list view based on text ? Cant
seem to get the IndexOf method working.
would the LVM_FINDITEM method using SendMessage API work in .Net ?

Nov 20 '05 #1
12 5360
Cor
Hi Mike,

I did not try it or ever used it, but did you try this
\\\
dim i as integer = DirectCast(ListView1, IList).IndexOf("")
///

Cor
Nov 20 '05 #2
Cor,

* "Cor" <no*@non.com> scripsit:
I did not try it or ever used it, but did you try this
\\\
dim i as integer = DirectCast(ListView1, IList).IndexOf("")
///


This won't work because 'ListView' doesn't implement the 'IList'
interface, but you may want to try to cast 'ListView.Items' (which is an
'ListView.ListViewItemCollection' and implements the 'IList' interface).

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #3
Cor
I did not check it, because I knew Ilist was implementing Listview but did
not check how.

After checking it I think you are right.

So that will be
\\\
dim i as integer = DirectCast(ListView1.items, IList).IndexOf("")
///

I am answering to fast now

Cor

* "Cor" <no*@non.com> scripsit:
I did not try it or ever used it, but did you try this
\\\
dim i as integer = DirectCast(ListView1, IList).IndexOf("")
///


This won't work because 'ListView' doesn't implement the 'IList'
interface, but you may want to try to cast 'ListView.Items' (which is an
'ListView.ListViewItemCollection' and implements the 'IList' interface).

Nov 20 '05 #4
* "Cor" <no*@non.com> scripsit:
I did not check it, because I knew Ilist was implementing Listview but did
not check how.

After checking it I think you are right.

So that will be
\\\
dim i as integer = DirectCast(ListView1.items, IList).IndexOf("")
///


I am answering to fast now


I hope it will work.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #5
nah.. it doesnt work...

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:bv************@ID-208219.news.uni-berlin.de...
* "Cor" <no*@non.com> scripsit:
I did not check it, because I knew Ilist was implementing Listview but did not check how.

After checking it I think you are right.

So that will be
\\\
dim i as integer = DirectCast(ListView1.items, IList).IndexOf("")
///


I am answering to fast now


I hope it will work.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>

Nov 20 '05 #6
Cor
Hi Mike,

I assume that you get with this method only if something exist in the items
and not in the sub items, I think that there will be a possibility to use
this also on the sub items, because that is also derived from Ilist, but I
did not see it in the documentation.

Why not just do a for each loop.
The listview is a complex control you know and using it simple will helps
mostly.

Cor
Nov 20 '05 #7
yeah resorted to looping it but not the real best solution rite ?

used to be a sendmessage() API call that would work.. i tried using it in
..net but it didnt seem to work ....

the ListView.Items.IndexOf(listitem) does manage to find the listitem object
but thats if we are using a unique listitem each time we add an item into
the list. Who would do such a thing? Of course we're gonna reuse the same
variable in a loop to add items into the listview. There should be some
faster way to find an item/subitem in a listview more efficiently ?
"Cor" <no*@non.com> wrote in message
news:es**************@TK2MSFTNGP11.phx.gbl...
Hi Mike,

I assume that you get with this method only if something exist in the items and not in the sub items, I think that there will be a possibility to use
this also on the sub items, because that is also derived from Ilist, but I
did not see it in the documentation.

Why not just do a for each loop.
The listview is a complex control you know and using it simple will helps
mostly.

Cor

Nov 20 '05 #8
Cor
Hi Mike,

Why a listview, a listview is for me a control that the user uses to click
on.

I would think on a datagrid with an underlaying table that I could search.

Cor
Nov 20 '05 #9
Does the datagrid have a search feature that returns the row number itself ?

"Cor" <no*@non.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Hi Mike,

Why a listview, a listview is for me a control that the user uses to click
on.

I would think on a datagrid with an underlaying table that I could search.

Cor

Nov 20 '05 #10
Mike Smith wrote:
yeah resorted to looping it but not the real best solution rite ?

used to be a sendmessage() API call that would work.. i tried using it in
.net but it didnt seem to work ....

the ListView.Items.IndexOf(listitem) does manage to find the listitem object
but thats if we are using a unique listitem each time we add an item into
the list. Who would do such a thing? Of course we're gonna reuse the same
variable in a loop to add items into the listview. There should be some
faster way to find an item/subitem in a listview more efficiently ?


Mike...

Here is an example of using the LVM_FINDITEM and SendMessage...

Option Strict On
Option Explicit Off

Imports System.Diagnostics
Imports System.Runtime.InteropServices

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents ListView1 As System.Windows.Forms.ListView
Friend WithEvents ColumnHeader1 As System.Windows.Forms.ColumnHeader
Friend WithEvents ColumnHeader2 As System.Windows.Forms.ColumnHeader
Friend WithEvents ColumnHeader3 As System.Windows.Forms.ColumnHeader
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Dim ListViewItem1 As System.Windows.Forms.ListViewItem = New
System.Windows.Forms.ListViewItem(New String() {"First", "Sub1",
"Sub2"}, -1)
Dim ListViewItem2 As System.Windows.Forms.ListViewItem = New
System.Windows.Forms.ListViewItem(New String() {"Fourth", "Sub1",
"Sub2"}, -1)
Dim ListViewItem3 As System.Windows.Forms.ListViewItem = New
System.Windows.Forms.ListViewItem(New String() {"Second", "Sub1",
"Sub2"}, -1)
Dim ListViewItem4 As System.Windows.Forms.ListViewItem = New
System.Windows.Forms.ListViewItem(New String() {"Thrid", "Sub1",
"Sub2"}, -1)
Me.ListView1 = New System.Windows.Forms.ListView
Me.ColumnHeader1 = New System.Windows.Forms.ColumnHeader
Me.ColumnHeader2 = New System.Windows.Forms.ColumnHeader
Me.ColumnHeader3 = New System.Windows.Forms.ColumnHeader
Me.SuspendLayout()
'
'ListView1
'
Me.ListView1.Columns.AddRange(New
System.Windows.Forms.ColumnHeader() {Me.ColumnHeader1, Me.ColumnHeader2,
Me.ColumnHeader3})
Me.ListView1.Dock = System.Windows.Forms.DockStyle.Fill
Me.ListView1.Items.AddRange(New
System.Windows.Forms.ListViewItem() {ListViewItem1, ListViewItem2,
ListViewItem3, ListViewItem4})
Me.ListView1.Location = New System.Drawing.Point(0, 0)
Me.ListView1.Name = "ListView1"
Me.ListView1.Size = New System.Drawing.Size(292, 273)
Me.ListView1.TabIndex = 0
Me.ListView1.View = System.Windows.Forms.View.Details
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.ListView1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Private Const LVM_FIRST As Integer = &H1000
Private Const LVM_FINDITEMA As Integer = LVM_FIRST + 13
Private Const LVM_FINDITEMW As Integer = LVM_FIRST + 83

<Flags()> _
Private Enum LVFI_FLAGS
Param = &H1
Partial = &H8
[String] = &H2
Wrap = &H20
NearestXY = &H40
End Enum

Private Enum VK
Left = &H25
Right = &H27
Up = &H26
Down = &H28
Home = &H24
[End] = &H23
Prior = &H21
[Next] = &H22
End Enum

<StructLayout(LayoutKind.Sequential)> _
Private Structure POINT
Public x As Integer
Public y As Integer
End Structure

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Private Structure LVFINDINFO
Public flags As LVFI_FLAGS
Public psz As String
Public lParam As IntPtr
Public pt As POINT
Public vkDirection As VK
End Structure

Private Declare Auto Function SendMessage Lib "user32" _
(ByVal hWnd As IntPtr, _
ByVal Msg As Integer, _
ByVal wParam As Integer, _
ByRef lParam As LVFINDINFO) As Integer

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim msg As Integer = CInt(IIf(Marshal.SystemDefaultCharSize >
1, LVM_FINDITEMW, LVM_FINDITEMA))
Dim lvfi As LVFINDINFO

With lvfi
.flags = LVFI_FLAGS.String
.psz = "Second"
End With

MessageBox.Show(SendMessage(ListView1.Handle, msg, -1,
lvfi).ToString())
End Sub
End Class

HTH

--
Tom Shelton

Nov 20 '05 #11
Cor
Hi Mike,

Not that I directy know, but with the datagrid you are always using the
datasource.

Mostly a datatable. And to search in that underlaying table has very much
posibilities to search in and searching in that way is also a faster.

Cor
Does the datagrid have a search feature that returns the row number itself

?
Nov 20 '05 #12
hey man ... thanx .. this is what i wanted... just a quick question on
this..
it only works on items and not subitems ?

"Tom Shelton" <to*@mtogden.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Mike Smith wrote:
yeah resorted to looping it but not the real best solution rite ?

used to be a sendmessage() API call that would work.. i tried using it in .net but it didnt seem to work ....

the ListView.Items.IndexOf(listitem) does manage to find the listitem object but thats if we are using a unique listitem each time we add an item into the list. Who would do such a thing? Of course we're gonna reuse the same variable in a loop to add items into the listview. There should be some
faster way to find an item/subitem in a listview more efficiently ?

Mike...

Here is an example of using the LVM_FINDITEM and SendMessage...

Option Strict On
Option Explicit Off

Imports System.Diagnostics
Imports System.Runtime.InteropServices

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form

Designer 'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents ListView1 As System.Windows.Forms.ListView
Friend WithEvents ColumnHeader1 As System.Windows.Forms.ColumnHeader
Friend WithEvents ColumnHeader2 As System.Windows.Forms.ColumnHeader
Friend WithEvents ColumnHeader3 As System.Windows.Forms.ColumnHeader
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Dim ListViewItem1 As System.Windows.Forms.ListViewItem = New
System.Windows.Forms.ListViewItem(New String() {"First", "Sub1",
"Sub2"}, -1)
Dim ListViewItem2 As System.Windows.Forms.ListViewItem = New
System.Windows.Forms.ListViewItem(New String() {"Fourth", "Sub1",
"Sub2"}, -1)
Dim ListViewItem3 As System.Windows.Forms.ListViewItem = New
System.Windows.Forms.ListViewItem(New String() {"Second", "Sub1",
"Sub2"}, -1)
Dim ListViewItem4 As System.Windows.Forms.ListViewItem = New
System.Windows.Forms.ListViewItem(New String() {"Thrid", "Sub1",
"Sub2"}, -1)
Me.ListView1 = New System.Windows.Forms.ListView
Me.ColumnHeader1 = New System.Windows.Forms.ColumnHeader
Me.ColumnHeader2 = New System.Windows.Forms.ColumnHeader
Me.ColumnHeader3 = New System.Windows.Forms.ColumnHeader
Me.SuspendLayout()
'
'ListView1
'
Me.ListView1.Columns.AddRange(New
System.Windows.Forms.ColumnHeader() {Me.ColumnHeader1, Me.ColumnHeader2,
Me.ColumnHeader3})
Me.ListView1.Dock = System.Windows.Forms.DockStyle.Fill
Me.ListView1.Items.AddRange(New
System.Windows.Forms.ListViewItem() {ListViewItem1, ListViewItem2,
ListViewItem3, ListViewItem4})
Me.ListView1.Location = New System.Drawing.Point(0, 0)
Me.ListView1.Name = "ListView1"
Me.ListView1.Size = New System.Drawing.Size(292, 273)
Me.ListView1.TabIndex = 0
Me.ListView1.View = System.Windows.Forms.View.Details
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.ListView1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Private Const LVM_FIRST As Integer = &H1000
Private Const LVM_FINDITEMA As Integer = LVM_FIRST + 13
Private Const LVM_FINDITEMW As Integer = LVM_FIRST + 83

<Flags()> _
Private Enum LVFI_FLAGS
Param = &H1
Partial = &H8
[String] = &H2
Wrap = &H20
NearestXY = &H40
End Enum

Private Enum VK
Left = &H25
Right = &H27
Up = &H26
Down = &H28
Home = &H24
[End] = &H23
Prior = &H21
[Next] = &H22
End Enum

<StructLayout(LayoutKind.Sequential)> _
Private Structure POINT
Public x As Integer
Public y As Integer
End Structure

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Private Structure LVFINDINFO
Public flags As LVFI_FLAGS
Public psz As String
Public lParam As IntPtr
Public pt As POINT
Public vkDirection As VK
End Structure

Private Declare Auto Function SendMessage Lib "user32" _
(ByVal hWnd As IntPtr, _
ByVal Msg As Integer, _
ByVal wParam As Integer, _
ByRef lParam As LVFINDINFO) As Integer

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim msg As Integer = CInt(IIf(Marshal.SystemDefaultCharSize >
1, LVM_FINDITEMW, LVM_FINDITEMA))
Dim lvfi As LVFINDINFO

With lvfi
.flags = LVFI_FLAGS.String
.psz = "Second"
End With

MessageBox.Show(SendMessage(ListView1.Handle, msg, -1,
lvfi).ToString())
End Sub
End Class

HTH

--
Tom Shelton

Nov 20 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Coffee guy | last post by:
I need to locate an item in ListView, quickly. I have a listview with items, for each I added a unique "Tag" property. Is there a simple way to rapidly search through these items to find based on...
95
by: Jim Heavey | last post by:
Hello. When you use the ListView1.Item(0).Selected = true, this addis the item to items which show up int the ListView1.SelectedItems collection, but the item is not highlighted in the control. ...
6
by: Nick | last post by:
Hi there, I'm trying to implement drag-drop for my listview control in large icon view mode. Unfortunately the order of the items gets completely messed up upon inserting the item back into the...
5
by: Phill W. | last post by:
(VB'2003) What's the correct way to remove multiple, selected items from a ListView control (say, from a ContextMenu)? I ask because I'm getting a very annoying ArgumentOutOfRangeException...
1
by: plmanikandan | last post by:
Hi, I have a listview with items,subitems.I tems and subitems are added as below listView1.View = View.Details; listView1.Columns.Add("Column 1", 100,HorizontalAlignment.Center);...
3
by: RT | last post by:
Is there any way to make Listview items invisible or otherwise keep them from displaying? Seems like temporarily removing, then restoring later would be a severe runtime hit.
0
by: Peter | last post by:
Hi, I have a problem with Listview using checkboxes. If i check items by code BEFORE the form is shown the Listview.Items are confused during the ItemChecked Event !!! After showing the...
2
by: Peter | last post by:
Hi, I have a problem with Listview using checkboxes. If i check items by code BEFORE the form is shown the Listview.Items are confused during the ItemChecked Event !!! After showing the...
1
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hello, In my application I need to periodically remove all current items from a ListView and add a new set into it. The following abbreviated code contains the basic idea: private void...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...

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.