Show() vs ShowDialog() in C# Windows Forms Application



Open a new Form in Windows Application

In this c# windows application tutorial we will learn how to open a windows from from another form. we can open a form by two ways Form.Show() and Form.ShowDialog() methods in windows forms application. We will learn both methods Show() and ShowDialog() with an example in this c# post.
In this example we need two forms for demo, one is main form works as parents and the form will be opened works as child form.


Show()

Show() used to open new form, when we use show() method it allow us to perform any action on parent page.
We can open same form multiple times when we use Show() method to open new window, because it allow to click on parent form after opened child form.

Form frm2 = new Form2();
frm2.Show();


ShowDialog()

ShowDialog() method used to open new form, but it not allow to focus on parent window after opened child form and we can not perform any action on parent form.
When we use ShowDialog() method to open new windows, it does not allow to open same windows multiple time.

Form frm2 = new Form2();
frm2.ShowDialog();


Example :

Here, we have two windows form Form1 and Form2. We try to open Form2 from Form1, so we need a button control on Form1 for open Form2.

meeraacademy.com
Difference between Show() and ShowDialog()

Write below code in button control click events to open a Form2.

private void button1_Click(object sender, EventArgs e)
{
     Form frm2 = new Form2();
     frm2.Show();
}

The output of above example:

meeraacademy.com
Difference between Show() and ShowDialog()

In above figure we can see that we opened Form2 multiple time from Form1 as we use Show().

In next c#.net windows application tutorial we will learn more about Show() and ShowDialog() with example.

Leave a Reply

Your email address will not be published. Required fields are marked *