ShowDialog Vs. Show

T

tolemaC

hi guys, I have this small example:

I have two forms, Form1 and Form2, Form1 is MainForm in my application,

The Form2 has this attribute:

public Hashtable ht;

and a TextBox too(textBox1);

in the constructor of Form2 after initialize components call, I create the
hash table and add an element:

ht = new Hashtable();
ht.Add("1", "Hello");

ok, Form2 has a method call "InitAll", is this:

public void InitAll()
{
textBox1.Text = ht["1"].ToString();
}

well, in Form1_Load I have this.

frm2 = new Form2();

frm2 is a Form1 attribute (Form2 frm2;)

ok, Form1 has a Button(button1), in button1_Click I have this code:

frm2.InitAll();
frm2.Show();

Form2 have MinimizeBox and MaximizeBox to false.


Ok, run application push button1 in Form1 and form2 is showed, close Form2
and push button1 other time and I get the next Exception:

"An un handled exception of type 'System.ObjectDisposedException' ocurred in
System.Windows.Forms.dll"

this ocurred when execute the "frm2.InitAll()" function in line:
textBox1.Text = ht["1"].ToString();

Well, if I open the Form2 with ShowDialog method then work fine, but if I
use Show I get this exception.

Why?

..Javier Ros Moreno.
 
W

William Ryan

You instantiate the form2 object in Form1 load and when
you close it with the maximize/minimize buttons it's
disposed and ready for GC so when you call it again, it's
disposed.

That isn't the case with ShowDialog. I'm pretty sure
you'll get the exception the next time you reference
frm2..it's just that InitAll is the first method that's
called.

I think you can fix it by Declaring frm2 in the
form_load...then on the button click use frm2 = New Form2
();
then call frm2.InitAll();
frm2.Show();

I'm pretty sure this is the difference.

Good Luck,

Bill
-----Original Message-----
hi guys, I have this small example:

I have two forms, Form1 and Form2, Form1 is MainForm in my application,

The Form2 has this attribute:

public Hashtable ht;

and a TextBox too(textBox1);

in the constructor of Form2 after initialize components call, I create the
hash table and add an element:

ht = new Hashtable();
ht.Add("1", "Hello");

ok, Form2 has a method call "InitAll", is this:

public void InitAll()
{
textBox1.Text = ht["1"].ToString();
}

well, in Form1_Load I have this.

frm2 = new Form2();

frm2 is a Form1 attribute (Form2 frm2;)

ok, Form1 has a Button(button1), in button1_Click I have this code:

frm2.InitAll();
frm2.Show();

Form2 have MinimizeBox and MaximizeBox to false.


Ok, run application push button1 in Form1 and form2 is showed, close Form2
and push button1 other time and I get the next Exception:

"An un handled exception of
type 'System.ObjectDisposedException' ocurred in
System.Windows.Forms.dll"

this ocurred when execute the "frm2.InitAll()" function in line:
textBox1.Text = ht["1"].ToString();

Well, if I open the Form2 with ShowDialog method then work fine, but if I
use Show I get this exception.

Why?

..Javier Ros Moreno.








.
 

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

Top