3D-kreativ
3D-kreativ

Reputation: 9301

How to handle several windows form

I'm doing a small project to store movies. I'm using three windows form, a intro form, a main form and a new movie form. I also have a class that I call MovieManager that is the heart of the application. My problem is that I'm not sure how I should handle this windows.

Let's say I want the application to start with the intro form and when the user click on the OK-button the main form should appear. What is the best way to do this? Should I in the Program.cs create an object of the MovieManager class that show and hide the different windows form or should I in the Program.cs just start by showing the intro form?

Upvotes: 0

Views: 213

Answers (3)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236208

You can simply do all staff in Program.cs when starting application. Show your IntroForm as dialog. If user clicks OK, then start main application form, otherwise close application.

static void Main()
{
    IntroForm introForm = new IntroForm();
    if (introForm.ShowDialog() != DialogResult.OK)
    {
        Application.Exit();
        return;

    }

    Application.Run(new MainForm());
}

If you need single MovieManager instance for all these forms, then you can create it in Main method and pass same instance to IntroForm and MainForm:

MovieManager movieManager = new MovieManager();

IntroForm introForm = new IntroForm(movieManager);
if (introForm.ShowDialog() != DialogResult.OK)
{
    Application.Exit();
    return;
}

Application.Run(new MainForm(movieManager));

Also you can implement MovieManager as Singleton, which will be accessible everywhere via static property MovieManager.Instance.

Upvotes: 2

Youp Bernoulli
Youp Bernoulli

Reputation: 5645

What you call intro form, I should call a splash screen. In the program.cs I should just pop-up the splash screen (with a logo, title and info about the application, version number, etc.). The splash screen is shown for a certain amount of time (use a timer for this, or Thread.Sleep is alos possible altough a bit heavy).

When the splash screen closes show the MainForm is shown a from there you can instantiate a MovieManager or use a static MovieManager (it depends on its use). From the mainform you can then just instantiate and show new movie form(s).

We use a piece of code something like this:

static void Main(string[] args)
{
  try
  {
    SplashScreen.ShowSplashScreen();
    Application.DoEvents();
    SplashScreen.WaitForVisibility(.5);
    bool starting = true;
    while (starting)
    {
      try
      {
        SplashScreen.SetStatus("Initialize mainform...");
        starting = false;
        Application.Run(new MainForm());
      }
      catch (Exception ex)
      {
        if (starting)
          starting = XtraMessageBox.Show(ex.Message, "Fout bij initialiseren", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1) == DialogResult.Retry;
        else
          throw (ex);
      }
    }
  }
  catch (Exception ex)
  {
    if (ex is object)
      XtraMessageBox.Show(ex.Message, "Algemene fout", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
  }
}

And code in the splashscreen (excerpt) looks like this:

if (_splashForm == null)
{
  ThreadPool.QueueUserWorkItem(RunSplashForm, null);
  while (_splashForm == null || _splashForm.IsHandleCreated == false)
  {
    System.Threading.Thread.Sleep(50);
  }
}

Maybe these links will also provide you some usefull information:

http://www.reflectionit.nl/Articles/Splash.aspx

And we used this as the basis for our own code:

http://www.codeproject.com/Articles/5454/A-Pretty-Good-Splash-Screen-in-C

Upvotes: 1

Ali Vojdanian
Ali Vojdanian

Reputation: 2111

There are different ways to show a new form. you can use MdiWinForm first you must change IsMdiContainer property to true then use these codes in the MainForm :

  Form2 f2;    

private void button1_Click(object sender, EventArgs e)
{
    if (f2 == null) {
       f2 = new Form2();
       f2.MdiParent = this;
       f2.FormClosed += delegate { f2 = null; };
       f2.Show();
    }
    else {
       f2.Activate();
    }
}

Upvotes: 0

Related Questions