日期:2014-05-19  浏览次数:20864 次

如何让判断某个窗体已显示?
点击某个按钮,显示一个窗体;如果已经显示,就不做任何操作了.如何实现?

------解决方案--------------------
可以不可以判断进程存在呢?。。。。。
------解决方案--------------------
Form2 f = new Form2 ();
private void button1_Click(object sender, System.EventArgs e)
{
if (!f.Created)
{
f= new Form2 ();
f.Show ();
}
else
MessageBox.Show ( "窗体已创建 ");
}
------解决方案--------------------
private Form_Closing(...)
{
e.Cancel = true;
}
------解决方案--------------------
********下次一次把问题说清楚!!!!!!

private void notifyIcon_DoubleClick(object sender, System.EventArgs e)
{
if (this.WindowState != FormWindowState.Minimized)
{
this.WindowState = FormWindowState.Minimized;
this.Visible = false;
}
else
{
this.Show ();
this.WindowState = FormWindowState.Normal;
this.Activate ();
}
}

------解决方案--------------------
private bool checkChildFrmExist(string childFrmName)
{
foreach (Form childFrm in this.MdiChildren)
{
//用子窗体的Name进行判断,如果已经存在则将他激活
if (childFrm.Name == childFrmName)
{
if (childFrm.WindowState == FormWindowState.Minimized)
childFrm.WindowState = FormWindowState.Normal;
childFrm.Activate();
return true;
}
}
return false;
}
------解决方案--------------------
/// <summary>
/// 保证每个MDI子窗体只创建一个实例,若该窗体已隐藏,则将其show出来,不再另行创建实例
/// Modified on Oct. 18th,2004
/// </summary>
public bool AnotherInstance(System.Windows.Forms.Form Main, string Type)
{
try
{
for (int i = 0; i < Main.MdiChildren.Length; i++)
{
if (Main.MdiChildren[i].GetType().ToString() == Type)
{
if (Main.MdiChildren[i].Visible == false)
Main.MdiChildren[i].Visible = true;
//2005/03/23 cc modified begin
Main.MdiChildren[i].Activate();
//2005/03/23 cc modified end
return true;
}
}
return false;
}
catch (System.Exception err)
{
throw new Exception(err.Message.ToString());
}
}
------解决方案--------------------
我觉得楼住说的还比较模糊,我的理解是楼住在按钮click中创建了一个Form实例,他想再次点击按钮时,如果上一次点击产生的窗口还在显示的话就不再创建新的Form实例了。
如果是我这种理解的话,解决办法如下:
定义一个static int成员InstanceCount(初始化为0),创建新实例之前判断该值是否小于1,如果下于1就创建,在创建新实例之后InstanceCount增加1,否则退出。
需要注意的是在FormClosing的时候要判断InstanceCount是否大于1,是则减1。

Class NewForm{
public static int InstanceCount=0;
.........
private Form_Closing(...)
{
if(NewForm.InstanceCount> =1)
NewForm.InstanceCount-=1;
}

}
private void ..Click(object sender, System.EventArgs e)
{
if(NewForm.InstanceCount <1)
{
NewForm myFrm=new NewForm();