0

我在窗口表单应用程序中显示或隐藏表单有问题。

我首先在 program.cs ( Application.Run(new loginform());) 开始运行 loginform,当登录成功时,然后显示另一个表单( Main Interface ),我想在显示第二个表单时关闭或隐藏登录表单,但是它不管用。

我不知道该怎么做。是否与线程相关的问题?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace Myapp
{
   static class Program
   {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Loginfrm());

        }
    }
}
4

3 回答 3

1

您可以向您的Loginfrm类添加一个属性,指示登录是否成功。然后,在关闭 your 之后Loginfrm,您可以开始另一个消息循环。

例子:

static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Loginfrm login = new Loginfrm(); 
    Application.Run(login);
    if (login.LogInSuccesfull)
        Application.Run(new MainForm());
}
于 2012-06-09T09:31:42.170 回答
0
// in the mainform add to project form and call it SubForm
SubForm subform = new Subform();
subform.Show();
// in the subform
subform.Close();
于 2012-06-09T08:24:55.147 回答
0

使用单例

主界面.cs

using System;

public class MainInterface : Form
{
   private static MainInterface Current;

   private MainInterface ()
   {
      if ( LoginForm . Instance != null )
         LoginForm . Instance . Close ();
   }

   public static MainInterface Instance
   {
      get 
      {
         if (Current == null)
         {
            Current = new MainInterface ();
         }
         return Current;
      }
   }
}

登录表单.cs

using System;

public class LoginForm: Form
{
   private static LoginForm Current;

   private LoginForm ()
   {

      if ( MainInterface . Instance != null )
         MainInterface . Instance . Close ();
   }

   public static LoginForm Instance
   {
      get 
      {
         if (Current == null)
         {
            Current = new LoginForm ();
         }
         return Current;
      }
   }
}

程序.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace Myapp
{
   static class Program
   {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            LoginForm . Instance . ShowDialog ();

        }
    }
}

转换形式:

从登录表单

LoginForm . Instance . Hide ();
MainInterface . Instance . ShowDialog ();

从主界面

MainInterface . Instance . Hide ();
LoginForm . Instance . ShowDialog ();

对于超过 2 个表单,我建议使用 Manager 类(例如 Process )来管理它们并在它们之间切换:)

问候,

于 2012-06-09T08:58:25.727 回答