if the reference equals to null, it's normal that the dispose method fails
(because it is called on a null object).
take a look at the using statement :
using(frmMain f = new frmMain())
{
f.ShowDialog();
}
If you need navigate throught multiple forms, I suggest you to have a main
form (or a console application why not) that have a method display form.
Each time the user request a form, close the current window, dispose it and
then open the newly requested form...
ex:
class frmmain : Form
{
private BaseForm _currentForm;
public void SetCurrentForm(Type formType)
{
_currentform.Close();
_currentForm.dispose();
_currentform = (BaseForm)Activator.CreateInstance(formType, this);
_currentForm.ShowDialog();
}
}
class BaseForm : BaseForm
{
protected frmmain _mainForm;
public BaseForm(frmmain mainform)
{
_mainForm = mainform;
}
}
class Form1 : Baseform
{
public Form1(frmmain mainform) : base(mainform)
{
}
private void button1_Click(object sender, EventArgs e)
{
_mainForm.SetCurrentForm(typeof(Form2));
}
}
Steve