【问题标题】:Close form without exit application关闭表单而不退出应用程序
【发布时间】:2011-03-27 10:55:05
【问题描述】:

我目前正在做一个小项目,希望得到一些帮助。

我有两个表单,第一个是登录窗口,第二个是主程序。我遇到的问题是,当我用this.Close() 关闭form1 时,它正在退出整个程序。

我觉得我需要使用线程或类似的东西,但我找不到合适的资源来解决我的问题。

谢谢。

【问题讨论】:

  • 这是winforms还是wpf?这不是线程问题,而是您最有可能使用它的范围内的“this”问题。代码会有所帮助。
  • 我对 winforms 做的不多,只是为了验证,您正在创建登录屏幕,主窗口是父窗口,不是吗?

标签: c# windows


【解决方案1】:

您可以隐藏第一个表单而不是关闭它:

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

【讨论】:

  • 那我还有一个问题,当我关闭Form2的时候我想关闭隐藏的Form1,我该怎么做呢?
  • @Victor:同样的方法,但您需要提供一种方法来真正关闭两个表单并关闭应用程序。
  • 基于您隐藏表单的事实,并且仅在您关闭此隐藏表单时才会关闭应用程序,顺便说一句,这将是一个未使用的资源,一直打开..我会说这不是最好的解决方案...
【解决方案2】:

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

【讨论】:

【解决方案3】:

您不能更改您的 program.cs 以使其运行主窗体,并在主窗体的启动中创建并显示一个登录表单,然后隐藏自己(等待登录发生) ?

【讨论】:

  • 正是我要说的:确保你没有使用第一个表单来加载这样的应用程序... Application.Run(new Form1());
【解决方案4】:

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

【讨论】:

    【解决方案5】:

    你对创建这样的东西有什么看法

    它称为托盘图标

    所以用户可以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();
        }
    }
    

    就是这样,希望对你有帮助

    【讨论】:

      【解决方案6】:

      打开 Program.cs - 您可以在调用应用程序运行之前做很多事情,包括显示您的登录信息。这是我的一个项目的示例。它试图找到数据库连接。如果不能,它会打开一个向导并连接到 access 或 mssql。如果打开良好,它会显示一个闪烁屏幕并最终运行应用程序,否则它会关闭。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多