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

C#子窗体
public partial class mainForm1 : Form
  {
  public mainForm1()
  {
  InitializeComponent();
  }
  private void gy_Click(object sender, EventArgs e)
  {
  Help gy =new Help();//创建子窗体;
  gy.MdiParent = this;//指定当前窗体为MDI父窗体;
  gy.Show();//打开窗体
 
  }
  private void mainForm1_Load(object sender, EventArgs e)
  {

  }
  }
打开两次会出现两个同样的对话框,我想只出现一个对话框,请问如何实现?

------解决方案--------------------
public partial class mainForm1 : Form
{
public mainForm1()
{
InitializeComponent();
}
 Help gy=null;
private void gy_Click(object sender, EventArgs e)
{
if(gy==null)
{
Help gy =new Help();//创建子窗体;
gy.MdiParent = this;//指定当前窗体为MDI父窗体;
gy.Show();//打开窗体
}
else
{
gy.Show();//打开窗体
}

  
}
private void mainForm1_Load(object sender, EventArgs e)
{

}
}

------解决方案--------------------
C# code

Help gy ;
private void gy_Click(object sender, EventArgs e)
{
    if(gy == null || gy.IsDisposed)  
    {
       gy =new Help();//创建子窗体;
        gy.MdiParent = this;//指定当前窗体为MDI父窗体;
        gy.Show();//打开窗体
    }
    else
    {
        gy.Active();
    }
}

------解决方案--------------------
探讨

public partial class mainForm1 : Form
{
public mainForm1()
{
InitializeComponent();
}
Help gy=null;
private void gy_Click(object sender, EventArgs e)
{
if(gy==null)
{
……

------解决方案--------------------
public partial class mainForm1 : Form
{

public static Help gy;
public mainForm1()
{
InitializeComponent();
}
private void gy_Click(object sender, EventArgs e)
{
if(gy==null)
{
gy =new Help();//创建子窗体;
gy.MdiParent = this;//指定当前窗体为MDI父窗体;
gy.Show();//打开窗体
}
}
------解决方案--------------------
记个标志,每次打开时能知道上次有没有打开就行.
------解决方案--------------------
C# code
public partial class mainForm1 : Form
  {
  public mainForm1()
  {
  InitializeComponent();
  }
 Help gy=null;
  private void gy_Click(object sender, EventArgs e)
  {
  if(gy==null)
  {
  gy =new Help();//创建子窗体;
  gy.MdiParent = this;//指定当前窗体为MDI父窗体;
  gy.Show();//打开窗体
  }
  else
  {
  gy.Show();//打开窗体
  }

   
  }
  private void mainForm1_Load(object sender, EventArgs e)
  {

  }
  }

------解决方案--------------------
C# code

      //判断子窗体是否打开
      foreach (Form frm in Application.OpenForms)
            {
                if (frm.Name == "gy")
                {
                    isOpened = true;
                    frm.Activate();
                    break;
                }
            }

------解决方案--------------------
最简单做法
C# code
private void gy_Click(object sender, EventArgs e)
{
    foreach (Form item in this.MdiChildren)
    {
        Help hf = item as Help;
        if (hf != null)
        {
            hf.Activate();
            return;
        }
    }

    Help gy = new Help();//创建子窗体;
    gy.MdiParent = this;//指定当前窗体为MDI父窗体;
    gy.Show();//打开窗体
}

------解决方案--------------------