如何设置的Windows窗体中StartPosition形式使用code? [英] How do you set the StartPosition of a Windows Forms form using code?
问题描述
有没有一种方法来设置的Windows窗体中StartPosition形式使用code?看来无论我尝试导致StartPostion是默认值。
下面是我在做什么的形式来显示:
公共DealsForm()
{
的InitializeComponent();
this.StartPosition = FormStartPosition.CenterParent;
}
下面是我在做什么,显示的格式为:
私人无效nvShowDeals_LinkClicked(对象发件人,LinkLabelLinkClickedEventArgs E)
{
DealsForm FRM =新DealsForm();
frm.DataSource = this.Deals;
frm.Show(本);
}
我试图把在上述各方法以下,但没有成功:
this.StartPosition = FormStartPosition.CenterParent;
如果我通过属性编辑器设置它...它完美的作品,但我的真的想通过code做到这一点。
应该是一个没有脑子,而是为了我的生活中,我似乎无法推测出来......也许我需要更多的咖啡因。
更新:
如果我做了的ShowDialog()
,并通过它的工作原理父......但我真的不希望将其显示为一个对话框。
如果我做的ShowDialog(),并传递 父母它的工作原理......但我真的不 要显示它作为一个对话框。
这是正确的,因为ShowDialog的将设置frm.Parent == nvShowDeals.Parent
由于使用.Show(),然后frm.Parent == NULL从而FormStartPosition.CenterParent被忽略。
因此,要完成这个功能我会做如下修改:
公共DealsForm()
{
的InitializeComponent();
//this.StartPosition = FormStartPosition.CenterParent;
}
// DealsForm_Load事件
私人无效DealsForm_Load(对象发件人,EventArgs的)
{
this.Location = this.Owner.Location; //新的code
}
在这里,我将做如下修改:
私人无效nvShowDeals_LinkClicked(对象发件人,LinkLabelLinkClickedEventArgs E)
{
DealsForm FRM =新DealsForm();
frm.DataSource = this.Deals;
frm.StartPosition = FormStartPosition.Manual; //新的code
frm.Show(本);
}
Is there a way to set the StartPosition of a Windows Forms form using code? It seems whatever I try results in the StartPostion being the default.
Here is what I am doing in the form to display:
public DealsForm()
{
InitializeComponent();
this.StartPosition = FormStartPosition.CenterParent;
}
Here is what I am doing to display the form:
private void nvShowDeals_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
DealsForm frm = new DealsForm();
frm.DataSource = this.Deals;
frm.Show(this);
}
I have tried putting the following in each of the above methods, to no avail:
this.StartPosition = FormStartPosition.CenterParent;
If I set it via the Property Editor ... it works perfectly, but I would really like to do it via code.
Should be a no-brainer ... but for the life of me I can't seem to figure it out ... maybe I need more caffeine.
Update:
If I do a ShowDialog()
and pass the parent it works ... but I really don't want to show it as a Dialog.
If I do a ShowDialog() and pass the parent it works ... but I really don't want to show it as a Dialog.
That is correct since ShowDialog would set frm.Parent == nvShowDeals.Parent
Since you are using .Show() then frm.Parent == null thus FormStartPosition.CenterParent is ignored.
So to accomplish this function I would make the following changes:
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
}
And Here I would make the following changes:
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);
}
这篇关于如何设置的Windows窗体中StartPosition形式使用code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!