Form won't stay away...

  • Thread starter Thread starter Tim
  • Start date Start date
T

Tim

Hi

I have the following code to open a form modally. It works great only the
second time I have to close it twice, the third time 4 times.

I am using Dispose(), why is it coming back so much?

Tim

MM.Forms.frmPriceCalc fCalc = new MM.Forms.frmPriceCalc();
fCalc.RegPriceCalc = true; //regular price
fCalc.calcSource = 2;
fCalc.LoadFormInfo();
if(fCalc.ShowDialog(this) == DialogResult.OK)
{
//price has changed refresh the grid.
GridDataPreload();
}
fCalc.Dispose();
 
Tim,

Are you running this in an event handler for a control? If so, it is
possible that an exception is being thrown, which is preventing the Dispose
method from being called, and leaving the instance hanging around.

In order to get around this, you should code it like this:

using (MM.Forms.frmPriceCalc fCalc = new MM.Forms.frmPriceCalc())
{
fCalc.RegPriceCalc = true; //regular price
fCalc.calcSource = 2;
fCalc.LoadFormInfo();

if(fCalc.ShowDialog(this) == DialogResult.OK)
{
//price has changed refresh the grid.
GridDataPreload();
}
}

This way, if an exception is thrown, you will dispose of the form
properly.

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top