Hi,


I have develop a program in C# windows Application & now The problem is My Login form is designed on form4 I want to run form4 first after that i want to run the other forms but when i run the program form1 is run automatically. acually in coding I have run the form1 after form4

I use the code for

In form4

form1 fr1 = new form1();
fr1.show();

Why it happends what is the solution for this problem

tahnk u

virusisfound

Dani AI

Generated

Brief recap: sees Form1 appear first even though Form4 is intended as the login form. and correctly pointed at the application startup and using a login dialog. The extra option below avoids changing which form is created in a single line and gives explicit control over app lifetime: use a custom ApplicationContext to show the login form first, then open the main window only if login succeeds.

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

class AppContext : ApplicationContext
{
    public AppContext()
    {
        var login = new LoginForm();
        login.FormClosed += OnLoginClosed;
        login.Show();
    }

    private void OnLoginClosed(object sender, FormClosedEventArgs e)
    {
        var login = (LoginForm)sender;
        if (login.Authenticated) // set true inside LoginForm on success
        {
            var main = new MainForm();
            main.FormClosed += (s, args) => ExitThread();
            main.Show();
        }
        else
        {
            ExitThread();
        }
    }
}

Notes and troubleshooting tips:

  • Implement a public property (for example bool Authenticated) on your login form and set it to true when credentials are validated, then close the login form.
  • If the app still opens Form1 first, set a breakpoint at your application's entry point to confirm which code runs, and search the solution for any accidental new Form1(...) or Show() calls in constructors/Load handlers or designer code.
  • In Visual Studio you can also inspect the project Application/Startup settings to confirm the intended startup object.
  • For details about this pattern see the ApplicationContext documentation: ApplicationContext class.

Recommended Answers

All 2 Replies

In the solution explorer double click (or right click and "View Code")

There you will find:

Application.Run(new Form1());

now change it to:

Application.Run(new Form4());

ereruh's post would mean the application closes when Form4 closes.

If the login dialog is only required at the start of the application then one solution is to open the login form before the Application.Run in as below.

DialogResult result;
using(Form4 loginDialog = new Form4())
{
    result = loginDialog.ShowDialog();
}
if (result == DialogResult.OK)
    Application.Run(new Form1());

Another solution is to open the login form in Form1.Load

private void Form1_Load(object sender, EventArgs e)
{
    DialogResult result;
    using(Form4 loginDialog = new Form4())
    {
        result = loginDialog.ShowDialog();
    }
    if (result != DialogResult.OK)
        this.Close();
}

Note: the above samples assume that Form4 is a dialog and returne DialogResult.OK if the login is OK. You will need to adjust the code to suit how your Form4 works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.