C#給RichTextBox添加尋找畫面

來源:互聯網
上載者:User

 

代碼

代碼
/*
--整理者:永恒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(0, 0);
    
       //把捲軸復原到焦點所在位置
       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(0, 0);
     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(0, 0);
        this.richTextBox1.ScrollToCaret();
        frmLookFor frm = new frmLookFor();
        frm.Show(this);
       }
   }
}

//★★如果想要改啟動並執行表單在工作列中不顯示表徵圖:★★★★★★★★

//更改屬性:ShowInTaskbar = false;

//註:在尋找畫面關閉的時候一定要加上這句代碼:

frmRulerChk.frmlook = null;

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.