我目前正在开发一个小型项目并希望获得一些帮助。没有退出应用程序的关闭窗体
我有2种形式,第一种是登录窗口,第二种是主程序。我遇到的问题是,当我用this.Close()
关闭form1
时,它正在退出整个程序。
我有一种感觉,我需要使用线程或类似的东西,但我找不到合适的资源来解决我的问题。
谢谢。
我目前正在开发一个小型项目并希望获得一些帮助。没有退出应用程序的关闭窗体
我有2种形式,第一种是登录窗口,第二种是主程序。我遇到的问题是,当我用this.Close()
关闭form1
时,它正在退出整个程序。
我有一种感觉,我需要使用线程或类似的东西,但我找不到合适的资源来解决我的问题。
谢谢。
可以隐藏第一种形式,而不是将其关闭:
this.Hide();
Form2 form2 = new Form2();
form2.Show();
然后我有另一个问题,当我关闭Form2我想关闭隐藏的Form1,我该怎么做? – 2010-08-09 18:31:12
@Victor:同样的道理,但您需要提供一种真正关闭这两种表单并关闭应用程序的方法。 – 2010-08-09 18:33:30
@Victor:Application.Exit()应该诀窍 – 2010-08-09 18:37:33
如果你使用WPF,您可以设置Application.MainWindow你的第二个“主要”窗口,关闭您的登录表单之前。
另一种方式WPF更灵活... – 2010-08-09 18:33:51
实际上在vb.net中,您可以指定应用程序关闭模式。 C#不允许你轻松地说。 http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/61b817f8-d7d3-44de-9095-91a6e3f2150c – ioWint 2011-08-21 05:02:56
难道你不能改变你的program.cs,以便它运行主窗体,并在主窗体的启动时创建并显示登录窗体,然后隐藏自身(等待登录发生以显示自身)?
准确地说,我会说: 请确保你没有使用第一种形式像这样加载应用程序... Application.Run(new Form1()); – MilkyWayJoe 2010-08-09 18:29:35
Program.cs是您的主要功能所在。如果使用Visual Studio将项目创建为Windows应用程序,那么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());
}
打开程序。 cs - 在应用程序运行调用之前,您可以做很多事情,包括显示您的登录信息。以下是我的一个项目的示例。它试图找到数据库连接。如果不能,它会打开一个向导并连接到访问或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();
什么你的意见,以创建类似的东西
其所谓的托盘图标
使用户可以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();
}
}
就是这样,希望它能帮助你
是这winforms或wpf?这不是一个线程问题,而是一个什么“this”在你最有可能使用的范围内的问题。代码将有所帮助。 – 2010-08-09 18:25:30
可能重复的[关闭窗体,然后调用另一个。](http://stackoverflow.com/questions/2751076/closing-a-form-and-then-call-another-one) – 2010-08-09 18:30:15
我还没有做很多与winforms,但只是为了验证,你正在创建你的登录屏幕与主窗口,因为它的父母,不是? – 2010-08-09 18:53:46