2

メモ帳を作成しています。検索と置換のフォームがあります。フォームが開くボタンをクリックすると、ユーザーは2つのテキストボックスに2つの入力を入力し、ボタンを押します。次に、メインフォームの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();
        }

問題は、機能していないことです。行にNullReferenceExceptionが発生しf1.MainText.Replace(findMe, replaceMe); ます。何か考えはありますか?

4

3 に答える 3

2

ここで、フォームの新しいインスタンスを作成します。

Form1 f1 = new Form1();

すべてのプロパティはデフォルト値に初期化されます(つまり、文字列はnullになります)。次に、あるプロパティでReplaceメソッドを呼び出そうとすると、例外が発生します。MainTextnull

f1.MainText.Replace(findMe, replaceMe);

最初にこのプロパティを初期化する必要があります。

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

アップデート:

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);
        }
    }
}
于 2010-02-13T16:15:34.350 に答える
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();
    }
}
于 2010-02-13T16:23:08.477 に答える
-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.Form1またはProgram.Form2すでにインスタンス化された参照として使用できるようになります。

于 2010-02-13T16:23:09.680 に答える