思路链:我想关掉我的login窗体,打开main窗体,
于是我在button里加入this.close();
main_form.show();main窗体一闪而过,这个很理解,show()并不让窗体消息循环我就改成
this.close();
Application.Run(new main_form());
也不行然后从别人源代码里看到
this.Hide();
main_form.Show();
却行了,按理应该同样一闪而过啊 ??求解,,,我一定要把this关掉呢,有好点的方法么
于是我在button里加入this.close();
main_form.show();main窗体一闪而过,这个很理解,show()并不让窗体消息循环我就改成
this.close();
Application.Run(new main_form());
也不行然后从别人源代码里看到
this.Hide();
main_form.Show();
却行了,按理应该同样一闪而过啊 ??求解,,,我一定要把this关掉呢,有好点的方法么
解决方案 »
- 解析字符串
- 请问一下哪里哟visual stdio 2005 下载。
- 怎样修改listview表头的高度
- 请教,winform中的dagagrid.DataSource 在什么情况下是DataView和Datatable
- 利用zedgraph控件如何隐藏X轴?
- 高分求教Connection Point的问题
- 使用GDI+画图,如何将图形中的某一部分保存为一个image?
- 怎么样能让页面不总是提示安装adobe flash player
- World Wide Web Publishing 启动不了
- 关于用VS的“发生生成错误,要继续并运行上次的成功生成吗?”
- c#如何实现ping功能?
- winform中如何得到js的属性?
使用this.Hide();
如果需要在程序中不断变换主窗口,建议采用如下方法:Main方法:
static class Program
{
public static ApplicationContext AppContext = new ApplicationContext();
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
AppContext.MainForm = new Form1();
Application.Run(AppContext );
}
}From1中:
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.Show();
Program.AppContext.MainForm = frm;
this.Close();
}如果需要经常变换的话(比如就是输用户名密码这样的登录),应该在Main方法中,先运行一个ShowDilaog窗口,如果DialogResult返回OK,Application运行新窗口
......打错了
如果不需要经常变换的话(比如就是输用户名密码这样的登录),应该在Main方法中,先运行一个ShowDialog窗口,如果DialogResult返回OK,Application运行新窗口Main方法中:
login frmlogin = new login();
if (login.ShowDialog() == DialogResult.OK)
{
Application.Run(new main_form());
}
static void Main()
{
frmLogin login = new frmLogin();
login.ShowDialog();
if(login.DialogResult.Equals(DialogResult.OK))
{
login.Dispose();
login = null;
Application.Run(new frmMain());
}
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); FrmLogin Login = new FrmLogin();
Login.Text = "Login";
Login.ShowDialog(); if (Login.IsDone)
{
MessageBox.Show("已登录");
Application.Run(new Form1());
}
else
MessageBox.Show("登录失败");
} class FrmLogin : Form
{
public bool IsDone = false; public FrmLogin()
{
this.Text = "登录";
this.StartPosition = FormStartPosition.CenterScreen; Button B = new Button();
B.Parent = this;
B.Text = "Login";
B.Click += new EventHandler(B_Click);
} void B_Click(object sender, EventArgs e)
{
IsDone = true;
Close();
}
}
主窗体一定要关掉,你还硬要说它是主窗体吗?这部自相矛盾嘛。最好不要在程序中某个深处去修改Application的主窗体。虽然Application支持这个功能,但是很诡异。最好的方法,是在编写程序之前就想好什么叫做、哪一个是主窗体,然后一直用它做主窗体。这样当多人开发软件时,不至于不同的程序员变来变去地。对于Hide的窗体,它可以在打开下一个窗体时监视子窗体什么时候关闭,例如this.Hide();
MForm next = new MForm();
next.FormClosed += new FormClosedEventHandler(next_FormClosed);
next.Show();
void next_FormClosed(object sender, FormClosedEventArgs e)
{
this.Close();
}
main_form.show();
不知道是不是你想要的结果
该属性确定此上下文的主 Form。该属性可在任意时间更改。如果未重写 OnMainFormClosed,那么当 mainForm 参数关闭时线程的消息循环终止。用ildasm.exe观察System.Windows.Forms.dll,ApplicationContext.set_MainForm(MainForm的Set器)的实现如下:
.method public hidebysig specialname instance void
set_MainForm(class System.Windows.Forms.Form 'value') cil managed
{
// 代码大小 61 (0x3d)
.maxstack 3
.locals init (class [mscorlib]System.EventHandler V_0)
IL_0000: ldarg.0
IL_0001: ldftn instance void System.Windows.Forms.ApplicationContext::OnMainFormDestroy(object,
class [mscorlib]System.EventArgs)
IL_0007: newobj instance void [mscorlib]System.EventHandler::.ctor(object,
native int)
IL_000c: stloc.0
IL_000d: ldarg.0
IL_000e: ldfld class System.Windows.Forms.Form System.Windows.Forms.ApplicationContext::mainForm
IL_0013: brfalse.s IL_0021
IL_0015: ldarg.0
IL_0016: ldfld class System.Windows.Forms.Form System.Windows.Forms.ApplicationContext::mainForm
IL_001b: ldloc.0
IL_001c: callvirt instance void System.Windows.Forms.Control::remove_HandleDestroyed(class [mscorlib]System.EventHandler)
IL_0021: ldarg.0
IL_0022: ldarg.1
IL_0023: stfld class System.Windows.Forms.Form System.Windows.Forms.ApplicationContext::mainForm
IL_0028: ldarg.0
IL_0029: ldfld class System.Windows.Forms.Form System.Windows.Forms.ApplicationContext::mainForm
IL_002e: brfalse.s IL_003c
IL_0030: ldarg.0
IL_0031: ldfld class System.Windows.Forms.Form System.Windows.Forms.ApplicationContext::mainForm
IL_0036: ldloc.0
IL_0037: callvirt instance void System.Windows.Forms.Control::add_HandleDestroyed(class [mscorlib]System.EventHandler)
IL_003c: ret
} // end of method ApplicationContext::set_MainForm更换MainForm后,ApplicationContext会移除对原MainForm的HandleDestroyed的事件处理,并对新的MainForm添加HandleDestroyed事件处理.另外MSDN中,本身提供了一个让一个WinApp有两个并列(主)窗口的示例(两个窗口全部关闭后才会退出)只要处理好窗口自身的问题,通过修改ApplicationContext.MainForm的方法是安全的,可以放心使用.
{
Thread th = new Thread(new ThreadStart(new MethodInvoker(delegate
{
this.Invoke(new MethodInvoker(delegate { this.Close(); }));
Application.Run(new Form2());
})));
th.Start();
}用新线程关闭原窗口,打开新窗口。
main_form的Load事件:login_form loginform = new login_form();
loginform.Owner = this;
loginform.ShowDialog();
然后login_form完成你需要的操作后就可以
this.Close();