【问题标题】:Start Loading Form only One time and not appear again till Application is restart仅开始加载表单一次,直到应用程序重新启动后才再次出现
【发布时间】:2018-10-13 03:56:29
【问题描述】:

我正在使用 C# 进行编码,并且我正在使用加载表单 (SplashScreen) 来启动我的应用程序,一旦加载,您就可以访问登录表单。 因此,在您登录后,您可以访问仪表板表单,并且在 Dashboard From 中有用于注销的按钮。 因此,当您单击注销时,它会再次显示登录表单以再次登录,但我的问题是在登录表单出现之前,它会先直接加载表单(SplashScreen),然后再登录。我希望加载表单(SplashScreen)在应用程序第一次启动时仅在开始时出现一次,并且在应用程序重新启动之前不会再次出现。

在我的代码下方:

// 登录表单

public Login_Form()
{
  Thread t = new Thread(new ThreadStart(StartFrom));
  t.Start();
  Thread.Sleep(5000);
  InitializeComponent();
  t.Abort();       
}

public void StartFrom()
{
   Application.Run(new frmSplashScreen());
}

// 退出

private void Logout_Click(object sender, EventArgs e)
{
  Hide();

  Login_From fLogin = new Login_From();
  if (fLogin.ShowDialog() == DialogResult.OK)
  {
     DashBoardForm view = new DashBoardForm();
     view.ShowDialog();
  }
  else
  {
     Application.Exit();
  }
}

//程序cs

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
         Application.EnableVisualStyles();
         Application.SetCompatibleTextRenderingDefault(false);
         //Application.Run(new Login_From());

         Login_From fLogin = new Login_From();
         if (fLogin.ShowDialog() == DialogResult.OK)
         {
             Application.Run(new DashBoardForm());
         }

         else 
         {
             Application.Exit();
         }


    }
}

【问题讨论】:

  • Main方法中,将用于显示splash的代码放在Program.cs中。
  • 现在检查我已经更新了

标签: c#


【解决方案1】:

你可以这样做。

首先从 program.cs 中运行启动画面

//程序cs

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
         Application.EnableVisualStyles();
         Application.SetCompatibleTextRenderingDefault(false);

         frmSplashScreen splash = new frmSplashScreen();
         splash.Show();
    }
}

然后从初始屏幕显示登录表单。

//飞溅

public frmSplashScreen()
{
    System.Threading.Thread.Sleep(5000);
    Login_From fLogin = new Login_From();
    fLogin.Show();
}

注销时,只需创建一个新的登录表单并显示即可。

// 退出

private void Logout_Click(object sender, EventArgs e)
{
    Hide();
    Login_From fLogin = new Login_From();
    this.Close();
    fLogin.ShowDialog();
}

【讨论】:

    【解决方案2】:

    为了清楚起见,请阅读内联的 cmets。

    主要方法

    [STAThread]
    static void Main(String[] args)
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
    
        new SplashScreenUsingVBFramework().Run(args);
    }
    

    继承 WindowsFormsApplicationBase

    // We need to add Microsoft.VisualBasic reference for type 
    // WindowsFormsApplicationBase.
    
    class SplashScreenUsingVBFramework : WindowsFormsApplicationBase
    {
        protected override void OnCreateSplashScreen()
        {
            base.OnCreateSplashScreen();
            // You can replace the Splash2 screen to yours.
            this.SplashScreen = new CSWinFormSplashScreen.SplashScreen2();
        }
        protected override void OnCreateMainForm()
        {
            base.OnCreateMainForm();
    
            // Here you can specify the MainForm you want to start.
            Login_From fLogin = new Login_From();
            if (fLogin.ShowDialog() == DialogResult.OK)
            {
                Application.Run(new DashBoardForm());
            }
            else 
            {
                Application.Exit();
            }
        }
    }
    

    我从示例here 中借用并修改了代码。它甚至还有一个闪屏示例,如果您有兴趣,它会淡出。

    【讨论】:

      猜你喜欢
      • 2013-09-12
      • 1970-01-01
      • 2016-09-29
      • 2011-07-14
      • 1970-01-01
      • 1970-01-01
      • 2021-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多