[英]Close form without exit application
我目前正在开展一个小项目,并希望得到一些帮助。
我有2个表单,第一个是登录窗口,第二个是主程序。 我遇到的问题是,当我用this.Close()
关闭form1
,它将退出整个程序。
我有一种感觉,我需要使用线程或类似的东西,但我找不到合适的资源来解决我的问题。
谢谢。
您可以隐藏第一个表单而不是关闭它:
this.Hide();
Form2 form2 = new Form2();
form2.Show();
你不能改变你的program.cs,以便它运行主窗体,并在它创建的主窗体的启动时显示一个登录表单,然后隐藏自己(等待登录发生在自己身上)?
如果您正在使用WPF,则可以在关闭登录表单之前将Application.MainWindow设置为第二个“主”窗口。
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());
}
编辑:FORGOT SOMETHING
我忘了提到如果你从登录表单中获取成员,你必须先实例化它。 这不是一个好的技术,我建议对此做全局用户类的想法,但我有一个程序需要这个方法,因为我提到它,这是一个例子:
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 - 在调用Application运行之前你可以做很多事情,包括显示你的登录信息。 这是我的一个项目的样本。 它试图找到数据库连接。 如果它不能,它会打开一个向导,并连接到访问或mssql。 如果open是好的,它会显示一个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();
你的意见是什么创造这样的东西
它叫托盘图标
所以用户可以close all forms
, app still running
, user can go back to app any time
让我们开始编码(注意这是WPF应用程序)
private NotifyIcon m_notifyIcon;
private ContextMenuStrip m_contextMenu;
private bool _ForceClose;
public MainWindow()
{
InitializeComponent();
CreateNotifyIcon();
}
private void CreateNotifyIcon()
{
m_contextMenu = new ContextMenuStrip();
ToolStripMenuItem mI1 = new ToolStripMenuItem { Text = "Open" };
mI1.Click += (sender, args) => Maximize();
ToolStripMenuItem mI2 = new ToolStripMenuItem { Text = "Exit" };
mI2.Click += (sender, args) => EndApplication();
m_contextMenu.Items.Add(mI1);
m_contextMenu.Items.Add(mI2);
//Initalize Notify Icon
m_notifyIcon = new NotifyIcon
{
Text = "Application Title",
Icon = new Icon("Icon.ico"),
//Associate the contextmenustrip with notify icon
ContextMenuStrip = m_contextMenu,
Visible = true
};
}
我们有这些功能
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);
}
并在Window Closing
事件监听器中(不要忘记添加它)
private void Window_Closing(object sender, CancelEventArgs e)
{
if (_ForceClose == false)
{
e.Cancel = true;
Minimize();
}
}
就是这样,希望它对你有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.