Solved

Find item in list view

Posted on 2004-09-29
5
356 Views
Last Modified: 2008-01-09
Dear experts,
I just wondering that is there any control available to search a string in the list view or not. If not could someone show me how to implement that control please.
There function of the control is similar to the Find function (F5) in Excel or Word, when you want to look up the string or piece of information, the popup dialog prompt to enter the string and a list of options such as Match case, Match the whole word, find next button, etc...
The function should be return the index of the list item in the list view.
I am thinking of using foreach to do this job but is there any better way to do so.

Thank you so much.

0
Comment
Question by:contran
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 20

Expert Comment

by:TheAvenger
ID: 12187527
No, there isn't. You have to go over all elements in the list view and check using String.IndexOf() if the element contains the searched string.
0
 
LVL 10

Expert Comment

by:123654789987
ID: 12188306
Try this.

                                                ListView  listView2 = new ListView();

                  ListViewItem ite = new ListViewItem("Search text");
                   int foundListItem = listView2.Items.IndexOf(ite);
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 12188317
@123654789987: this solution could find only items which have exactly this text, it would not find an item with the text:

"Hello Search text Hello"

Also as you are creating a new listviewitem, I suppose it will not be found at all in the Items collection - it is just not there
0
 

Author Comment

by:contran
ID: 12196719
Hi TheAvenger,

Thank you for your suggestion , i can found the searching key using IndexOf() however how can i do the find next button i.e. if more than 1 results in the list view the findnext button should be able to capture all results.
Here is the code that i've used
//The str - the search key word,
//flag - indicate the optional search i.e. 0 for match exact word, 1 for match case
private int SearchItem(string str,int flag)
{
  int index = -1;
  foreach(System.Windows.Forms.ListViewItem item in this.listView1.Items)
  {
     foreach(ListViewItem.ListViewSubItem subItem in item.SubItems)
     {
       if(flag == 0)
       {
          if(subItem.Text.Trim() == str.Trim())
      index =  item.Index;
       }
     
      if(flag == 1)
      {
        int text = subItem.Text.IndexOf(str);
        if(text!=0)
        {
           //MessageBox.Show(this,text.ToString(),"");
           index =  item.Index;
        }
       }
     }
   }
       return index;      
}




0
 
LVL 20

Accepted Solution

by:
TheAvenger earned 125 total points
ID: 12198453
Well, you can keep a collection of list items that contain the required text. For example use an ArrayList to put the list items in.

And one remark: the comparison if (text != 0) is wrong. It should be if (text >= 0) because 0 is a valid index, so IndexOf will return 0 if the text starts with what you search for.
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question