Question

I'm building a Notepad. I have a Find and Replace form. When I click a button that form opens, user gives two input in two textboxes and press a button. Then the RichTextBoxes from the main form is supposed to get modified.

Here's the form of the FindAndReplace form :

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();
        }

The problem is its not working.. I'm getting a NullReferenceException in the line f1.MainText.Replace(findMe, replaceMe); Any idea?

Was it helpful?

Solution

Here you create a new instance of the form:

Form1 f1 = new Form1();

All properties are initialized to their default values (i.e. strings to null). Next you try to call the Replace method on the MainText property which is null and you get the exception:

f1.MainText.Replace(findMe, replaceMe);

You need to first initialize this property:

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

UPDATE:

When you create the FindAndReplace form you could pass to its constructor the current value of the text:

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);
        }
    }
}

OTHER TIPS

Your find and replace form must know about your main form. The way you are doing it, you are creating a completely new main form, which will have no text in the main text area. When you create the Find and Replace form, you should pass your parent form, or even just the main text, to the Find and replace form, then search the main form text from the form just passed in.

You want something like the following:

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();
    }
}

You could add static form references to your Program class.

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);
        }
    }
}

Then, from any form in your application, you'll be able to use Program.Form1 or Program.Form2 as an already instantiated reference.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top