现在的位置: 首页 > 综合 > 正文

C#给RichTextBox添加查找画面

2012年02月26日 ⁄ 综合 ⁄ 共 3315字 ⁄ 字号 评论关闭

 

代码

代码
/*
--整理者:永恒de影

--整理时间:2010/06/08

--内容:C#如何给RichTextBox添加查找画面:
*/

//★★★★★★★★★案例分析:★★★★★★★★★★★★★★★★★★★★★★★★★★★
//环境:C#
//1 我有一个RichTextBox控件,里面有很多的内容,我想按下按钮(ctrl+F)弹出一个查找画面
//2 在查找画面中我输入一个值进行查找,可以对父页面中RichTextBox中的内容进行查询
//3 对查找出来的内容,以特殊的颜色标示出来

//★★★★★★★★★实现方法:★★★★★★★★★★★★★★★★★★★★★★★★★★★
//第一步:在richTextBox1_KeyDown事件中加上一下代码:
    if (e.Control && e.KeyCode == Keys.F)
    {
       
//选中所有的RichTextBox的内容
       this.richTextBox1.SelectAll();
       
       
//改变RichTextBox的选中的字体颜色
       this.richTextBox1.SelectionColor = Color.Black;

       //改变RichTextBox的选中的字体的背景颜色
       this.richTextBox1.SelectionBackColor = Color.Moccasin;

       //选中richTextBox从0开始的0个字符
       this.richTextBox1.Select(00);
    
       
//把滚动条回滚到焦点所在位置
       this.richTextBox1.ScrollToCaret();

       //弹出查找画面
       frmLookFor frm = new frmLookFor();
       frm.Show(
this);
     }

//第二步:在查找画面中

//变量定义和对象实例化
public static string strKey = "";
private int index = 0;
private int k = 1;

strKey = this.textBox1.Text;

if (strKey == "")
{
    
return;
}

//フォーム初期化
frmRulerChk frm1 = (frmRulerChk)this.Owner;//为了在子窗体中得到主窗体的东西

//指定の文字列を検索
int m = System.Text.RegularExpressions.Regex.Matches(((RichTextBox)frm1.Controls["richTextBox1"]).Text, strKey, System.Text.RegularExpressions.RegexOptions.IgnoreCase).Count;

if (((RichTextBox)frm1.Controls["richTextBox1"]).Text != "")
{
   
if (k <= m)
   {
      
while ((index = ((RichTextBox)frm1.Controls["richTextBox1"]).Find(strKey, index, RichTextBoxFinds.None)) >= 0)
      {
       
//選択した文字列の色
        ((RichTextBox)frm1.Controls["richTextBox1"]).SelectionColor = Color.Red;

       //選択した文字列のBackColor
       ((RichTextBox)frm1.Controls["richTextBox1"]).SelectionBackColor = Color.Blue;

       //フォーカス移動
        ((RichTextBox)frm1.Controls["richTextBox1"]).Focus();

       //文字列選択
        ((RichTextBox)frm1.Controls["richTextBox1"]).Select(index, 5);

       //スクロールバーはフォーカスのところへ移動
        ((RichTextBox)frm1.Controls["richTextBox1"]).ScrollToCaret();
       index
++;
       k
++;

       if (k == m)
       {
          MessageBox.Show(
"検索の開始位置に達しました""注意", MessageBoxButtons.OK, MessageBoxIcon.Information);
          index 
= 0;
          k 
= 1;
        }

      return;
      }                       
   }
}

//★★如果要实现查询画面只弹出一个并且最小化的把状态改为正常状态:★★★★★★★★
//步骤一中代码改为:

//实例化要打开的窗体
public static frmLookFor frmlook = null;

if (e.Control && e.KeyCode == Keys.F)
{
  
if (frmRulerChk.frmlook == null)
  {
     
this.richTextBox1.SelectAll();

     this.richTextBox1.SelectionColor = Color.Black;
     
this.richTextBox1.SelectionBackColor = Color.Moccasin;
     
this.richTextBox1.Select(00);
     
this.richTextBox1.ScrollToCaret();
     frmRulerChk.frmlook 
= new frmLookFor();
     frmRulerChk.frmlook.Show(
this);
  }
  
else
  {
     
try
     {
        
if (frmRulerChk.frmlook.WindowState == FormWindowState.Minimized)
        { 
           frmRulerChk.frmlook.WindowState 
= FormWindowState.Normal;
        }
        frmRulerChk.frmlook.Activate();
      }
      
catch
      {
        
this.richTextBox1.SelectAll();

        this.richTextBox1.SelectionColor = Color.Black;
        
this.richTextBox1.SelectionBackColor = Color.Moccasin;
        
this.richTextBox1.Select(00);
        
this.richTextBox1.ScrollToCaret();
        frmLookFor frm 
= new frmLookFor();
        frm.Show(
this);
       }
   }
}

//★★如果想要改运行的窗体在任务栏中不显示图标:★★★★★★★★

//更改属性:ShowInTaskbar = false;

//注:在查找画面关闭的时候一定要加上这句代码:

frmRulerChk.frmlook 
= null;

 

 

抱歉!评论已关闭.