Click here to Skip to main content
15,911,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
As follows:

C#
private delegate void TestHandler();
private TestHandler mTestDelegate;

public fmMain()
{
  InitializeComponent();
}

private void fmMain_Load(object sender, EventArgs e)
{
  mTestDelegate = new TestHandler(() =>
  {
      Form fmTest = new Form();
      fmTest.ShowDialog();
  });
  mTestDelegate.BeginInvoke(null, null);
}


if i use "fmTest.Show()",then "fmTest" will not be displayed,it's being stuck!
but,use "fmTest.ShowDialog()",it can be displayed!
who can tell me why ??
Posted

1 solution

Simple: ShowDialog waits fro the form to close before it continues, Show doesn't.

So if you do this:
C#
MyDialog md = new MyDialog();
md.Text = "Hello there!";
md.ShowDialog();
Console.WriteLine(md.Text);
Nothing will show on the console output until the user closes the form (normally with the OK or Cancel button, but it could be the "X" or CTRL-F4 instead). What will be displayed depends on whether the user changes the value of the Txt property before he closes the form. ShowDialog is a Blocking method.

But if you do this:
C#
MyDialog md = new MyDialog();
md.Text = "Hello there!";
md.Show();
Console.WriteLine(md.Text);
The the text "Hello there!" will be output immediately. You can use both the original form and the new one at the same time. The user can change the Text property all he likes, but it will never again cause the Console.WriteLine to be executed!

ShowDialog is useful when you want to present info to a user, or let him change it, or get info from him before you do anything else.

Show is useful when you want to show information to the user but it is not important that you wait fro him to be finished.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900