简体   繁体   English

用showdialog()替换show()

[英]replacing show() with showdialog()

I am opening a form at run time from the main gui form using form.showdialog(); 我正在运行时使用form.showdialog();从主GUI表单打开一个表单form.showdialog();

I set the proppeties likeform should appear in center etc 我设置属性likeform应该出现在中心等

 form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
form.ClientSize = new System.Drawing.Size(200, 50);
form.StartPosition = FormStartPosition.CenterParent;

and added a label 并添加了标签

Label popupLabel1 = new Label();
form.Controls.Add(popupLabel1);

Problem is when i replace form.showdialog() with form.show() I cant see the content of label and now this new form does not appear in the center. 问题是当我用form.show()替换form.showdialog()时,我看不到标签的内容,现在这个新表单没有出现在中间。 Why these set properties are not occuring ? 为什么这些设置属性没有出现?

Thanls 谢谢

You aren't showing your full code, which is necessary in the case. 您没有显示完整的代码,这是必要的。 When and where is what code executed? 何时何地执行什么代码?

What you need to remember is that .Show() is not a blocking call, while .ShowDialog() is a blocking call. 您需要记住的是.Show()不是阻塞调用,而.ShowDialog()是阻塞调用。 This means that if you have code after the .Show/ShowDialog call, this won't be executed immediately when you use ShowDialog - it will be executed when the form is closed. 这意味着,如果在.Show / ShowDialog调用之后有代码则在使用ShowDialog时不会立即执行该代码-在关闭窗体时将执行该代码。

Assuming you have code like this: 假设您有如下代码:

var form = new YourForm();
form.Show(); // NOT BLOCKING!
form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
form.ClientSize = new System.Drawing.Size(200, 50);
form.StartPosition = FormStartPosition.CenterParent;
Label popupLabel1 = new Label();
form.Controls.Add(popupLabel1);

If you change the Show to ShowDialog, then you need to move it to the end, after the creation of the labels. 如果将Show更改为ShowDialog,则需要在创建标签后将其移到末尾。

var form = new YourForm();
form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
form.ClientSize = new System.Drawing.Size(200, 50);
form.StartPosition = FormStartPosition.CenterParent;
Label popupLabel1 = new Label();
form.Controls.Add(popupLabel1);
form.ShowDialog(); // BLOCKING!

When you are displaying a form using Show() and not ShowDialog(), you need to set its MDI parent child properties. 当使用Show()而不是ShowDialog()显示表单时,需要设置其MDI父子属性。

try following code: 尝试以下代码:

this.IsMdiContainer = true;
form.MdiParent = this;
form.Show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM