2010-02-13 34 views
2

我正在構建一個記事本。我有一個查找和替換表單。當我點擊窗體打開的按鈕時,用戶在兩個文本框中輸入兩個輸入,然後按下一個按鈕。然後主窗體中的RichTextBoxes應該被修改。如何在C#.NET中修改其他窗體的RichTextBox

這裏是FindAndReplace表格的形式:

private void btnReplaceAll_Click(object sender, EventArgs e) 
     { 
      string findMe = txtFind.Text; 
      string replaceMe = txtReplace.Text; 
      Form1 f1 = new Form1(); 
      f1.MainText.Replace(findMe, replaceMe); 
      //this.Hide(); 
     } 

的問題是它不工作..我在該行f1.MainText.Replace(findMe, replaceMe); 任何想法得到一個NullReferenceException?

回答

2

在這裏,您創建窗體的新實例:

Form1 f1 = new Form1(); 

所有屬性都初始化爲它們的默認值(即字符串爲null)。接下來,您嘗試呼籲MainText屬性,它是nullReplace方法,你會得到異常:

f1.MainText.Replace(findMe, replaceMe); 

您需要先初始化這個屬性:

f1.MainText = "blablabla"; 
f1.MainText = f1.MainText.Replace(findMe, replaceMe); 

UPDATE:

當您創建FindAndReplace表單時,您可以將文本的當前值傳遞給其構造函數:

public class Form1 : Form 
{ 
    protected void FindAndReplace_Click(object sender, EventArgs e) 
    { 
     var findAndReplaceForm = new FindAndReplaceForm(MainText.Text); 
     findAndReplaceForm.ShowDialog(); 
     MainText.Text = findAndReplaceForm.NewText; 
    } 
} 

public class FindAndReplaceForm : Form 
{ 
    private readonly string _originalText; 

    public FindAndReplaceForm(string originalText) 
    { 
     _originalText = originalText; 
    } 

    public string NewText 
    { 
     get 
     { 
      return (_originalText ?? string.Empty).Replace(findMe, replaceMe); 
     } 
    } 
} 
+0

還應該指出,調用'f1.MainText.Replace'不會做任何事情。 'Replace'返回一個新的字符串和所要求的替換;它不執行就地修改(字符串是不可變的)。 – 2010-02-13 16:20:45

+0

我需要用'MainText'的當前值初始化'f1' ..任何方式? – 2010-02-13 16:20:48

+0

@Bibhas,你認爲'當前值'是什麼意思?它存儲在哪裏? – 2010-02-13 16:22:37

0

您的查找和替換窗體必須知道您的主窗體。你這樣做的方式,你正在創建一個全新的主表格,在主文本區域沒有文字。創建查找和替換表單時,應將父表單或甚至是主文本傳遞給查找和替換表單,然後從剛剛傳入的表單中搜索主表單文本。

您希望如下所示:

public class FindAndReplaceForm 
{ 
    private Form1 MainForm; 

    public FindAndReplaceForm(Form1 parentForm) 
    { 
     this.MainForm = parentForm; 
     //The rest of you constructor here 
    } 

    private void btnReplaceAll_Click(object sender, EventArgs e) 
    { 
     string findMe = txtFind.Text; 
     string replaceMe = txtReplace.Text; 

     //The following line will search the parent form 
     this.MainForm.MainText.Replace(findMe, replaceMe); 
     //this.Hide(); 
    } 
} 
+0

層次結構可能會帶來多種形式的麻煩。 – Lazlo 2010-02-13 16:24:15

+0

'this.ParentForm = parentForm;'這一行無效。 'this.parentform'獲取父窗體的一個實例.. – 2010-02-13 16:30:26

+1

@Bibhas,你是不正確的。它是一個實例變量,它對獲取或設置沒有限制。 – 2010-02-13 16:50:50

-1

您可以將靜態表單引用添加到您的Program類。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    static class Program 
    { 
     public static Form1 F1 { get; set; } 
     public static Form2 F2 { get; set; } 

     /// <summary> 
     /// The main entry point for the application. 
     /// </summary> 
     [STAThread] 
     static void Main() 
     { 
      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 

      Form1 = new Form1(); 
      Form2 = new Form2(); 

      Application.Run(Form1); 
     } 
    } 
} 

然後,任何形式的應用程序,你就可以使用Program.Form1Program.Form2爲已經實例化的參考。

+0

如果我們需要使用Form1訪問它,爲什麼我們需要'F1'和'F2'? – 2010-02-13 16:36:24

+0

這並沒有真正回答這個問題。 – 2010-02-13 16:58:13

相關問題