在 MDI Child 的中心显示对话框
Display dialog box in center of MDI Child
免责声明:我知道已经为类似问题提供了答案,但这些答案似乎对我不起作用。
我有一个使用带有 MDIClient 的主窗体的应用程序;我想显示一个允许用户输入值的对话框;此对话框将显示在调用该对话框的 MDIChild 窗体的中心。
我已经看过以下解决方案:
C# - Show Dialog box at center of its parent
但是,除非与我的解决方案存在与应用程序相关的差异,否则这似乎存在一些基本问题。
建议通过以下方式实现:
private void OpenForm(Form parent)
{
FormLoading frm = new FormLoading();
frm.Parent = parent;
frm.StartPosition = FormStartPosition.CenterParent;
frm.ShowDialog();
}
然而,这有以下问题:
当我尝试实现它时,在单步执行代码时,一旦它到达设置表单父级的行,就会发生以下异常:
Top-level control cannot be added to a control.
N.B。除非所有表单都使用 true
的 TopLevel 值进行初始化,否则这个值似乎没有在任何地方设置!
好的,所以;我们将 TopLevel 设置为 false
以允许将父表单设置为对话框的父表单。假设我这样做,当它到达 ShowDialog()
:
行时
Form that is not a top-level form cannot be displayed as a modal dialog box. Remove the form from any parent form before calling showDialog.
这就是我的泥潭;对话框表单似乎不需要是 TopLevel 表单才能拥有 Parent 但同时需要是 TopLevel 表单以便它可以显示为对话框...
最后一点,我认为我不必将我想要的表单的 'StartPosition' 设置为对话框,因为这已经在表单的 InitializeComponent()
部分中设置了;尽管如此,我已经尝试在函数中明确设置它,但没有任何区别。
您可以手动定位对话框:
private void invokeDialogButton_Click(object sender, EventArgs e)
{
var dialogForm = new DialogForm();
dialogForm.StartPosition = FormStartPosition.Manual;
//Get the actual position of the MDI Parent form in screen coords
Point screenLocation = Parent.PointToScreen(Parent.Location);
//Adjust for position of the MDI Child form in screen coords
screenLocation.X += Location.X;
screenLocation.Y += Location.Y;
dialogForm.Location = new Point(screenLocation.X + (Width - dialogForm.Width) / 2,
screenLocation.Y + (Height - dialogForm.Height) / 2);
dialogForm.ShowDialog(this);
}
看看我的 Github 页面上的这个 working example project(Visual Studio 2015 社区版项目)。
免责声明:我知道已经为类似问题提供了答案,但这些答案似乎对我不起作用。
我有一个使用带有 MDIClient 的主窗体的应用程序;我想显示一个允许用户输入值的对话框;此对话框将显示在调用该对话框的 MDIChild 窗体的中心。
我已经看过以下解决方案:
C# - Show Dialog box at center of its parent
但是,除非与我的解决方案存在与应用程序相关的差异,否则这似乎存在一些基本问题。
建议通过以下方式实现:
private void OpenForm(Form parent)
{
FormLoading frm = new FormLoading();
frm.Parent = parent;
frm.StartPosition = FormStartPosition.CenterParent;
frm.ShowDialog();
}
然而,这有以下问题:
当我尝试实现它时,在单步执行代码时,一旦它到达设置表单父级的行,就会发生以下异常:
Top-level control cannot be added to a control.
N.B。除非所有表单都使用 true
的 TopLevel 值进行初始化,否则这个值似乎没有在任何地方设置!
好的,所以;我们将 TopLevel 设置为 false
以允许将父表单设置为对话框的父表单。假设我这样做,当它到达 ShowDialog()
:
Form that is not a top-level form cannot be displayed as a modal dialog box. Remove the form from any parent form before calling showDialog.
这就是我的泥潭;对话框表单似乎不需要是 TopLevel 表单才能拥有 Parent 但同时需要是 TopLevel 表单以便它可以显示为对话框...
最后一点,我认为我不必将我想要的表单的 'StartPosition' 设置为对话框,因为这已经在表单的 InitializeComponent()
部分中设置了;尽管如此,我已经尝试在函数中明确设置它,但没有任何区别。
您可以手动定位对话框:
private void invokeDialogButton_Click(object sender, EventArgs e)
{
var dialogForm = new DialogForm();
dialogForm.StartPosition = FormStartPosition.Manual;
//Get the actual position of the MDI Parent form in screen coords
Point screenLocation = Parent.PointToScreen(Parent.Location);
//Adjust for position of the MDI Child form in screen coords
screenLocation.X += Location.X;
screenLocation.Y += Location.Y;
dialogForm.Location = new Point(screenLocation.X + (Width - dialogForm.Width) / 2,
screenLocation.Y + (Height - dialogForm.Height) / 2);
dialogForm.ShowDialog(this);
}
看看我的 Github 页面上的这个 working example project(Visual Studio 2015 社区版项目)。