473,385 Members | 1,356 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,385 software developers and data experts.

search in listview

I want that search in the listView by enter a text in the TextBox and use the textBox_TextChange event then refresh listView items.....

please F1.....
Apr 15 '09 #1
2 8137
Expand|Select|Wrap|Line Numbers
  1.         // This should hold all of our items. Whenever you call
  2.         // listView1.Items.Add() you should also add the item to this List
  3.         private readonly List<ListViewItem> ListItems = new List<ListViewItem>();
  4.  
  5.         private void textBox1_TextChanged(object sender, EventArgs e)
  6.         {
  7.             List<ListViewItem> tmp = new List<ListViewItem>();
  8.  
  9.             if (string.IsNullOrEmpty(textBox1.Text))
  10.             {
  11.                 // No text, so we just fill the list with our items!
  12.                 tmp = ListItems;
  13.                 goto INSERT_ITEMS;
  14.             }
  15.  
  16.             // Store the text so we're not making pointless calls
  17.             string txtLower = textBox1.Text.ToLower();
  18.  
  19.             // Iterate over all the items in our list
  20.             // so we make sure we're searching *everything*
  21.             foreach (ListViewItem item in ListItems)
  22.             {
  23.                 // Simple flag to check if a subitem
  24.                 // has the search term.
  25.                 bool addItem = false;
  26.  
  27.                 // This can either be StartsWith or Contains
  28.                 // Personally, StartsWith is more intuitive 
  29.                 // unless you decide to add more 'advanced'
  30.                 // searching. In that case, you'll want to
  31.                 // create a new searching method. (Or class)
  32.                 if (item.Text.ToLower().StartsWith(txtLower))
  33.                 {
  34.                     // The first indexed item has our text.
  35.                     // Don't bother checking subitems!!
  36.                     tmp.Add(item);
  37.                     continue;
  38.                 }
  39.  
  40.                 foreach (ListViewItem subItem in item.SubItems)
  41.                 {
  42.                     if (subItem.Text.ToLower().StartsWith(txtLower))
  43.                     {
  44.                         addItem = true;
  45.                     }
  46.                 }
  47.  
  48.                 // If a subitem of this item has the text
  49.                 // we need to add it.
  50.                 if(addItem)
  51.                 {
  52.                     tmp.Add(item);
  53.                 }
  54.             }
  55.  
  56.             // No real way around this unless you extract it
  57.             // to another method. Which may be a good idea.
  58.             INSERT_ITEMS:
  59.  
  60.             // Clear the current items.
  61.             listView1.Items.Clear();
  62.  
  63.             // Add the correct items. :)
  64.             foreach (ListViewItem item in tmp)
  65.             {
  66.                 listView1.Items.Add(item);
  67.             }
  68.         }
It's not very efficient, but it should give you an idea.
Apr 16 '09 #2
thanks.i already tested this way.
but is it best? in my project performance is very important and must be very high......
Apr 16 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Jon Ripley | last post by:
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...
1
by: decrypted | last post by:
I am having trouble with creating / finding effective searching algorithms. I constantly run into a situation where I have a list of objects and need to find out if a property of one of these...
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
6
by: Marc Robitaille | last post by:
Hello, I have created a new ListView control that inherits from ListView control. In the WndProc override subroutine, I have been able to find and use those constants : Private Const...
1
by: shivram | last post by:
hey!! am currently building a search application for documents. wherein am using the listview control in VJ# . So its like i display the document name, path and some relevance value using the...
4
by: Brian Gaze | last post by:
I have created a ListView control and have bound this to a datasource. Within the ItemTemplate of the ListView I have added another ListViewControl which is databound in the code behind. The idea...
1
by: Luqman | last post by:
I am using VS 2008, and I am looking for some code which puts live search capability to GridView or ListView 3.5. Just like Yahoo search engine text box, you start typing anything in textbox and it...
3
by: yogarajan | last post by:
Dear Friends i am devloping for search box it is woking good i want highlight my search text. My original text is Mixed case (Lower case and upper case) my search text is lower case means how can...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?

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.