problem with multiple forms

Tech-Archive recommends: Speed Up your PC by fixing your registry

From: GS-SE (GSSE_at_discussions.microsoft.com)
Date: 01/18/05


Date: Tue, 18 Jan 2005 12:57:07 -0800

I'm writing an app that has a main form where, upon user request, must load a
new form (which will act as a modal dialog). In the new form, the user does
a few things, and then when they are finished, they are returned to the main
form with a result (similar to a DialogResult). This will occur several
times. I've seen many posts about similar actions and I've experimented with
the answers given. However, I can't find a solution that fits my problem.

I'm using the OnLoad event for my main form to hook up events and set up the
2D mapping control. However, I have a flag that only allows the OnLoad
events to occur the very first time the Dialog Loads (not sure that's a good
idea):

private void mainForm_OnLoad(...)
{
  if (firstTime)
   {
       ...Initialize
   }
}

The user presses a button on the main form, which calls a private method
which loads the secondary form like this:

private void Launch()
{
  this.Text = "";
  this.Hide(); // Do I need this?

  DialogResult res;
  // Create new form
  MyDialog dlg = new MyDialog();
  if ((res = dlg.ShowDialog()) == DialogResult.OK)
  {
      // Update Info from User Action
      UpdateInfo();
      this.Text = "AppName";
      this.ShowDialog();
  }
  else if (res == DialogResult.Cancel)
  {
      this.Text = "AppName";
      this.ShowDialog();
  }
}

It works fine the first time, however, the second time it stops at the
ShowDialog call with the following error: An unhandled exception of type
'System.ArgumentException' occurred in System.Windows.Forms.dll

Anyone know why that would happen (or need more information to help figure
it out)?? :)

Thanks
-G


Quantcast