如何只打开一个窗体实例!非MDI. 有两个窗体,第一个窗体上有一个按钮,点击后会打开第二个窗体,但再点击时又会再打开一个,请问如何才能让多次点击只能打开一个实例?非MDI窗体.请给出详细代码.谢了. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在函数体外Form2 f2=null;在按钮里面事件写if(f2!=null){f2=new Form2();f2.Show();} private Form2 f2=new Form2();private void button1_Click(object sender, System.EventArgs e){ this.f2.Show();} 同意二楼得,没有必要每次都NEW得 我XXX.大家能不能说清楚一点,我不懂 用线程互斥:System.Threading.Mutex mutex=new System.Threading.Mutex(true,"窗体名称",out flag); if(!flag) { MessageBox.Show("本机已有一个n程序在运行!","启动程序出错提示"); System.Threading.Thread.Sleep(5000);//线程挂起5秒钟 Environment.Exit(1);//退出程序 } ******************* 防止程序多次执行 ************************** public abstract class OneInstance { /// <summary> /// 用来判断一个指定的程序是否正在运行 /// </summary> /// <param name="appId">程序名称,长一点比较好,防止有重复</param> /// <returns>如果程序是第一次运行返回True,否则返回False</returns> public static bool IsFirst(string appId) { bool ret=false; if(OpenMutex(0x1F0001,0,appId)==IntPtr.Zero) { CreateMutex(IntPtr.Zero,0,appId); ret=true; } return ret; } [DllImport("Kernel32.dll",CharSet=CharSet.Auto)] private static extern IntPtr OpenMutex( uint dwDesiredAccess, // access int bInheritHandle, // inheritance option string lpName // object name ); [DllImport("Kernel32.dll",CharSet=CharSet.Auto)] private static extern IntPtr CreateMutex( IntPtr lpMutexAttributes, // SD int bInitialOwner, // initial owner string lpName // object name ); } static void Main() { //防止程序多次运行 if(!OneInstance.IsFirst("你的程序名称")) { MessageBox.Show ("你的程序已经在运行中!!"); return; } Application.Run(new Form1()); } 一个最简单实用的在函数体外Form2 f2;在按钮里面事件写if(f2!=null || f2.IsDisposed){f2=new Form2();f2.Show();}else{f2.Activate()} sington模式好像可以,但我还是不很了解 Form2 about = new Form2 ();about.ShowDialog(); 是这个吧?if(f2==null){f2=new Form2();f2.Show();} C# 预加载 各位大侠,急急急啊!C#串口通信接发程序 对属性里边的value的理解 有没有人知道,这种是什么字体? 怎樣在Report services 中繪制柏拉圖? 第六次散分了~,爽了一把 请问如何使用installshield打包msde? 请教,怎样把dataGrid的数据赋值给label 关于序列化的一个匪夷所思的问题 求一本学习C#的非扫描版经典书籍和一本任何版本的C# windows编程学习书籍 如何用c#附加数据库 求救:李洪根老师的.NET平台下WEB应用程序的部署(安装数据库和自动配置)问题。
Form2 f2=null;
在按钮里面事件写
if(f2!=null)
{
f2=new Form2();
f2.Show();
}
private void button1_Click(object sender, System.EventArgs e)
{
this.f2.Show();
}
大家能不能说清楚一点,我不懂
System.Threading.Mutex mutex=new System.Threading.Mutex(true,"窗体名称",out flag);
if(!flag)
{ MessageBox.Show("本机已有一个n程序在运行!","启动程序出错提示");
System.Threading.Thread.Sleep(5000);//线程挂起5秒钟
Environment.Exit(1);//退出程序
}
public abstract class OneInstance
{
/// <summary>
/// 用来判断一个指定的程序是否正在运行
/// </summary>
/// <param name="appId">程序名称,长一点比较好,防止有重复</param>
/// <returns>如果程序是第一次运行返回True,否则返回False</returns>
public static bool IsFirst(string appId)
{
bool ret=false;
if(OpenMutex(0x1F0001,0,appId)==IntPtr.Zero)
{
CreateMutex(IntPtr.Zero,0,appId);
ret=true;
}
return ret;
} [DllImport("Kernel32.dll",CharSet=CharSet.Auto)]
private static extern IntPtr OpenMutex(
uint dwDesiredAccess, // access
int bInheritHandle, // inheritance option
string lpName // object name
); [DllImport("Kernel32.dll",CharSet=CharSet.Auto)]
private static extern IntPtr CreateMutex(
IntPtr lpMutexAttributes, // SD
int bInitialOwner, // initial owner
string lpName // object name
);
}
static void Main()
{
//防止程序多次运行
if(!OneInstance.IsFirst("你的程序名称"))
{
MessageBox.Show ("你的程序已经在运行中!!");
return;
} Application.Run(new Form1());
}
在函数体外
Form2 f2;
在按钮里面事件写
if(f2!=null || f2.IsDisposed)
{
f2=new Form2();
f2.Show();
}
else
{
f2.Activate()
}
about.ShowDialog();
if(f2==null)
{
f2=new Form2();
f2.Show();
}