Move all the Form B internal variable initialization to its constructor
Here is how your formA will look like. It has 2 buttons: One simply initializes an instance of formb and call the public property. The other button displays formB. Form_load is only called when you show the form to the user via Show() or ShowDialog() calls.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace formload
{
public partial class FormA : Form
{
public FormA()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
FormB frm = new FormB();
MessageBox.Show(frm.MyProperty);
frm = null;
}
private void button2_Click(object sender, EventArgs e)
{
FormB frm = new FormB();
frm.ShowDialog();
MessageBox.Show(frm.MyProperty);
frm = null;
}
}
}
Here is how formb would look:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace formload
{
public partial class FormB : Form
{
public FormB()
{
InitializeComponent();
myPropString = "Default set via constructor";
}
private void FormB_Load(object sender, EventArgs e)
{
myPropString = "Set from form load";
}
private string myPropString;
public string MyProperty
{
get { return myPropString; }
set { myPropString = value; }
}
}
}