2010-02-28 25 views
0

我有2種形式代碼問題。在RTB搜索文本C#

一個包括一個RichTextBox另一種是用在這個RTB

我的代碼是否顯示錯誤,我不知道如何解決它來搜索文本。

這是顯示錯誤

RichTextBox box = ((Form1)base.Owner).rtxtEditor; 

它稱該行「對象引用不設置到對象的實例。」

這是我的整個代碼。

private void frmFind_Shown(object sender, EventArgs e) 
    { 
     this.txtSearch.Focus(); 
    } 

    private void cmdFind_Click(object sender, EventArgs e) 
    { 

     RichTextBox box = ((Form1)base.Owner).rtxtEditor; 
     int start = box.Find(this.txtSearch.Text, 0); 
     if (start == -1) 
     { 
      this.lblMatch.Text = "No match found"; 
      this.cmdFindNext.Enabled = false; 
     } 
     else 
     { 
      this.lblMatch.Text = ""; 
      box.Select(start, this.txtSearch.Text.Length); 
      this.cmdFindNext.Enabled = true; 
      box.ScrollToCaret(); 
      ((Form1)base.Owner).Focus(); 
     } 
    } 

    private void cmdFindNext_Click(object sender, EventArgs e) 
    { 

      RichTextBox box = ((Form1)base.Owner).rtxtEditor; 
      int start = box.Find(this.txtSearch.Text, ((Form1 base.Owner).rtxtEditor.SelectionStart + 1, 0); 
      if (start == -1) 
      { 
       this.lblMatch.Text = "No more matches"; 
       this.cmdFindNext.Enabled = false; 
      } 
      else 
      { 
       box.Select(start, this.txtSearch.Text.Length); 
       box.ScrollToCaret(); 
       ((Form1)base.Owner).Focus(); 
      } 

請幫忙!我我的截止日期是2morrow

+0

你是同一個人http://stackoverflow.com/users/278779/pops還是你在他的課堂上? – SLaks 2010-02-28 18:28:25

回答

1

Owner屬性爲null除非你手動設置或致電Show重載需要一個Form參數。

您需要將顯示查找表格的代碼更改爲form.Show(this)

+0

謝謝!我討厭這麼小的東西是如此容易修復 – Christi 2010-02-28 18:32:53

+0

你應該接受這個答案,通過點擊空白檢查 – SLaks 2010-02-28 18:42:05