C#:用A窗口新建B窗口,关掉A窗口,B窗口也关闭???? A窗口内:A a=new A();a.show(); //此时的a即为我想要的B窗口在A窗口里,我新建B窗口,B窗口是跟A窗口一样的。当我关闭A窗口时,B窗口也被迫关闭了。如何才能做到当我关闭A时,B不受影响????感谢了!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 或者捕获窗体的关闭动作,使为隐藏即可。如: protected override void WndProc(ref Message m) { const int WM_SYSCOMMAND = 0x0112; const int SC_CLOSE = 0xF060; if (m.Msg == WM_SYSCOMMAND && (int)m.WParam == SC_CLOSE) { this.Visible = false; return; } base.WndProc(ref m); } 比较简单的方法,就是修改Program.cs文件。代码如下: static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); //Application.Run(new Form1());//注释这行,开启Form1窗口后,在Form1窗口打开Form2窗口,然后this.close();Form1窗口关闭,Form2窗口无影响 Form1 form1 = new Form1(); form1.Show(); Application.Run(); } } 因为你的B窗口是在A中生成.关闭A也会销毁B的... 方法一:你的B窗口是在主窗口中生成的,在A窗口建立时把B传递进去...方法二:你在关闭A时,在关闭事件中使它隐藏,不关闭,,也可以达到同样的效果.. A为主窗体,B、C为子窗体。在A中写打开子窗体的事件。在A中调用事件打开B窗体,在B中调用A的事件来打开C窗体。 4楼的方法挺好用的!!但是唯一的缺点是,当我关闭完A窗口和所有由A衍生出来的窗口时,发现A还存在进程里,没有被结束,但是此时我已经将所有的窗口给关闭了! 退出的时候就需要使用Application.ExitThread()方法了Application.ExitThread 方法退出当前线程上的消息循环,并关闭该线程上的所有窗口所以我在2楼又给你提供了一个隐藏窗体的方法,这种方法比较简单,不会因为编程不小心造成遗留问题 其实4楼的方法主要用在登录窗体中。情形如:如果未登录,打开登录窗体;如果登录成功,关闭登录窗体并打开程序主窗体。简单示例代码:Program.csusing System;using System.Collections.Generic;using System.Windows.Forms;namespace stserver{ static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); login loginfrm=new login(); loginfrm.ShowDialog(); if (loginfrm.islogin == true) { loginfrm.Close(); Application.Run(new stserver()); } else { loginfrm.ShowDialog(); } } }}登录窗体login.csusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Data.Sql;using System.Data.SqlClient;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Xml;namespace stserver{ public partial class login : Form { public login() { InitializeComponent(); } public bool islogin = false; private void btn_submit_Click(object sender, EventArgs e) { try { XmlDocument doc = new XmlDocument(); doc.Load(Application.StartupPath + "\\config.xml"); string connstr = doc.DocumentElement.SelectSingleNode("serverconnstr").InnerText; string selectstr = "select top 1 id from admin where uid='" + txb_uid.Text + "' and pwd='" + txb_pwd.Text + "'"; SqlConnection conn = new SqlConnection(connstr); conn.Open(); SqlCommand cmd = new SqlCommand(selectstr, conn); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { islogin = true; dr.Close(); conn.Close(); conn.Dispose(); MessageBox.Show("您好," + txb_pwd.Text + "!,欢迎使用\n\n祝您工作愉快!", "欢迎", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Close(); } else { dr.Close(); conn.Close(); conn.Dispose(); MessageBox.Show("登录失败!\n请检查您的登录用户名和密码是否正确。\n\n如果您忘记了自己的密码请联系技术部"); txb_uid.Text = ""; txb_pwd.Text = ""; txb_uid.Focus(); } dr.Close(); conn.Close(); conn.Dispose(); } catch { MessageBox.Show("不能连接到服务器,请联系技术部","服务器故障",MessageBoxButtons.OK,MessageBoxIcon.Error); } } private void btn_cancel_Click(object sender, EventArgs e) { Application.ExitThread(); } }}所以,在登录的时候可以用我在1楼提供的方法也就是4楼的实现代码。但是在一般的多窗体程序中,如果要在Program.cs中如此控制太过复杂而且容易出错,所以只在登录验证中使用该方法,在普通窗体中使用我在2楼提供的方法 嗯嗯!你的这种方法我了解,但是我也遇到了相同的问题,就是当我的窗口全部关闭完时,我打开任务管理器,发现A窗口的进程还在,并没有退出来!其实我要实现的效果很简单,跟win7里的便笺一样!而且我发现,win7的便笺无论打开多少个窗口,如果最后一个窗口关闭了,它的进程也就全部退出了! 哦这个功能啊呵呵,也很简单:在关闭窗体的时候判断你写的程序一共开了多少个窗体,如果窗体数目大于1那么执行this.Close()否则执行Application.ExitThread(0) b窗体的声明作成静态变量,或其它不会因a被销毁而会被销毁的变量就行了。public class B: form{ public static B w = null; } winform里的treeview节点内容如何垂直居中? POS收款机 客显 热敏打印机 这个问题有点难? [求助]如何用C#实现定时备份sql数据库啊(急) GPS卫星定位系统 谈论下C#中析构函数的作用,顶者有分! 关于IE6中使用smsx.cab打印的问题! 请问[运行]对话框是系统中的一个文件吗?怎么在程序中打开 使用API GetClassName遇到麻烦,请高手指点一下(在线等待) ado.net关于access问题!! winform中contextmenustript如何动态添加子菜单 新人求助,有关数据库引用问题
protected override void WndProc(ref Message m)
{
const int WM_SYSCOMMAND = 0x0112;
const int SC_CLOSE = 0xF060;
if (m.Msg == WM_SYSCOMMAND && (int)m.WParam == SC_CLOSE)
{
this.Visible = false;
return;
}
base.WndProc(ref m);
}
代码如下:
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//Application.Run(new Form1());//注释这行,开启Form1窗口后,在Form1窗口打开Form2窗口,然后this.close();Form1窗口关闭,Form2窗口无影响
Form1 form1 = new Form1();
form1.Show();
Application.Run();
}
}
4楼的方法挺好用的!!但是唯一的缺点是,当我关闭完A窗口和所有由A衍生出来的窗口时,发现A还存在进程里,没有被结束,但是此时我已经将所有的窗口给关闭了!
退出的时候就需要使用Application.ExitThread()方法了Application.ExitThread 方法
退出当前线程上的消息循环,并关闭该线程上的所有窗口
所以我在2楼又给你提供了一个隐藏窗体的方法,这种方法比较简单,不会因为编程不小心造成遗留问题
Program.csusing System;
using System.Collections.Generic;
using System.Windows.Forms;namespace stserver
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
login loginfrm=new login();
loginfrm.ShowDialog();
if (loginfrm.islogin == true)
{
loginfrm.Close();
Application.Run(new stserver());
}
else
{
loginfrm.ShowDialog();
}
}
}
}
登录窗体login.csusing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;namespace stserver
{
public partial class login : Form
{
public login()
{
InitializeComponent();
}
public bool islogin = false; private void btn_submit_Click(object sender, EventArgs e)
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(Application.StartupPath + "\\config.xml");
string connstr = doc.DocumentElement.SelectSingleNode("serverconnstr").InnerText;
string selectstr = "select top 1 id from admin where uid='" + txb_uid.Text + "' and pwd='" + txb_pwd.Text + "'";
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
SqlCommand cmd = new SqlCommand(selectstr, conn);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
islogin = true;
dr.Close();
conn.Close();
conn.Dispose();
MessageBox.Show("您好," + txb_pwd.Text + "!,欢迎使用\n\n祝您工作愉快!", "欢迎", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Close();
}
else
{
dr.Close();
conn.Close();
conn.Dispose();
MessageBox.Show("登录失败!\n请检查您的登录用户名和密码是否正确。\n\n如果您忘记了自己的密码请联系技术部");
txb_uid.Text = "";
txb_pwd.Text = "";
txb_uid.Focus();
}
dr.Close();
conn.Close();
conn.Dispose();
}
catch
{
MessageBox.Show("不能连接到服务器,请联系技术部","服务器故障",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
} private void btn_cancel_Click(object sender, EventArgs e)
{
Application.ExitThread();
}
}
}所以,在登录的时候可以用我在1楼提供的方法也就是4楼的实现代码。但是在一般的多窗体程序中,如果要在Program.cs中如此控制太过复杂而且容易出错,所以只在登录验证中使用该方法,在普通窗体中使用我在2楼提供的方法
嗯嗯!你的这种方法我了解,但是我也遇到了相同的问题,就是当我的窗口全部关闭完时,我打开任务管理器,发现A窗口的进程还在,并没有退出来!
其实我要实现的效果很简单,跟win7里的便笺一样!
而且我发现,win7的便笺无论打开多少个窗口,如果最后一个窗口关闭了,它的进程也就全部退出了!
哦这个功能啊呵呵,也很简单:在关闭窗体的时候判断你写的程序一共开了多少个窗体,如果窗体数目大于1那么执行this.Close()否则执行Application.ExitThread(0)
public class B: form{ public static B w = null;
}