I want that search in the listView by enter a text in the TextBox and use the textBox_TextCha nge event then refresh listView items.....
please F1.....
please F1.....
// This should hold all of our items. Whenever you call
// listView1.Items.Add() you should also add the item to this List
private readonly List<ListViewItem> ListItems = new List<ListViewItem>();
private void textBox1_TextChanged(object sender, EventArgs e)
{
List<ListViewItem> tmp = new List<ListViewItem>();
if (string.IsNullOrEmpty(textBox1.Text))
{
// No text, so we just fill the list with our items!
tmp = ListItems;
goto INSERT_ITEMS;
}
// Store the text so we're not making pointless calls
string txtLower = textBox1.Text.ToLower();
// Iterate over all the items in our list
// so we make sure we're searching *everything*
foreach (ListViewItem item in ListItems)
{
// Simple flag to check if a subitem
// has the search term.
bool addItem = false;
// This can either be StartsWith or Contains
// Personally, StartsWith is more intuitive
// unless you decide to add more 'advanced'
// searching. In that case, you'll want to
// create a new searching method. (Or class)
if (item.Text.ToLower().StartsWith(txtLower))
{
// The first indexed item has our text.
// Don't bother checking subitems!!
tmp.Add(item);
continue;
}
foreach (ListViewItem subItem in item.SubItems)
{
if (subItem.Text.ToLower().StartsWith(txtLower))
{
addItem = true;
}
}
// If a subitem of this item has the text
// we need to add it.
if(addItem)
{
tmp.Add(item);
}
}
// No real way around this unless you extract it
// to another method. Which may be a good idea.
INSERT_ITEMS:
// Clear the current items.
listView1.Items.Clear();
// Add the correct items. :)
foreach (ListViewItem item in tmp)
{
listView1.Items.Add(item);
}
}
Comment