[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7533
  • Last Modified:

Diffrence between Show, ShowDialog, ShowDialog(owner)

Hi All,,

I want to know the difference between:

Show();
ShowDialog();
ShowDialog(owner);

This is in visual c# .Net 2.0

When to use each of them?

Cheers,

0
Abu Hamdan
Asked:
Abu Hamdan
  • 3
  • 2
  • 2
  • +2
3 Solutions
 
DhaestCommented:
The form class' ShowDialog method functions differently than its Show method. Here's how:
ShowDialog's returned value can be used to determine how the user closed the form (using the DialogResult property).

ShowDialog does not call the Dispose method automatically on its close. However, Show, when modeless, does dispose upon close.
One of the benefits of the modal dialog not being disposed after it's closed is that the data can be retrieved from the modal form.
A good practice is to call the Dispose method in a final block, when using the ShowDialog method, which releases resources and handles from memory.


0
 
JimBrandleyCommented:
A few more differences:
ShowDialog() is not used with an MDI child form.

Show(owner) allows you to pass the parent form to a child form. Then the child form can cast that reference to the class of the parent form, and use that reference to interact with public properties and methods in the parent form.

Jim
0
 
Abu HamdanEnterprise Architect, PM ExpertAuthor Commented:
Hi Dhaest
Can u give an example where to free resources when using showDialog?
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
Abu HamdanEnterprise Architect, PM ExpertAuthor Commented:
Hi Jim,

Why ShowDialog not used with MDI forms? and how can i get refrence of parent form using showOwner? what methid that i can recieve the form refrence in on the called child form?

thanks,
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
The bigger picture:

Show() displays the form as modeless.  This allows the user to interact with the newly showed form AND any other forms that are already open.

ShowDialog() displays the form as modal.  This PREVENTS the user from interacting with any other forms until the modal dialog is closed.  Code in the "callee" (where the dialog was shown from) is HALTED until the modal dialog is closed.
0
 
JimBrandleyCommented:
I think Idle_Mind answered your question about MDI forms.

Say your main form is Form1, and a child is Form2. Then:

Form2 child = new Form2();
child.Show(this);

Now, say Form1 has a public string property called Foo. Then in Form2 you can:
Form1 parent = this.Owner as Form1;
this.textBox1.Text = parent.Foo;
or
parent.Foo = this.textBox1.Text;

Jim

0
 
DhaestCommented:
Example with showDialog...

dim myForm as new form1
myForm.showdialog()
myForm.dispose()
0
 
Abu HamdanEnterprise Architect, PM ExpertAuthor Commented:
Thanks for useful information guys,, i think that you all helped me very well , and i will split the points
0
 
sumitp_143Commented:
One more difference between Show() and ShowDialog() is how the objects in the calling forms are handled.

If you create any object in the calling class and then call other form using ShowDialog() then the object would be claimed by Garbage Collection if Garbage Collection takes place and the other form is still object open.

If you you Show() instead all the objects declared in the calling form are always active and Garbage Collection does not claim them.

So if you have an object in the calling form, which you want to be active even when the user is working on other form always use Show() and do not use ShowDialog()
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now