Click here to Skip to main content
15,911,711 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to program search and highlight functionality in listviewitem windows application using forms.
Posted
Updated 29-Aug-11 20:20pm
v2
Comments
BillWoodruff 30-Aug-11 3:06am    
What have you tried so far ?
Member 8198840 30-Aug-11 5:28am    
i hve displayed ListView in form of list of strings.now a separate form is there to give string name to be searched and highlighted.some code is:
private void btn3_Click(object sender, EventArgs e)
{

s MainForm = new s();

//logic to serach and highlight
ListViewItem foundItem =

MainForm.listView1.FindItemWithText(txtbx1.Text);
if (foundItem != null)
{
MainForm.listView1.TopItem = foundItem;
MessageBox.Show("Calling FindItemWithText found"
+ foundItem.ToString());


}
} but its not working.
Member 8198840 30-Aug-11 23:24pm    
how can i perform search operation on listview having list of objects(each object has 4 strings as data members).

1 solution

Let me disclaimer this attempt at an answer by saying that I certainly do not regard myself as an expert on the WinForms native ListView, and I use a 3rd. Party ListView in my own code.

From my quick study, it appears that your code is not causing an error: however, the 'TopItem property does not, as you may be expecting, move the position of a ListViewItem within the ListView to the top of the ListView client display rectangle.

'TopItem' appears to me to have much the same effect as 'EnsureVisible' : it just makes it show up in the visible area of the ListView. Unfortunately, it appears to me that the WinForms ListView Control has no 'ScrollIntoView method, or other useful way to position a given ListViewItem exactly where you want it in the visible area of the ListView.

'Highlighting' is something you can easily simulate by just setting the BackColor, and/or selecting the matching ListViewItem found by searching with 'FindItemWithText.'

Now if your intent is to actually MOVE the found ListViewItem to the top of the ListView: that can be done like this:
private void button1_Click(object sender, EventArgs e)
{
  ListViewItem lv = listView1.FindItemWithText("AnyOldItem");

  if(lv == null) return; // or throw some error ....

  // move the found Item to the top of the ListView
  listView1.Items.Remove(lv);
  listView1.Items.Insert(0, lv);

  // simulate some highlighting ...
  lv.BackColor = Color.Yellow;
  lv.Selected = true;
}
Which I think is ... uhhh ... not very beautiful code :).

I hope someone else gives you a better solution !
 
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