Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a listview that is populated with aprox 500 lines of text. Then I use the following code below to search for text (string) in a listView. Code works great. The only thing is that only highlights string that are currently displayed in the listView so if I scroll down, nothing is highlighted anymore so I need to repeat the search (page by page). Can I make any changes to this code in order to highlight the entire contents of the listView?


C#
public void FindListViewItem(DependencyObject obj)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                ListViewItem lv = obj as ListViewItem;
                if (lv != null)
                {
                    HighlightText(lv);
                }
                FindListViewItem(VisualTreeHelper.GetChild(obj as DependencyObject, i));
            }
        }
        
        private void HighlightText(Object itx)
        {
            Regex regex;
            if (itx != null)
            {
                if (itx is TextBlock)
                {
                    regex = new Regex("(" + textboxsearch.Text + ")", RegexOptions.IgnoreCase);
                    TextBlock tb = itx as TextBlock;
                    if (textboxsearch.Text.Length == 0)
                    {
                        string str = tb.Text;
                        tb.Inlines.Clear();
                        tb.Inlines.Add(str);
                        return;
                    }
                    string[] substrings = regex.Split(tb.Text);
                    tb.Inlines.Clear();
                    foreach (var item in substrings)
                    {
                        if (regex.Match(item).Success)
                        {
                            Run runx = new Run(item);
                            runx.Background = Brushes.YellowGreen;
                            tb.Inlines.Add(runx);
                        }
                        else
                        {
                            tb.Inlines.Add(item);
                        }
                    }
                    return;
                }
                else
                {
                    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(itx as DependencyObject); i++)
                    {
                        HighlightText(VisualTreeHelper.GetChild(itx as DependencyObject, i));
                    }
                }
            }
        }
Posted
Updated 9-Mar-15 13:03pm
v2
Comments
Sergey Alexandrovich Kryukov 9-Mar-15 19:14pm    
The biggest problem of your question is that you did not indicate what do you mean by "ListView"? Which one?
Almost certainly, this is WPF, System.Windows.Controls.ListView, but you should not give us riddles.
—SA
PIEBALDconsult 9-Mar-15 19:15pm    
"I have a listview that is populated with aprox 500 lines of text"

You need a better design first.

The question is not quite clear, but it's possible that what you need is the ability to select a set of rows, not one. Of course, this is possible:
https://msdn.microsoft.com/en-us/library/system.windows.controls.listbox.selectionmode%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.controls.selectionmode%28v=vs.110%29.aspx[^].

It's also very likely that this is just a matter of design. It all depends on what you really need to do with the found items. In many cases, you would need to think about alternative methods of marking search results in a list view. What's the problem with selection? One accidental click, and search results are lost. One simple alternative would be check box column. A search could set selection state in this columns, and the use can even remove some of the items from the set, or add some, before performing further operations to the set of found items. If you go this way (which I would rather recommend), you need also some more operations, such as "check all" and "clear all" on checked states.

—SA
 
Share this answer
 
XML
Many thanks for taking the time to reply.
Yes, the question sadly was incomplete.   My application runs an algorithm that outputs a log which is several lines of text.  I can easily pipe it to a text file and use the word editor search functionality; however, I have re-directed the log to a WPF listview to search few important strings.
Both <ListBox SelectionMode="Extended">  and  <ListBox SelectionMode="Multiple">  allows me to select multiple or all the lines; however, when I search , the string is found only on the lines that are displayed.  One click (scroll down) and search result are lost.
Many thanks again
 
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