search item in listview

  • Thread starter Thread starter Ching-Lung
  • Start date Start date
C

Ching-Lung

How do I search for an item in a list view?

// in the constructor
ListView lv = new ListView();
lv.Items.Add("Apple");
lv.Items.Add("Banana");
lv.Items.Add("Cherry");

// in a button
if(lv.Items.Contain(new ListViewItem("Apple")))
MessageBox.Show("Found");
else
MessageBox.Show("Didn't found");

If condition fails all the time. Any idea?

Thanks,
-CL
 
Ching-Lung said:
if(lv.Items.Contain(new ListViewItem("Apple")))
MessageBox.Show("Found");
else
MessageBox.Show("Didn't found");

If condition fails all the time. Any idea?

You are testing two different ListViewItems, which
just happen to have the same text. You can either
keep the reference to the original item, or iterate
through all items in the Items collection and check
the Text property for each.
 
Hi Ching-Lung,
I agree with froeschlin, and I'd like to provide more information. the
Contains method would return if an reference of item is stored in this
collection. To do a search based on content, you should iterate the
Collection just like:

bool FindInCollection(ListViewItemCollection collection,string text)
{
foreach(ListViewItem item in collection)
if (item.Equals(text))
return true;
return false;
}
If you have any further questions, be free to let me know, thanks!

Kind regards,

Ying-Shen Yu [MS]
Microsoft Support Engineer

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. 2001 Microsoft Corporation. All rights
reserved.
--------------------
| From: =?ISO-8859-1?Q?Christian_Fr=F6schlin?= <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.windowsforms
| Subject: Re: search item in listview
| Date: Tue, 26 Aug 2003 18:52:45 +0200
| Organization: (posted via) Mnet Telekommunikations GmbH
| Lines: 15
| Message-ID: <[email protected]>
| References: <[email protected]>
| NNTP-Posting-Host: host-62-245-183-130.customer.m-online.net
| Mime-Version: 1.0
| Content-Type: text/plain; charset=us-ascii; format=flowed
| Content-Transfer-Encoding: 7bit
| X-Trace: svr8.m-online.net 1061916768 3030 62.245.183.130 (26 Aug 2003
16:52:48 GMT)
| X-Complaints-To: (e-mail address removed)
| NNTP-Posting-Date: Tue, 26 Aug 2003 16:52:48 +0000 (UTC)
| User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.0.1)
Gecko/20020823 Netscape/7.0
| X-Accept-Language: en-us, en, ja
| Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin
e.de!newsfeed.freenet.de!news.space.net!news.m-online.net!not-for-mail
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.windowsforms:50943
| X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
|
| Ching-Lung wrote:
|
| > if(lv.Items.Contain(new ListViewItem("Apple")))
| > MessageBox.Show("Found");
| > else
| > MessageBox.Show("Didn't found");
| >
| > If condition fails all the time. Any idea?
|
| You are testing two different ListViewItems, which
| just happen to have the same text. You can either
| keep the reference to the original item, or iterate
| through all items in the Items collection and check
| the Text property for each.
|
|
 
Hi Ching-Lung,
Contains method do comparation by the Equal method, However the
ListViewItem as well as ListviewSubItem didn't override the Equal method,
so they are using the default implementation in Object, which only compares
their reference.
To find an Item by value, I think do an iteration is a much simpler and
direct, although you may create your own class by deriving the ListViewItem
and ListViewSubItem, thanks!

Kind regards,

Ying-Shen Yu [MS]
Microsoft Support Engineer

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. 2001 Microsoft Corporation. All rights
reserved.
--------------------
| X-Tomcat-ID: 37540730
| References: <[email protected]>
<[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain
| Content-Transfer-Encoding: 7bit
| From: (e-mail address removed) (Ying-Shen Yu[MSFT])
| Organization: Microsoft
| Date: Wed, 27 Aug 2003 01:32:37 GMT
| Subject: Re: search item in listview
| X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.windowsforms
| Lines: 63
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.windowsforms:50972
| NNTP-Posting-Host: TOMCATIMPORT1 10.201.218.122
|
| Hi Ching-Lung,
| I agree with froeschlin, and I'd like to provide more information.
the
| Contains method would return if an reference of item is stored in this
| collection. To do a search based on content, you should iterate the
| Collection just like:
|
| bool FindInCollection(ListViewItemCollection collection,string text)
| {
| foreach(ListViewItem item in collection)
| if (item.Equals(text))
| return true;
| return false;
| }
| If you have any further questions, be free to let me know, thanks!
|
| Kind regards,
|
| Ying-Shen Yu [MS]
| Microsoft Support Engineer
|
| This posting is provided "AS IS" with no warranties, and confers no
rights.
| You assume all risk for your use. 2001 Microsoft Corporation. All rights
| reserved.
| --------------------
| | From: =?ISO-8859-1?Q?Christian_Fr=F6schlin?= <[email protected]>
| | Newsgroups: microsoft.public.dotnet.framework.windowsforms
| | Subject: Re: search item in listview
| | Date: Tue, 26 Aug 2003 18:52:45 +0200
| | Organization: (posted via) Mnet Telekommunikations GmbH
| | Lines: 15
| | Message-ID: <[email protected]>
| | References: <[email protected]>
| | NNTP-Posting-Host: host-62-245-183-130.customer.m-online.net
| | Mime-Version: 1.0
| | Content-Type: text/plain; charset=us-ascii; format=flowed
| | Content-Transfer-Encoding: 7bit
| | X-Trace: svr8.m-online.net 1061916768 3030 62.245.183.130 (26 Aug 2003
| 16:52:48 GMT)
| | X-Complaints-To: (e-mail address removed)
| | NNTP-Posting-Date: Tue, 26 Aug 2003 16:52:48 +0000 (UTC)
| | User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.0.1)
| Gecko/20020823 Netscape/7.0
| | X-Accept-Language: en-us, en, ja
| | Path:
|
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin
| e.de!newsfeed.freenet.de!news.space.net!news.m-online.net!not-for-mail
| | Xref: cpmsftngxa06.phx.gbl
| microsoft.public.dotnet.framework.windowsforms:50943
| | X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
| |
| | Ching-Lung wrote:
| |
| | > if(lv.Items.Contain(new ListViewItem("Apple")))
| | > MessageBox.Show("Found");
| | > else
| | > MessageBox.Show("Didn't found");
| | >
| | > If condition fails all the time. Any idea?
| |
| | You are testing two different ListViewItems, which
| | just happen to have the same text. You can either
| | keep the reference to the original item, or iterate
| | through all items in the Items collection and check
| | the Text property for each.
| |
| |
|
|
 
Back
Top