简体   繁体   English

关闭表格没有退出申请

[英]Close form without exit application

I'm currently working on a small project and would like some help on it. 我目前正在开展一个小项目,并希望得到一些帮助。

I have 2 forms, the first is a login window and the second will be the main program. 我有2个表单,第一个是登录窗口,第二个是主程序。 The problem I have is that when I close form1 with this.Close() it is exiting the whole program. 我遇到的问题是,当我用this.Close()关闭form1 ,它将退出整个程序。

I have a feeling that I need to use threading or something like that but I can't find the right resource for my problem to be solved. 我有一种感觉,我需要使用线程或类似的东西,但我找不到合适的资源来解决我的问题。

Thanks. 谢谢。

You could hide the first form instead of closing it: 您可以隐藏第一个表单而不是关闭它:

this.Hide();
Form2 form2 = new Form2();
form2.Show();

你不能改变你的program.cs,以便它运行主窗体,并在它创建的主窗体的启动时显示一个登录表单,然后隐藏自己(等待登录发生在自己身上)?

如果您正在使用WPF,则可以在关闭登录表单之前将Application.MainWindow设置为第二个“主”窗口。

Program.cs is where your main function is. Program.cs是您的主要功能。 If you created the project as a Windows App with Visual Studio then there will be a function in main that runs the form that opens when you start the program. 如果您使用Visual Studio将项目创建为Windows应用程序,则main中将有一个函数运行在您启动程序时打开的表单。 Just get the login info from the login form in main and then call the second window. 只需从main中的登录表单获取登录信息,然后调用第二个窗口。 Here is an example: 这是一个例子:

[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());
}

EDIT: FORGOT SOMETHING 编辑:FORGOT SOMETHING

I forgot to mention that if you do get members from the login form you will have to instantiate it first. 我忘了提到如果你从登录表单中获取成员,你必须先实例化它。 This is not a good technique, and I recommend doing the Global user class idea over this, but I have a program that requires this method, and since I mentioned it, here is the example: 这不是一个好的技术,我建议对此做全局用户类的想法,但我有一个程序需要这个方法,因为我提到它,这是一个例子:

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());
}

Open Program.cs - you can do quite a bit before the call to Application run, including showing your login information. 打开Program.cs - 在调用Application运行之前你可以做很多事情,包括显示你的登录信息。 Here is a sample from one of my projects. 这是我的一个项目的样本。 It tries to find the database connection. 它试图找到数据库连接。 If it can't, it opens up a wizard and either connects to access or mssql. 如果它不能,它会打开一个向导,并连接到访问或mssql。 If the open is good it shows a spalsh screen and finally runs the application, else it closes. 如果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();

whats your opinion to create something like that 你的意见是什么创造这样的东西

任务栏图标

its called tray Icon 它叫托盘图标

so user can close all forms and the app still running ,, and user can go back to app any time 所以用户可以close all formsapp still runninguser can go back to app any time

lets start coding (note this is WPF Application) 让我们开始编码(注意这是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
    };
}

and we have these functions 我们有这些功能

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);
}

and in Window Closing event listener (don't forget to add it) 并在Window Closing事件监听器中(不要忘记添加它)

private void Window_Closing(object sender, CancelEventArgs e)
{
    if (_ForceClose == false)
    {
        e.Cancel = true;
        Minimize();
    }
}

that's it, hope it help you 就是这样,希望它对你有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 关闭Windows窗体而不退出整个应用程序 - Close a windows form without exiting the entire application 启动并立即关闭应用程序而不显示表单 - Start and Immediately Close application without showing Form 首页> C#>如何关闭主窗体而不让应用程序关闭? - c# - How to close the main form without letting the application close? Winforms:Application.Exit 与 Environment.Exit 与 Form.Close - Winforms: Application.Exit vs Environment.Exit vs Form.Close 如何让只有授权用户才能访问退出应用程序或关闭特定表格? - How do you let only authorized user have access to exit application or close specific Form? 是否允许在不关闭C#中的应用程序的情况下关闭Form1? - Allow Form1 to close without closing Application in C#? 如何在不使用 process.Kill() 的情况下安全地关闭另一个具有退出弹出窗口的应用程序 - How to safely close another application that has an exit popup without using process.Kill() 关闭TCP连接并退出应用程序的方法 - Way to close TCP Connection and Exit Application 如何在不关闭应用程序的情况下关闭登录表单并显示主表单? - How can I close a login form and show the main form without my application closing? 关闭C#表单但不关闭调用应用程序? - Close C# form but not close calling application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM