Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a form containing a txtSearch TextBox and a ListView Control.
I have focus on text box.
I have set a textbox_changed event of text box to search items in listview control.
In which i use FindItemWithText method to find the textbox text as following

C#
private void txtSearch_TextChanged(object sender, EventArgs e)
{
    try
    {
        ListViewItem foundItem = lstHelp.FindItemWithText(txtSearch.Text,           false,0,true);
        if (foundItem != null)
        {
            lstHelp.TopItem = foundItem;
        }
    }
    catch { }
}



But with textbox changed event listbox selection is not changing.
From which property/method I select/clear selection in listview control.

Please help me.
Posted
Updated 11-Nov-11 2:33am
v2
Comments
Smithers-Jones 11-Nov-11 8:33am    
Added code-block, removed all-caps from subject line.

Your question is a bit puzzling: if you want to delete an item from a ListView, why do you want to move it to the top of the ListView first ?

'FindItemWithText' works fine, and you can just call foundItem.Remove(); to delete the item, after you've checked you have a non-null result of your search.

And, I do wonder here if your goal is to actually delete the item, or simply to remove it from being displayed ... possibly so you can add it back to the ListViewItems later ?

Assuming there's a reason you want to move the ListViewItem to the top of the ListView: I'm going to talk about the much more interesting issue of how to do that:

You have located what, in my humble opinion, is a flaw in the old ListView control: setting TopItem will only have an effect if the item you set to TopItem is scrolled out of view in the current ListView. This seems very stupid because ListView also supports the 'EnsureVisible' method on a ListViewItem which will make sure the ListViewItem is made visible, if scrolled out of view.

You can use this instead:
C#
private ListViewItem foundItem;
// here we are triggering the search by a button click
private void button1_Click(object sender, EventArgs e)
{
   foundItem = listView1.FindItemWithText(textBox1.Text);

   if (foundItem != null)
   {
     listView1.Items.Remove(foundItem);
     listView1.Items.Insert(0, foundItem);
   }
   else
   {
     // do something here if no match ?
   }
}
May I suggest you consider not using the TextChanged event of the TextBox to trigger your search: because it's going to perform a new search each time any key on the keyboard is pressed.

If you don't want to trigger the search by a button press (as in the example code above), you might consider looking at TextBox properties like 'AcceptsReturn' and thinking about a strategy based on defining some key-down Event that triggers the search when it recognizes Return/Enter has been pressed (which is kind of a convention).
 
Share this answer
 
v5
Try the following.
C#
// Set HideSelection property to false.
// This is to retain selection when the control is out of focus

lstHelp.HideSelection = false;

// set selected index when found a match

ListViewItem foundItem = lstHelp.FindItemWithText(txtSearch.Text,       false,0,true);
if (foundItem != null)
{
  lstHelp.Items[foundItem.Index].Selected = true;        
}
else
{
  lstHelp.SelectedItems.Clear(); // clear existing selections if any
}
Note:- By default, when the list view is not focused, selected item is grayed out. Change the selected item style as required.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900