static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); LeftFrameTest frm = new LeftFrameTest(); frm.ShowDialog(); if (LeftFrameTest.bSign) Application.Run(new Form1()); else Application.Exit(); } } using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsApplication1 { public partial class LeftFrameTest : Form { public static bool bSign = false; public LeftFrameTest() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { bSign = true; this.Close(); } } }
f.Show();
this.Close();
方法2:采用简单的办法仅仅将form1隐藏;
原因是:form1采用的是 application.ran(new form1);启动的一个线程那么如果form1中声明了一个form2的话那么form2也是form1的一部分。form1关闭的话form2也会关闭的!
this.Visible = false;
f2.ShowDialog();
this.Visible= true;
试试
关闭 form1而不关闭form2,需要把他们隔离起来
this.Visible = false;
f2.ShowDialog();
就将FORM1隐藏.
this.Hide();
f.Show();
this.Close();
这样就好了
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread] static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
LeftFrameTest frm = new LeftFrameTest();
frm.ShowDialog();
if (LeftFrameTest.bSign)
Application.Run(new Form1());
else
Application.Exit();
}
} using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class LeftFrameTest : Form
{
public static bool bSign = false;
public LeftFrameTest()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
bSign = true;
this.Close();
}
}
}
到form1中初始化啊这样关闭form1,就不关form2的事情哦。。或者像上面说的重新开一个进程或者只隐藏form1
(1)主程序中生成form1;
(2)在form1中,生成from2;
Form2 f=new Form2();
f.Show();
(3)关闭form1,主程序保留
this.Close();
那不能在form1中实例化form2,这样产生父子关系了吧?
试试多线程
this.Visible = false;
form2.Show();
form2.show();//show方法显示创建的Form2窗体
this.close();//this代表当前窗体,关闭当前的Form1窗体
在Program类中static class Program
{
internal static ApplicationContext context = new ApplicationContext(new Form1());
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
//Application.SetCompatibleTextRenderingDefault(false); 这句要注释掉,否则会出错
Application.Run(context);
}
}
然后这样显示窗体:
view plaincopy to clipboardprint?
Form2 form = new Form2();
Program.context.MainForm = form;
form.Show();
f.Show();
this.Hide();
form1 是登录窗口,登录成功后关闭form1,进入from2?可以把form2注册为启动窗口,然后再form2.load里启动一个form1为模态窗口
这样一启动就先模态弹出form1,登录成功就form1.close(),失败就Application.exit().
f.Show();
this.Close();
Form2 frm2 = new Form2()在Form1内:
private Form2 frm2;public Form1(Form2 frm):this(){
this.frm2 = frm;
}// 关闭函数
private void CloseForm(){
if(this.frm2!=null){
frm2.Show();
this.close();
}
另一种:
你可以在创建Form3,在这个主窗口控制Form2和Form1的关闭和显示
}