日期:2014-05-18  浏览次数:21081 次

如何关闭已打开的子窗体?
我想完成以下的功能:
有一个父窗体frmMain,两个子窗体frmS,frmW.程序启动时frmS以在父窗体中以最大化的形式显示。我想在父窗体中加一个BUTTON1,当击这个BUTTON1后frmS关闭,frmW显示;然后在frmW中建一个BUTTON2,点这个按钮后,frmW退出,frmW重新载入!
这个具体怎么实现呀?能不能给出关键的代码和思路?

------解决方案--------------------
button1.click

foreach (Form childForm in MdiChildren)
{
if(childForm.Name=="frmS")
{
childForm.Close();
frmW f=new frmW();
 f.MdiParent = this;
f.Show();
}
}



然后在frmW中建一个BUTTON2,点这个按钮后,frmW退出,frmW重新载入! ???
关了从新载入????

------解决方案--------------------
frmW退出,frmW重新载入!????

是不是frmS重新载入啊,写错了吧!
------解决方案--------------------
C# code

/* -----------frmMian----------- */
private frmS fs = null;
private frmW fw = null;

private void button1_Click(object sender, EventArgs e)
{
   frmS f = new frmS();
   fs = f;
   f.Show();
   frmW ff = new frmW();
   fw = ff;
   ff.Show();
}

private void button3_Click(object sender, EventArgs e)
{
   if (fs != null)
   {
      fs.Close();
      fs = null;
   }
}

/* --------frmW------- */
private frmS fs = null;

private void button1_Click(object sender, EventArgs e)
{
  frmS f = new frmS();
  fs = f;
  f.Show();
  this.Close();
}

------解决方案--------------------
private void frmMain_Load(object sender, EventArgs e)
{
frmS fs = new frmS();
fs.MaximizeBox = true;
fs.MdiParent = this;
fs.Show();
}
 private void button1_Click(object sender, EventArgs e)
{
this.Close();
frmW fw = new frmW();
fw.MdiParent = this.MdiParent ;
fw.Show();
 }
private void button2_Click(object sender, EventArgs e)
{
this.Close();
frmS fs = new frmS();
fs.MdiParent = this.MdiParent ;
fs.Show();
 }
------解决方案--------------------

我的这个可以通过,而且可以防止打开多个窗体,如果已经打开,就激活他
C# code
public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void 登录ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form form in Application.OpenForms)
            {
                if (form.Name == "frmLogin")
                {
                    form.Activate();
                    form.WindowState = FormWindowState.Maximized;
                    return;
                }
            }
            
            frmLogin login = new frmLogin();

            login.MdiParent = this;
            login.WindowState = FormWindowState.Maximized;
            login.Show();
        }

        private void 日志ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form form in Application.OpenForms)
            {
                if (form.Text == "frmLogInfo")
                {
                    form.Activate();
                    return;
                }
            }
            
            frmLogInfo log = new frmLogInfo();
            log.MdiParent = this;
            log.WindowState = FormWindowState.Maximized;
            log.Show();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            frmLogin login = new frmLogin();
            login.WindowState = FormWindowState.Maximized;
            login.MdiParent = this;
            login.Show();
        }

    }