我创建一个新表单并从父表单调用如下:
loginForm = new SubLogin();
loginForm.Show();
Run Code Online (Sandbox Code Playgroud)
我需要在父级的中心显示子表单.所以,在子表单加载中我做了foll:`
Point p = new Point(this.ParentForm.Width / 2 - this.Width / 2, this.ParentForm.Height / 2 - this.Height / 2);
this.Location = p;
Run Code Online (Sandbox Code Playgroud)
但是这会抛出错误,因为父表单为空.我也尝试设置Parent属性,但没有帮助.有什么输入吗?
Qui*_*son 127
尝试:
loginForm.StartPosition = FormStartPosition.CenterParent;
loginForm.ShowDialog(this);
Run Code Online (Sandbox Code Playgroud)
当然,子的for will现在是父窗口的阻塞形式(对话框),如果不需要那么只需替换ShowDialog为Show..
loginForm.Show(this);
Run Code Online (Sandbox Code Playgroud)
您仍然需要指定StartPosition.
小智 18
"父母"和"所有者"之间似乎存在混淆.如果您打开一个MDI形式的表单,即嵌入另一个表单内,那么这个周围的表单就是Parent.具有值FormStartPosition.CenterParent的表单属性StartPosition引用此属性.您可以传递给Show方法的参数是Owner,而不是Parent!这就是为什么frm.StartPosition = FormStartPosition.CenterParent无法正常工作的原因.
如果将其StartPosition设置为Manual,则表单中放置的以下代码将使其所有者具有一定的偏移量.小偏移以平铺方式打开表单.如果所有者和拥有的表单具有相同的大小或者您打开多个拥有的表单,则这是一个优势.
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
if (Owner != null && StartPosition == FormStartPosition.Manual) {
int offset = Owner.OwnedForms.Length * 38; // approx. 10mm
Point p = new Point(Owner.Left + Owner.Width / 2 - Width / 2 + offset, Owner.Top + Owner.Height / 2 - Height / 2 + offset);
this.Location = p;
}
}
Run Code Online (Sandbox Code Playgroud)
Mat*_*ley 12
假设您的代码在父表单中运行,那么这可能是您正在寻找的内容:
loginForm = new SubLogin();
loginForm.StartPosition = FormStartPosition.CenterParent
loginForm.Show(this);
Run Code Online (Sandbox Code Playgroud)
对于记录,还有一个Form.CenterToParent()功能,如果你需要在创建之后将其置于中心,无论出于何种原因.
小智 11
除非我使用,否则父母的设置对我不起作用form.ShowDialog();.
在使用之前使用form.Show(); 或form.Show(this);没有工作,this.CenterToParent();.我只是把它放在表单的Load方法中.一切都很好.
设置了父级中心的起始位置,并在使用阻止showdialog时起作用.
在表单中启动MDIForm表单时,您需要使用.CenterScreen而不是.CenterParent.
FrmLogin f = new FrmLogin();
f.MdiParent = this;
f.StartPosition = FormStartPosition.CenterScreen;
f.Show();
Run Code Online (Sandbox Code Playgroud)
childform = new Child();
childform.Show(this);
Run Code Online (Sandbox Code Playgroud)
在事件子窗体加载
this.CenterToParent();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
128434 次 |
| 最近记录: |