开发者

How do I change what form loads on startup?

I'm using VS2008 and I created an app with a login screen. That screen is no longer needed, and I can't figure out how to change what form load开发者_StackOverflow中文版s on startup?

Thanks


go to program.cs and change the line:

Application.Run(new Form1());

to whatever form you want.


Go to the source file that contains the "Main" function and just change what Form object is being created,


update this line:

Application.Run(new Form1());


In you Main() function you should have some code like the following:

static void Main()
{            
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new MainForm());
}

This is where the program starts up the form called MainForm, this is where you need to change the name of the form that runs at startup.


In your startup project, you should have a program.cs file.

static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
}

The starting form is Form1. You could change that to whatever form you want.


You can create an ApplicationContext

Example:

  public class ApplicationLoader : ApplicationContext
    {
        #region main function

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            try
            {

                //Application.EnableVisualStyles();
                Application.Run(new ApplicationLoader());
            }
            catch( System.Exception exc )
            {
                MessageBox.Show( exc.Message, "Unexpected Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion

        public ApplicationLoader()
        {
            MainForm = new LoginForm();
        }

        protected override void OnMainFormClosed(object sender, EventArgs e)
        {
            if (sender is LoginForm)
            {
                //change forms
            }
            else
                ExitThread();
        }

        private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            //catch exception
            Application.Exit();
        }
    }


go to program.cs in solution explorer

static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new FrmLogin());
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜