“Show()”和“ShowDialog()”之间有什么区别? [英] What is the difference between "Show() "and "ShowDialog()"?

查看:111
本文介绍了“Show()”和“ShowDialog()”之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如下:



As follows:

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);
}





如果我使用fmTest.Show(),那么fmTest将不会显示,它正在卡住了!

但是,使用fmTest.ShowDialog(),它可以显示!

谁能告诉我为什么?



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 ??

推荐答案

简单:ShowDialog在表单继续之前等待表单关闭,Show不会。



所以如果你这样做:

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

So if you do this:
MyDialog md = new MyDialog();
md.Text = "Hello there!";
md.ShowDialog();
Console.WriteLine(md.Text);

在用户关闭表单之前,控制台输出上不显示任何内容(通常使用确定或取消按钮,但它可能是X或者CTRL-F4)。将显示的内容取决于用户在关闭表单之前是否更改Txt属性的值。 ShowDialog是一种阻止方法。



但如果你这样做:

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:

MyDialog md = new MyDialog();
md.Text = "Hello there!";
md.Show();
Console.WriteLine(md.Text);

文本Hello there!将立即输出。您可以同时使用原始表单和新表单。用户可以随意更改Text属性,但它永远不会再导致执行Console.WriteLine!



ShowDialog在您想要提供信息时非常有用在你做任何其他事情之前给用户,或者让他改变它,或者从他那里得到信息。



当你想向用户显示信息时显示很有用但是你等他完成并不重要。

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.


这篇关于“Show()”和“ShowDialog()”之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆