Question How to declare with which Form to start the Program

asmodi

New member
Joined
Sep 18, 2019
Messages
2
Programming Experience
1-3
Hello everybody,

I am building a windows form application and my program always starts with form 1 automaticly. How can I declare that I want to start with a different Form?
 
In your program.cs file you will have a a file that looks like this :
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TestCSharpApp
{
    internal static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        private static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}
On line 19, change Form1 or whatever will be in its place to whatever instance of your form you want to run.

The link John gave above may not let you choose your form object. So change it by setting your startup object as defined in his link, and then change the form in the program.cs file as I've outlined above.
Screenshot_29.jpg
 
There seems to be a difference in that dialog with VB and C#, where in C# you have to change the Application.Run code manually as said before.
 
Yes, the difference is hugely different. In VB.NET, you can select an actual form from the settings pane I have screenshot above, but in C#, you can optionally set your program startup form which holds the hard coded form to launch within it. This would require you to change the code to reference the instance of your forms name which you want to be the default form. Your reference while still useful is really geared towards VB.Net though.
 
Back
Top Bottom