2012-01-07 11 views
1

私はこれで始めるべきところでは本当にうっかりしません。RichTextBoxで検索し、その特定の単語のすべてのインスタンスをハイライト表示する方法

RichTextBoxのWPFアプリケーションがあります。この中には、ユーザーの選択に応じて変更されるFlowDocumentというテキストがあります。

ユーザーはTextBoxに単語を入力し、見つかった場合はこの単語のすべてのインスタンスを隣のRichTextBoxでハイライト表示する方法が必要です。 http://kentb.blogspot.com/2009/06/search-and-highlight-text-in-arbitrary.htmlこのアイデアは完璧ですが、RichTextBoxを使ってアプリケーションにどのように適用するかはわかりません。

ありがとうございます!

答えて

0

FlowDocumentで行います。このサンプルでは、​​その色の背景色を示しています。私はFlowDocumentReaderを使用してFlowDocumentを表示しますが、RichTextBoxにはFlowDocumentと表示されると思います。少し複雑に思えるかもしれませんが、実際のテキストをマークアップするのは、私がWindows.Form RichTextBoxとバックアップしなければならなかったように、ポジションを強調しなければならないという点で問題はありません。これは、どの色のハイライトが最もよく見えるかを決めるために使用したコードです。

docFlowDocument = new FlowDocument();   
System.Windows.Media.Brush defaultBrush = System.Windows.Media.Brushes.White; 
docFlowDocument.Background = defaultBrush; 
System.Windows.Media.Brush curBrush = defaultBrush; 
Paragraph p = new Paragraph(); 
Run r = new Run(); 
r.Background = curBrush; 
#region nullDocument 
if (String.IsNullOrEmpty(DocText)) 
{ 
    r.Foreground = System.Windows.Media.Brushes.Red; 
    r.Text = "No Text"; 
    p.Inlines.Add(r); 
    docFlowDocument.Blocks.Add(p); 


    List<string> colorNames = (from pc in typeof(Brushes).GetProperties() 
            select pc.Name).ToList(); 
    //Debug.WriteLine(colorNames.Count.ToString()); 
    //Debug.WriteLine(colorNames[0]); 

    Type brushesType = typeof(Brushes); 
    System.Reflection.MemberInfo[] membersinfo = brushesType.GetMembers(); 
    System.Reflection.PropertyInfo[] properties = brushesType.GetProperties(); 

    for (int i = 0; i < properties.Length; i++) 
    { 
     r = new Run(); 
     r.Background = (Brush)properties[i].GetValue(null, null); 
     r.Text = colorNames[i]; 
     p.Inlines.Add(r); 
     p.Inlines.Add(new LineBreak()); 
    } 
    docFlowDocument.Blocks.Add(p); 
    docFlowDocumentFinishedLastRun = true; 
    return docFlowDocument; 
} 
#endregion // nullDocument 
3

正規表現を使用してみましたか?

ような何か:限り、あなたは、このメソッドは動作しますが、同時にあなたのボックスで複数の色を必要としないよう

private void searchButton_Click(object sender, EventArgs e) 
{ 
    //Select all text and bring it back to default color values so you 
    //can make a new search selection 

    richTextBox1.SelectAll(); 
    richTextBox1.SelectionColor = System.Drawing.Colors.Black; 

    //Deselect all text to ready selections 

    richTextBox1.DeselectAll(); 

    //Create a MatchList variable and initialize it to all matches 
    //within the RichTextBox. Add a using statement of 
    //System.Text.RegularExpressions 

    Color evenColor = Color.Red; 
    Color oddColor = Color.Blue; 

    MatchCollection matches = Regex.Matches(richTextBox1.Text, searchTextBox.Text); 

    //Apply color to all matching text 
    int matchCount = 0; 
    foreach (Match match in matches) 
    { 
     richTextBox1.Select(match.Index, match.Length); 
     //richTextBox1.SelectionColor = System.Drawing.Color.Red; 
     richTextBox1.SelectionColor = 
      matchCount++ % 2 == 0 ? evenColor : oddColor; 
    } 
} 

。余分なロジックがあれば、それも組み込むことができます。私は確信しています。

編集:WPFでは動作しません。 WinFormsの投稿を保存する。

関連する問題