2

I want to load a welcome screen on the startup of the application then the user clicks a button on the welcome screen and then closes the welcome screen. Finally then shows the main screen.

static void Main() //startup method being called
{
  Application.EnableVisualStyles();
  Application.SetCompatibleTextRenderingDefault(false);
  Application.Run(new ACM812_DB()); // welcome screen
}

When a button is clicked on the welcome screen it then hides the welcome window and then brings up the main window. As shown below.

private void button1_Click(object sender, EventArgs e)
{
   Form1 form1 = new Form1();
   form1.Show(); // main window
   this.Hide();
}

It does work successfully but is it the correct way to implement this?

Updated code:

Main form startup (MainForm.cs)

namespace System
{
  public partial class MainForm : Form
  {
     private void MainForm_Load(object sender, EventArgs e)
     {
        WelcomeForm.Run(this);
     }

     public MainForm()
     {
        InitializeComponent();
     }

   }
 }

Welcome screen then called

public partial class WelcomeForm : Form
{
    static private Form Sender;

    static public void Run(Form sender)
    {
      if (sender == null) 
      throw new ArgumentNullException();
      Sender = sender;
      new WelcomeForm().ShowDialog();
    }

    private void ButtonClose_Click(object sender, EventArgs e)
    {
      Close();
    }
}
Broots Waymb
  • 4,713
  • 3
  • 28
  • 51
Sam
  • 73
  • 5

1 Answers1

3

Not really a good pattern.

Because the Application instance is made to manage the main form that is made to be "alive" until the application exit.

You can show the welcome screen in the main form load as a dialog:

MainForm.cs

private void MainForm_Load(object sender, EventArgs e)
{
  WelcomeForm.Run(this);
}

WelcomeForm.cs

public partial class WelcomeForm : Form
{
  static private Form Sender;
  static public void Run(Form sender)
  {
    if (sender == null) 
      throw new ArgumentNullException();
    Sender = sender;
    new WelcomeForm().ShowDialog();
  }
  private void ButtonClose_Click(object sender, EventArgs e)
  {
    Close();
  }
}

Hence the welcome screen can control the main form and set any public data needed.

The MainForm is created in the Main method and the Application instance take control. So the load event of MainForm is called and this last calls the Run static method of the WelcomeForm that creates an instance and shows it as a dialog. This stop the MainForm, not shown yet, until the welcome screen is closed. Then the MainForm can be shown.

It is one way among others, but it is simple, if it matches your goal.

  • So basically start the application from the **MainForm.cs**. But shouldn't in the **WelcomeForm.cs** the new Form1().Show() should be in the button_click event? – Sam Jul 05 '20 at 12:25
  • 1
    I corrected the typing error for Form1 instead of WelcomeForm, sorry. And yes, once the button close clicked in the welcome screen, shown as a dialog, the main form will appear. –  Jul 05 '20 at 12:32
  • Still confused your answer suggests that the welcome screen loaded when the button is clicked on the main form. But my question is, I want to first load the welcome screen then on the welcome screen click a button that then takes you to the main form. – Sam Jul 05 '20 at 12:32
  • Never mind the update came after your correction... – Sam Jul 05 '20 at 12:34
  • 1
    I added an explanation. –  Jul 05 '20 at 12:39
  • I have updated my code. is this correct? And should not the ButtonClose_click be inside the partial class? – Sam Jul 05 '20 at 12:44
  • 1
    You should call the WelcomeForm.Run from the load event not the constructor nor before InitializeComponent. The ButtonClose_click is for the welcome screen, so that is correct (answer code was corrected). –  Jul 05 '20 at 12:46
  • Im still having trouble with this part **You should call the WelcomeForm.Run from the load event not the constructor nor before InitializeComponent.** I have updated my code. Is this is what you meant? – Sam Jul 05 '20 at 12:58
  • Yes. But I really must sleep... c/c error again. I corrected the code: keep ShowDialog instead of Show. It will works now. –  Jul 05 '20 at 13:06