0

I have a form(f1) that needs to run some instructions before opening another one(f2), and when it ends must close. I set in program.cs Application.Run(new f1());and at the end of f1 istructions I wrote

f2 f = new f2();
f.ShowDialog();
this.Close();

but it doesn't close, just goes in background.

Dominic Zukiewicz
  • 8,258
  • 8
  • 43
  • 61
Vlad
  • 41
  • 1
  • 1
  • 8
  • 1
    Sounds like you're misusing forms. Why don't you move that code out to a class and call that class before doing `Application.Run(new f2())` and skip the first pointless form? – Neil Smith Jul 14 '14 at 16:16
  • Do you mean like a splash screen? Like Outlook opens when loading, and then getting the user to click OK before loading the application? – Dominic Zukiewicz Jul 14 '14 at 16:20
  • yes @DominicZukiewicz .. i would like to obtain something like that or visual studio starting form – Vlad Jul 14 '14 at 16:23
  • @Smith.h.Neil 'cause in the first form i load data from files to a dgv and I made the start form with a progress to hide all the loading process – Vlad Jul 14 '14 at 16:25
  • @user3547175 It sounded like you were using the first form just as a placeholder for code, which wouldn't be the proper use of a form. I see you're going for a splashscreen though. What you want is to display `f1` (splashscreen) while you load data and when the data is done loading you want to close `f1` and display `f2`? – Neil Smith Jul 14 '14 at 16:28
  • @user3547175: Have a look at this question too. It seems to have what you need: http://stackoverflow.com/a/4994912/128444 – Dominic Zukiewicz Jul 14 '14 at 16:30
  • `f1` splash screen and `f2` is the main form, need to close f1 after loading istructions – Vlad Jul 14 '14 at 16:31

1 Answers1

0

Everything should work the way you have it but hide f1 and display f2 this way:

this.Hide();
var frm2 = new f2();
frm2.Closed += (sender, args) => this.Close();
frm2.Show();

To give a more detailed answer:

public class Form1() : Form
{
    public Form1() {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e) {
        var worker = new BackgroundWorker();
        worker.DoWork += worker_DoWork;
        worker.RunWorkerCompleted += worker_RunWorkerCompleted;
        worker.RunWorkerAsync();
    }

    private void worker_DoWork(object sender, DoWorkEventArgs e) {
        Thread.Sleep(5000);
        // this is where you can load your data from file
    }

    private void worker_RunWorkerCompleted(
        object sender, RunWorkerCompletedEventArgs e) 
    {
        this.Hide();
        var frm2 = new Form2();
        frm2.Closed += (s, a) => this.Close();
        frm2.Show();
    }
}

So Form1 will start to load data using a background worker. When the data is loaded, the BackgroundWorker will fire the RunWorkerCompleted event. That event handler will hide Form1 instead of closing it, create Form2, add a handler for form2's Closed event so that closing Form2 will stop your application, and then shows Form2.

Neil Smith
  • 2,565
  • 1
  • 15
  • 18