Disposing in the 4th dimension.....

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi all

I've got a little problem regarding the disposing of a form....
My program is like this: I've got a mainForm with a main function, but before loading this form, I need to display an other one (StartSelectionForm) where the user setup his name and other string information
Innocently and full of certainty C# will absorb my simple code, I wrote the following main function

static void Main()

new StartSelectionForm().Show()

Application.Run(new MainForm())


BUT, when I run this.Dispose(); in StartSelectionForm after the user clicked an OK button, I get the following error in the main function

An unhandled exception of type 'System.ObjectDisposedException' occurred in system.windows.forms.dl

Additional information: Cannot access a disposed object named "StartSelectionForm"

Does anyone has an Idea why this error comes up

Thank you for your time

Sylvain
 
Hi Sylvain,
That means after you are disposing the form you are tring to use the
underlying native windows controls. I suppose in your main form you read
some of the start form's controls' properties or so. Cannot exactly tell you
what is the problem because you haven't posted enough code, but my
suggestion is on start form's Ok button click event read all controls on the
form and cache the values in some member variables. Latter when the main
form needs them it will read those variables and won't tuch the native
controls.
However that is only a guess. You need to post more code (like what happen
when the user clicks OK and how the main form or other object read what the
user has entered) in order to receive more accurate answer.
--
B\rgds
100 [C# MVP]

Sylvain said:
Hi all,

I've got a little problem regarding the disposing of a form.....
My program is like this: I've got a mainForm with a main function, but
before loading this form, I need to display an other one
(StartSelectionForm) where the user setup his name and other string
information.
Innocently and full of certainty C# will absorb my simple code, I wrote the following main function:

static void Main()
{
new StartSelectionForm().Show();

Application.Run(new MainForm());
}

BUT, when I run this.Dispose(); in StartSelectionForm after the user
clicked an OK button, I get the following error in the main function:
 
Back
Top