避免重复弹出窗体
主窗体有若干按钮,每个按钮可以弹出某个窗体类的一个实例,要求按按钮时如果有这个窗体类的实例就最大化这个实例,如果没有这个窗体类的实例则新建一个实例并最大化 问题点数:20、回复次数:5Top
1 楼maxxxz(ma)回复于 2005-06-20 12:58:38 得分 5
Private Shared instance As form1
''singleton模式
Public Shared Function getInstance() As form1
If instance Is Nothing Then
instance = New ShouZhi
End If
If instance.IsDisposed Then
instance = New ShouZhi
End If
Return instance
End Function
vb写的。一看就懂~Top
2 楼funsuzhou(☆【处变不惊】☆)回复于 2005-06-20 13:13:45 得分 0
怎么对所有的实例进行比较?
用C#Top
3 楼cobrastudio(老汉我)回复于 2005-06-20 13:27:24 得分 5
回复人: maxxxz(ma) ( ) 信誉:100 2005-06-20 12:58:00 得分: 0
Private Shared instance As form1
''singleton模式
Public Shared Function getInstance() As form1
If instance Is Nothing Then
instance = New ShouZhi
End If
If instance.IsDisposed Then
instance = New ShouZhi
End If
Return instance
End Function
vb写的。一看就懂~
===
在窗体的中都实现getInstance()方法?
Top
4 楼funsuzhou(☆【处变不惊】☆)回复于 2005-06-20 13:29:20 得分 0
我不要VB代码;
应用程序运行时有可能有很多打开的窗体,怎么对所有这些实例一一比较?Top
5 楼wwy830916(问题多如牛毛)回复于 2005-06-20 14:46:21 得分 10
(1)
private Form2 frm2;
private Form2 GetFrm2()
{
if(frm2 == null)
{
frm2 = new Form2();
}
if(frm2.IsDisposed)
{
frm2= new Form2();
}
return frm2;
}
private void button1_Click(object sender, System.EventArgs e)
{
GetFrm().Show();
frm2.Focus();
frm2.WindowState = FormWindowState.Maximized;
}
(2)
或者在Form2中:
public static bool IsOpen = false;
protected override void Dispose( bool disposing )
{
IsOpen = false;
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void Form2_Load(object sender, System.EventArgs e)
{
IsOpen = true;
}
Form1中:
private void button1_Click(object sender, System.EventArgs e)
{
if(!Form2.IsOpen)
{
frm2 = new Form2();
frm2.Show();
frm2.WindowState = FormWindowState.Maximized;
}
else
{
frm2.Focus();
frm2.WindowState = FormWindowState.Maximized;
}
}
(3)
再者就用API(一时忘了哪个API)Top




