【问题标题】:Unable to show a form as a mdiChild form using c#无法使用 c# 将表单显示为 mdiChild 表单
【发布时间】:2020-04-05 15:24:08
【问题描述】:

我正在构建一个 C#(VS 2015) 桌面应用程序。现在这个应用程序有几种形式,其中 'frmMain' 的 IsMdiContainer=true,其余的都是子形式。现在,在其中一个子窗体“frmChild1”中,在按钮单击事件下具有以下代码。

if(chkMarksEntrdOrNot(tsDDBSession.ToString(), tsDDBClass.ToString()) == false)
{

}
else
{
   MessageBox.Show("Marks of all the applicants for the chosen Session and Class have not been entered yet. Please enter marks then come again to this menu.",Application.ProductName,MessageBoxButtons.OK);
   this.Close();
   Admission.frmAdmissionTestMarksEntryBrows fATMEB = new frmAdmissionTestMarksEntryBrows();
   //frmMain fMn = new frmMain();
   //fATMEB.MdiParent = fMn;
   fATMEB.Show();
}

从上面的代码可以看出,在 else 部分,我正在关闭当前表单,然后尝试显示另一个子表单(frmAdmissionTestMarksEntryBrows)。该表单正在显示,但不是 mdiChild 表单。我想将其显示为 mdiChild 表单。我希望你能理解我的问题。请帮忙。

【问题讨论】:

  • 问问自己..这个表单怎么知道它应该是frmMain的MDI子表单?
  • 为什么注释掉://fATMEB.MdiParent = fMn;?如果取消注释会怎样?
  • 哦..您需要对主窗体的实际引用,而不是尝试创建新实例。你有这个参考吗?例如,如果您尝试将此新表单作为 mdiChild 添加到您自己的父级,请尝试:fATMEB.MdiParent = this.MdiParent;(在关闭自己之前)
  • 感谢@Oguz Ozgul,您的解决方案非常适合我
  • 然后添加为 sloution。请接受。保持安全。

标签: c# visual-studio-2015 desktop-application


【解决方案1】:
//frmMain fMn = new frmMain();
//fATMEB.MdiParent = fMn;

在显示之前您没有设置表单的 MdiParent。

您需要对主窗体frmMain 的实际引用(因为从上面的 cmets 中,我们了解到 frmMain 是所需的父级),并且不应尝试创建新实例。

如果您尝试将此新表单作为 mdichild 添加到您自己的父级,请尝试:

fATMEB.MdiParent = this.MdiParent;

(关闭自己之前)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多