1

I have two forms: Form1 and Form2.

I want to show Form2 as dialog when Form1 has been loaded. I mean when Form1 is loaded and visible to the user, then Form2 is showed as dialog.

With the Form1_Load event it first show the Form2 as dialog and then show Form1.

How can I first show Form1 and then Form2 as dialog?

Shin
  • 664
  • 2
  • 13
  • 30
buddy
  • 418
  • 4
  • 10
  • 29
  • possible duplicate: [How can I close a login form and show the main form without my application closing?](http://stackoverflow.com/questions/4759334/how-can-i-close-a-login-form-and-show-the-main-form-without-my-application-closi) – Cody Gray - on strike Feb 02 '12 at 06:28
  • It's all right here: https://msdn.microsoft.com/en-us/library/c7ykbedk(v=vs.110).aspx or google `showdialog method` -> this will not allow use of Form1 until Form2 closes. Don't forget to use Dispose if u need it closed and not hidden. – B. Shea Jan 12 '17 at 16:28

3 Answers3

5

Use the Shown event of form1 to load the form2 as follows:

void form1_Shown(object sender, EventArgs e)
{
    Form2 form2 = new Form2();
    form2.Show();
}

That way first form1 will be displayed and raise the Shown event and inside Shown event, form2 will be loaded and displayed.

S2S2
  • 8,322
  • 5
  • 37
  • 65
0

This to launch Form1, then Form2

public Form1()
{    
     this.Load+= Form1_Load;
     InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
     Form2 myForm2 = new Form2();
     myForm2.Show();
}

or to do without loading Form1 first, and forcing them into Form2 first.

public Form1()
{    
     Form2 myForm2 = new Form2();
     myForm2.ShowDialog(); 
     //ShowDialog() will prevent actions from happening on this 
     //thread until Form2 is closed.

     InitializeComponent();
}

if you just want to start Form2 first, just modify the Program.cs

static void Main(string[] args)
{

    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    if (args.Length == 0) //if no command line arguments, run Form1
    {
        Application.Run(new Form1());
    }
    else //if command line arguments exist, run Form2
    {
        Application.Run(new Form2());
    }

}
corylulu
  • 3,449
  • 1
  • 19
  • 35
  • sorry may i not clarify my question into your mind. I want to show first form1. when the form1 is displayed then the form2? – buddy Feb 02 '12 at 06:34
0

You can load the second form in event Form1 Validated:

public Form1()
{    
     this.Validated += Form1_Validated;
     InitializeComponent();
}
private void Form1_Validated(object sender, EventArgs e)
{
     Form2 myForm2 = new Form2();
     myForm2.Show();
}
wertyk
  • 410
  • 10
  • 30