C# 设置表单的父级
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2217716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set the Parent of a Form
提问by Greycrow
I have a Windows form from which I would like to open a status form that says "Saving..." and then disapears when the saving is complete. I would like to center this small status form in the middle of the calling form. I've tried setting the "StartPosition" propery to "CenterParent", but it doest work. I create the status form from the other form like so:
我有一个 Windows 表单,我想从中打开一个状态表单,上面写着“正在保存...”,然后在保存完成后消失。我想将这个小的状态表格放在呼叫表格的中间。我试过将“StartPosition”属性设置为“CenterParent”,但它不起作用。我从另一个表单创建状态表单,如下所示:
SavingForm saving = new SavingForm();
savingForm.Show();
Thread.Sleep(500); //Someone said this is bad practice ... why?
savingForm.Close();
Wouldn't the calling form be the "Parent"? When I set a watch for saving it says it has no parent.
调用表单不是“父”吗?当我设置手表进行保存时,它说它没有父级。
I tried:
我试过:
SavingForm saving = new SavingForm();
saving.Parent = this;
savingForm.Show();
Thread.Sleep(500);
savingForm.Close();
and it throws an exception "Top-level control cannot be added to a control."
并引发异常“无法将顶级控件添加到控件中”。
How do I center this status window in the calling window?
如何在调用窗口中居中此状态窗口?
Thanks in advance
提前致谢
采纳答案by Paul Sasik
i would do something like this:
我会做这样的事情:
SavingForm saving = new SavingForm();
savingForm.ShowDialog(this);
In SavingForm i would start a timer in the load handler that runs for 500 milliseconds and then closes the form when done. Cleaner that way. ShowDialog will also lock your UI to only display the saving form and not allow the user to monkey with anything.
在 SavingForm 中,我将在负载处理程序中启动一个计时器,该计时器运行 500 毫秒,然后在完成后关闭表单。那样更干净。ShowDialog 还将锁定您的 UI 以仅显示保存表单,而不允许用户随意更改任何内容。
回答by Cory Charlton
Use this:
用这个:
saving.Show(this);
To set the Owner when you show the form.
在显示表单时设置所有者。
Edit:The ShowDialog()
method also has an overload that let's you specify the owner if that is the route you decide to go:
编辑:该ShowDialog()
方法还有一个重载,让您指定所有者,如果这是您决定走的路线:
saving.ShowDialog(this);
回答by Matt
If you pass the parent (this
) to the Owner, like
如果您将父 ( this
) 传递给所有者,例如
SavingForm saving = new SavingForm() { Owner = this };
then you can access Owner's properties and methods in the child form (in this case SavingForm
), provided that the Owner's properties Modifier
is set to Internal
or Public
for each property you need to access (you can either edit the modifier directly in the source code, or via form's designer properties- there is a Modifier
property for each control).
那么你就可以访问用户在子窗体属性和方法(在这种情况下SavingForm
),前提是业主的属性Modifier
被设置为Internal
或Public
为每个需要访问的(你可以编辑修改直接在源代码中,或通过形式的财产设计器属性-Modifier
每个控件都有一个属性)。
You can find a nice explanation of the differences between Owner, Parent and ParentForm here.
您可以在此处找到关于 Owner、Parent 和 ParentForm 之间差异的很好的解释。
Note:Passing it like saving.Show(this);
or saving.ShowDialog(this);
did not help in my case.
注意:在我的情况下传递它saving.Show(this);
或saving.ShowDialog(this);
没有帮助。