argciv
argciv

Reputation: 11

How to close Form1 and open Form2

In Form1 when progressbar is completed then in timer_tick events' else part the following code is written by me:

frmLogin login = new frmLogin();
login.Show();
timer1.Enabled = false;
this.Hide();

So any solution to close the Form1 instead of the hide Form1?

Upvotes: 1

Views: 633

Answers (2)

Patrick Hofman
Patrick Hofman

Reputation: 156938

You have something called the ApplicationContext for that.

Use it like this:

ApplicationContext applicationContext = new ApplicationContext();

FormX formX = new FormX(applicationContext);
applicationContext.MainForm = formX;

Application.Run(applicationContext);

When closing Form1, hand over the MainForm

FormY formY = new FormY(applicationContext);
applicationContext.MainForm = formY;

Another option is to make a static ApplicationContext. That wouldn't require passing it around.

Upvotes: 1

John Alexiou
John Alexiou

Reputation: 29244

I would do it from the main form:

public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();            
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        var login=new LoginForm();
        if(login.ShowDialog()==DialogResult.OK)
        {
            // Validation ok
        }
        else
        {
            this.Close();
        }
    }
}

So before the main form shows up, it loads and shows the LoginForm. When that is done, it closes the LoginForm and shows the main form.

Upvotes: 0

Related Questions