2010-11-19 44 views
0

我有一个C#程序,利用查找功能,但它能够找到该单词,但没有突出显示在richTextBox中找到的单词。C#如何编程查找功能

有人可以告诉我的代码?

感谢。

查找函数类形式:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace Syscrawl 
{ 
public partial class Find_Form : Form 
{ 
    FTK_Menu_Browsing_History fmbh = new FTK_Menu_Browsing_History(); 

    public Find_Form() 
    { 
     InitializeComponent(); 
    } 

    public void searchButton_Click(object sender, EventArgs e) 
    { 
     string s1 = fmbh.getSearchBrowsing().ToLower(); 
     string s2 = textBoxSearch.Text.ToLower(); 

     if (s1.Contains(s2)) 
     { 
      MessageBox.Show("Word found!"); 

      this.fmbh.richTextBoxBrowsing.Find(s2); 
      this.fmbh.richTextBoxBrowsing.SelectionLength = s2.Length; 
      this.fmbh.richTextBoxBrowsing.SelectionColor = Color.Red; 
      this.Close(); 
     } 
     else 
     { 
      MessageBox.Show("Word not found!"); 
     } 
    } 
} 
} 
+0

有点困惑,你有没有将选择的开始设置为find的结果? – phill 2010-11-19 03:03:24

回答

1

你需要选择你在找什么第一。此:

int offset = s1.IndexOf(s2); 
richTextBox1.Select(offset, s2.Length); 

之后,您可以使整个突出显示。另一个技巧,以防止在选择过程中闪烁,在你的方式使用此代码:

protected override void WndProc(ref Message m) 
{ 
    if (m.Msg == 0) { 
     if (!_doPaint) 
      return; 
    } 

    base.WndProc(ref m); 
} 

之前选择任何设置_doPaint假后选择其设置为true。

希望我能帮上忙!

+0

不起作用): – athgap 2010-11-19 18:41:49

0

您需要致电s1.IndexOf(s2, StringComparison.CurrentCultureIgnoreCase)查找匹配的位置。

此外,它看起来像您的查找表单创建自己的历史窗体的实例;它不使用现有的实例。
您应该考虑接受构造函数参数。