有没有办法使用代码设置Windows窗体表单的StartPosition?似乎无论我尝试什么导致StartPostion是默认值.
以下是我在要显示的表单中所做的事情:
public DealsForm()
{
InitializeComponent();
this.StartPosition = FormStartPosition.CenterParent;
}
Run Code Online (Sandbox Code Playgroud)
这是我正在做的显示表单:
private void nvShowDeals_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
DealsForm frm = new DealsForm();
frm.DataSource = this.Deals;
frm.Show(this);
}
Run Code Online (Sandbox Code Playgroud)
我已尝试在上述每种方法中加入以下内容,但无济于事:
this.StartPosition = FormStartPosition.CenterParent;
Run Code Online (Sandbox Code Playgroud)
如果我通过属性编辑器设置它...它完美地工作,但我真的想通过代码来做.
应该是一个明智的选择......但对于我的生活,我似乎无法弄明白......也许我需要更多的咖啡因.
如果我做了一个ShowDialog()
并传递了它的父节点就可以了...但我真的不想把它显示为对话框.
spl*_*tne 18
也许你并不孤单.也许你不是疯了.阅读本文(Microsoft Connect客户反馈):
Windows窗体StartPosition属性仅适用于.ShowDialog方法,而不适用于.Show方法
客户:"Windows Form StartPosition仅适用于.ShowDialog方法而不适用于.Show方法.注意:我还附加了简单的代码和结果图像."
MS:"不幸的是,我们无法在将来的版本中解决这个特定问题,因为这里的修复将是对WinForms 1,1.1和2行为的重大改变"
Per*_*ion 11
如果我做一个ShowDialog()并传递它的工作原理...但我真的不想把它显示为对话框.
这是正确的,因为ShowDialog会设置frm.Parent == nvShowDeals.Parent
因为你使用.Show()然后frm.Parent == null因此会忽略FormStartPosition.CenterParent.
所以为了完成这个功能,我会做出以下改变:
public DealsForm()
{
InitializeComponent();
//this.StartPosition = FormStartPosition.CenterParent;
}
//DealsForm_Load Event
private void DealsForm_Load(object sender, EventArgs e)
{
this.Location = this.Owner.Location; //NEW CODE
}
Run Code Online (Sandbox Code Playgroud)
在这里,我将做出以下更改:
private void nvShowDeals_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
DealsForm frm = new DealsForm();
frm.DataSource = this.Deals;
frm.StartPosition = FormStartPosition.Manual; //NEW CODE
frm.Show(this);
}
Run Code Online (Sandbox Code Playgroud)
小智 6
您可以通过在Form_Load事件中调用this.CenterToParent()(实际知道父级)来执行此操作.不要在构造函数中调用它,因为它在调用Show(form)时设置了它.
private void myForm_Load(object sender, EventArgs e)
{
CenterToParent();
}
Run Code Online (Sandbox Code Playgroud)
我知道这个帖子已经老了但是很容易回答,所以希望能帮助遇到它的人找到简单的解决方案.
为了在 .Show 调用中以 parent 为中心,这就是我必须做的:
childForm.Location = new Point(
(parentForm.Location.X + parentForm.Width / 2) - (childForm.Width / 2),
(parentForm.Location.Y + parentForm.Height / 2) - (childForm.Height / 2));
childForm.StartPosition = FormStartPosition.Manual;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
49867 次 |
最近记录: |