contran
asked on
Find item in list view
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.
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.
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.
Try this.
ListView listView2 = new ListView();
ListViewItem ite = new ListViewItem("Search text");
int foundListItem = listView2.Items.IndexOf(it e);
ListView listView2 = new ListView();
ListViewItem ite = new ListViewItem("Search text");
int foundListItem = listView2.Items.IndexOf(it
@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
"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
ASKER
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.For ms.ListVie wItem item in this.listView1.Items)
{
foreach(ListViewItem.ListV iewSubItem 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,tex t.ToString (),"");
index = item.Index;
}
}
}
}
return index;
}
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.For
{
foreach(ListViewItem.ListV
{
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,tex
index = item.Index;
}
}
}
}
return index;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.