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?
Here you create a new instance of the form:
All properties are initialized to their default values (i.e. strings to null). Next you try to call the
Replacemethod on theMainTextproperty which isnulland you get the exception:You need to first initialize this property:
UPDATE:
When you create the FindAndReplace form you could pass to its constructor the current value of the text: