搜索字符串C# [英] Searching a string C#

查看:78
本文介绍了搜索字符串C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表视图,其中填充了aprox 500行文本。然后我使用下面的代码在listView中搜索文本(字符串)。代码效果很好。唯一的事情是只突出显示当前在listView中显示的字符串,所以如果我向下滚动,则不再突出显示任何内容,因此我需要重复搜索(逐页)。我是否可以对此代码进行任何更改以突出显示listView的全部内容?





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?


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));
                    }
                }
            }
        }

推荐答案

问题不太明确,但您可能需要的是能够选择一组行而不是一行。当然,这是可能的:

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 [ ^ ] 。



这也很可能只是设计问题。这一切都取决于你真正需要做的事情。在许多情况下,您需要考虑在列表视图中标记搜索结果的替代方法。选择有什么问题?一次意外点击,搜索结果丢失。一个简单的替代方案是复选框列。搜索可以在此列中设置选择状态,并且在对找到的项集执行进一步操作之前,使用甚至可以从集中删除一些项,或者添加一些项。如果你走这条路(我宁愿推荐),你还需要更多的操作,比如在检查状态下全部检查和全部清除。



-SA
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


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


这篇关于搜索字符串C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆