简体   繁体   中英

replacing show() with showdialog()

I am opening a form at run time from the main gui form using form.showdialog();

I set the proppeties likeform should appear in center etc

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

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.

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.

try following code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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