C# 无退出申请关闭表单
我目前正在做一个小项目,希望得到一些帮助 我有两个表单,第一个是登录窗口,第二个是主程序。我的问题是,当我用C# 无退出申请关闭表单,c#,windows,C#,Windows,我目前正在做一个小项目,希望得到一些帮助 我有两个表单,第一个是登录窗口,第二个是主程序。我的问题是,当我用this.close()关闭form1时,它正在退出整个程序 我有一种感觉,我需要使用线程或类似的东西,但我找不到合适的资源来解决我的问题 谢谢。您可以隐藏第一个表单而不是关闭它: this.Hide(); Form2 form2 = new Form2(); form2.Show(); 如果您使用的是WPF,您可以在关闭登录表单之前设置第二个“主”窗口。您不能更改program.cs以
this.close()
关闭form1
时,它正在退出整个程序
我有一种感觉,我需要使用线程或类似的东西,但我找不到合适的资源来解决我的问题
谢谢。您可以隐藏第一个表单而不是关闭它:
this.Hide();
Form2 form2 = new Form2();
form2.Show();
如果您使用的是WPF,您可以在关闭登录表单之前设置第二个“主”窗口。您不能更改program.cs以使其运行主表单,并在主表单启动时创建并显示登录表单,然后隐藏自己(等待登录显示本身)?Program.cs是您的主要功能所在。如果您使用Visual Studio将项目创建为Windows应用程序,则main中会有一个函数运行启动程序时打开的窗体。只需从main中的登录表单获取登录信息,然后调用第二个窗口。以下是一个例子:
[STAThread]
private static void Main(string[] args)
{
//there's some other code here that initializes the program
//Starts the first form (login form)
Application.Run(new form1());
//Get the login info here somehow. Maybe save in public members of form1 or
// in a global utilities or global user class of some kind
//Run the main program
Application.Run(new mainProgramForm());
}
编辑:忘记了某事我忘了提到,如果您确实从登录表单中获取成员,您必须首先实例化它。这不是一个好的技术,我建议在这上面使用全局用户类的思想,但我有一个程序需要这种方法,既然我提到了它,下面是一个示例:
private static void Main(string[] args)
{
//there's some other code here that initializes the program
//Instead of running a new form here, first create it and then run it
Form1 form1 = new Form1(); //Creates the form
Application.Run(form1); //Runs the form. Program.cs will continue after the form closes
//Get the login info
string username = form1.Username;
string password = form1.Password;
//Get rid of form1 if you choose
form1.Dispose();
//Validate the user info
//Run the main program
Application.Run(new mainProgramForm());
}
打开Program.cs-在调用应用程序运行之前,您可以做很多事情,包括显示您的登录信息。下面是我的一个项目的示例。它试图找到数据库连接。如果不能,它将打开一个向导并连接到access或mssql。如果打开良好,它会显示一个spalsh屏幕并最终运行应用程序,否则它会关闭
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
DialogResult LclResult;
EESDatabasePersistentData LclPersist;
LclPersist = new EESDatabasePersistentData();
string LclDataType;
string LclDatabase;
string LclServer;
string Password;
string UserID;
if (!LclPersist.GetConnection(out LclServer, out LclDatabase, out LclDataType, out UserID, out Password))
{
// Run the connection wizard
GetConnection(out LclServer, out LclDatabase, out LclDataType, out UserID, out Password);
}
if (LclDataType == "ACCESS")
InitDataAccess(LclDatabase);
else if (LclDataType == "SQLSERVER")
InitDataSQL(LclServer, LclDatabase, UserID, Password);
if (OpenGood)
{
if (!System.Diagnostics.Debugger.IsAttached)
{
FormSplash.Instance.SetupVersion();
///////////////////////////////////
// If we don't call do events
// splash delays loading.
Application.DoEvents();
FormSplash.Instance.Show();
}
Application.DoEvents();
Application.Run(new FormMentorMain());
}
else
Application.Exit();
你对创造这样的东西有什么看法 它被称为托盘图标 因此,用户可以
关闭所有表单
,而应用程序仍在运行
,,用户可以随时返回应用程序
让我们开始编码(注意这是WPF应用程序)
我们有这些功能
protected void Minimize()
{
Hide();
}
protected void Maximize()
{
Show();
this.WindowState =WindowState.Normal;
}
protected void EndApplication()
{
Minimize();
Thread.Sleep(1000);
_ForceClose = true;
WindowState = WindowState.Normal;
this.Close();
Environment.Exit(0);
}
在窗口关闭中
事件侦听器(不要忘记添加它)
就是这样,希望它能帮助您这是winforms还是wpf?这不是一个线程问题,而是一个在您最可能使用它的范围内“这”是什么的问题。代码会有帮助。可能的重复我对winforms没有做太多,但只是为了验证,您正在创建登录屏幕,主窗口是它的父窗口,不是吗?正是我想说的:确保您没有使用第一个表单像这样加载应用程序。。。Application.Run(新Form1());然后我还有一个问题,当我关闭Form2时,我想关闭隐藏的Form1,我该怎么做?@Victor:同样的方法,但是你需要提供一种方法来真正关闭两个表单并关闭应用程序。基于你隐藏表单的事实,并且只有在关闭这个隐藏表单时才会关闭应用程序,顺便说一下,将是一个始终处于打开状态的非受保护资源。。我认为这不是最好的解决方案……实际上,在vb.net中,您可以指定应用程序关闭模式。c#不允许你轻易说出。
protected void Minimize()
{
Hide();
}
protected void Maximize()
{
Show();
this.WindowState =WindowState.Normal;
}
protected void EndApplication()
{
Minimize();
Thread.Sleep(1000);
_ForceClose = true;
WindowState = WindowState.Normal;
this.Close();
Environment.Exit(0);
}
private void Window_Closing(object sender, CancelEventArgs e)
{
if (_ForceClose == false)
{
e.Cancel = true;
Minimize();
}
}