473,387 Members | 1,486 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,387 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 5376
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.