请问如何在主窗体中打开另一个子窗体?并且不可以重复打开!!!合理的好分!!!

时间:2022-08-30 22:53:40
请问如何在主窗体中打开另一个子窗体?并且不可以重复打开!!!合理的好分!!!

15 个解决方案

#1


我就借花献佛了  :)
class A
{
public static void Main()
{
B b=B.getB ();
}
}

class B
{
private static B x;
private B()
{
}
public static B getB()
{
if(x==null)
{
x=new B();
return x;
}
return x;
}
}
   下面来看一下,这个程序,class A就不用说了,一看就知道,主要是class B,我们把B的构造函数设计成private,而不是以前经常用的public,这正在问题的关键所在,如果我们用B b=new B()这样做的话,肯定不会成功,因为private只允许在类的内部调用,在外部是不行的。然后,由于我们已经不可能直接用new产生一个实例,那么只好把getB()设成static的啦,另外,还有一个static的x,如果x为空,则表示没有实例在,建一个吧,然后再访回给调用者。如果x不为空,说明有一个了,不行,只能有一个,好,我还给你给我的这个。

#2


照你的程序来看,要在每一个子窗体里增加 B 了?
那是不是太那个了?

#3


大家能不能给我一个比较完整的函数?谢啦。

#4


你可真行,那个B就是你的字窗体的类啊

#5


不好意思,我是刚学的。希望你能稍稍说明白点。

#6


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace My1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Button button1;

                  //这里很重要 
public static Form2 f=null;

/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.listBox1 = new System.Windows.Forms.ListBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
// 
// listBox1
// 
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(216, 112);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(216, 148);
this.listBox1.TabIndex = 0;
// 
// button1
// 
this.button1.Location = new System.Drawing.Point(224, 40);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(136, 24);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(536, 325);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button1,
this.listBox1});
this.IsMdiContainer = true;
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
if (f==null)
{
f=new Form2();
}
f.MdiParent=this;
f.Show();
}
}
}
下面是form2中的代码
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace My1
{
/// <summary>
/// Form2 的摘要说明。
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form2()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
// 
// Form2
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "Form2";
this.Text = "Form2";
this.Closed += new System.EventHandler(this.Form2_Closed);

}
#endregion

private void Form2_Closed(object sender, System.EventArgs e)
{
                          // 这里也重要  
Form1.f=null;
}
}
}

#7


你只要在调用子窗体的构造函数之前,先判断是否有改类的实例存在,如果存在就把实例的值赋给它,因为是引用值,所以只有一个实例。
还有,在关闭该子窗体的时候,一定要把那个静态变量释放掉,即:B.x.Dispose();

#8


这是我的做法:
class Form1
{
   private Form2 f2;
   private void button2_Click(object sender, System.EventArgs e)
     {
try 
{
    f2.Show();
}
catch 
{
    f2=new NoModel();
    f2.Show();
}
}

#9


给每个船体取个名字

public Hashtable forms = new Hashtable();
private void button9_Click(object sender, System.EventArgs e)
{
string name = "MyTest1";
OpenWin(new FormTest(), name);
}
private void OpenWin(Form frm, string name)
{


if (!(forms[name] is   Form))
{
frm.Show();
frm.Name = name;
frm.Closed += new EventHandler(this.frmClosed);
forms.Add(name, frm);


}
private void frmClosed(object send, EventArgs e)
{
forms.Remove(((Form)send).Name);
}

#10


tag

#11


我同意rbjojo的做法
也不知道是不是我们都没有正确理解这老兄的问题

#12


bolar() 的做法不错啊,我都试过了。

#13


using System;
using System.Windows .Forms ;
namespace Jandar.ClientCom
{
/// <summary>
/// CheckSheet 的摘要说明。
/// </summary>
public class CheckSheet
{

public CheckSheet()
{

//
// TODO: 在此处添加构造函数逻辑
//
}

//判断是否已存在MDI子窗口
//1--已存在,-1--不存在
// 类名字符串(包括NameSpace) 如 "SIIS.Form_query"
public  int ChildExist(Form fmMdi,string strType)
{
foreach(Form fm in fmMdi.MdiChildren )
{
if (fm.GetType().ToString()==strType)
{
fm.BringToFront ();
return 1;
}
}
return -1;
}




}
}

#14


klxyz(小康) 用的是设计模式中的singleton方法。

#15


klxyz(小康) 的方法最精练

#1


我就借花献佛了  :)
class A
{
public static void Main()
{
B b=B.getB ();
}
}

class B
{
private static B x;
private B()
{
}
public static B getB()
{
if(x==null)
{
x=new B();
return x;
}
return x;
}
}
   下面来看一下,这个程序,class A就不用说了,一看就知道,主要是class B,我们把B的构造函数设计成private,而不是以前经常用的public,这正在问题的关键所在,如果我们用B b=new B()这样做的话,肯定不会成功,因为private只允许在类的内部调用,在外部是不行的。然后,由于我们已经不可能直接用new产生一个实例,那么只好把getB()设成static的啦,另外,还有一个static的x,如果x为空,则表示没有实例在,建一个吧,然后再访回给调用者。如果x不为空,说明有一个了,不行,只能有一个,好,我还给你给我的这个。

#2


照你的程序来看,要在每一个子窗体里增加 B 了?
那是不是太那个了?

#3


大家能不能给我一个比较完整的函数?谢啦。

#4


你可真行,那个B就是你的字窗体的类啊

#5


不好意思,我是刚学的。希望你能稍稍说明白点。

#6


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace My1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Button button1;

                  //这里很重要 
public static Form2 f=null;

/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.listBox1 = new System.Windows.Forms.ListBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
// 
// listBox1
// 
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(216, 112);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(216, 148);
this.listBox1.TabIndex = 0;
// 
// button1
// 
this.button1.Location = new System.Drawing.Point(224, 40);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(136, 24);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(536, 325);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button1,
this.listBox1});
this.IsMdiContainer = true;
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
if (f==null)
{
f=new Form2();
}
f.MdiParent=this;
f.Show();
}
}
}
下面是form2中的代码
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace My1
{
/// <summary>
/// Form2 的摘要说明。
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form2()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
// 
// Form2
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "Form2";
this.Text = "Form2";
this.Closed += new System.EventHandler(this.Form2_Closed);

}
#endregion

private void Form2_Closed(object sender, System.EventArgs e)
{
                          // 这里也重要  
Form1.f=null;
}
}
}

#7


你只要在调用子窗体的构造函数之前,先判断是否有改类的实例存在,如果存在就把实例的值赋给它,因为是引用值,所以只有一个实例。
还有,在关闭该子窗体的时候,一定要把那个静态变量释放掉,即:B.x.Dispose();

#8


这是我的做法:
class Form1
{
   private Form2 f2;
   private void button2_Click(object sender, System.EventArgs e)
     {
try 
{
    f2.Show();
}
catch 
{
    f2=new NoModel();
    f2.Show();
}
}

#9


给每个船体取个名字

public Hashtable forms = new Hashtable();
private void button9_Click(object sender, System.EventArgs e)
{
string name = "MyTest1";
OpenWin(new FormTest(), name);
}
private void OpenWin(Form frm, string name)
{


if (!(forms[name] is   Form))
{
frm.Show();
frm.Name = name;
frm.Closed += new EventHandler(this.frmClosed);
forms.Add(name, frm);


}
private void frmClosed(object send, EventArgs e)
{
forms.Remove(((Form)send).Name);
}

#10


tag

#11


我同意rbjojo的做法
也不知道是不是我们都没有正确理解这老兄的问题

#12


bolar() 的做法不错啊,我都试过了。

#13


using System;
using System.Windows .Forms ;
namespace Jandar.ClientCom
{
/// <summary>
/// CheckSheet 的摘要说明。
/// </summary>
public class CheckSheet
{

public CheckSheet()
{

//
// TODO: 在此处添加构造函数逻辑
//
}

//判断是否已存在MDI子窗口
//1--已存在,-1--不存在
// 类名字符串(包括NameSpace) 如 "SIIS.Form_query"
public  int ChildExist(Form fmMdi,string strType)
{
foreach(Form fm in fmMdi.MdiChildren )
{
if (fm.GetType().ToString()==strType)
{
fm.BringToFront ();
return 1;
}
}
return -1;
}




}
}

#14


klxyz(小康) 用的是设计模式中的singleton方法。

#15


klxyz(小康) 的方法最精练