开发者

C#, how to hide one form and show another?

When my project starts Form1 loads and checks the program license with the server, if everything's OK it should: show Form2 and close Form1. Afterward when the user would close Form2 with the "x", the program should end.

What do you think would be the bes开发者_Python百科t way of doing it?

So far got only to form2.Show :)

...
if (responseFromServer == "OK")
{
    Form2 form2 = new Form2();
    form2.Show();
}

Thanks!


I use something like this. Code is in Program.cs

public static bool IsLogged = false;

Application.Run(new FUserLogin());
if (!isLogged)
  Application.Exit();
else
Application.Run(new FMain());


As you probably know if you use Form1 as your main form then you can't close it as this will close the application (unless you customize the way the app starts, but that's more advanced).

One option is to start by creating Form2 as your main form, but keep it hidden, then create and show Form1, and then when the license check is finished, close Form1 and make Form2 visible.

Or you can start by showing Form1 and then when the license check is done, call Form1.Hide() and then create and show Form2. Then when Form2 is closed by the user, call Form1.Close() in the Form2.Closed event handler:

class Form1
{
    private void Form1_Load(object sender, EventArgs e)
    {
        // do the license check, 
        // and then when the license check is done:
        if (responseFromServer == "OK")
        {
           Form2 form2 = new Form2();
           Form2.FormClosed += new FormClosedEventHandler(Form2_FormClosed);
           Form2.Show();
           this.Hide();
       }
       else
           this.Close();
    }


    private void Form2_FormClosed(object sender, FormClosedEventArgs e)
    {
        this.Close(); // will exit the application     
    }
}


You can show the first form using ShowDialog, which will block until the form closes. Inside Form1 you can just call this.Close() when it is done processing, and either set the DialogResult property, or (probably cleaner) you can create a public property that Form1 sets before it closes, and have the caller check that. Then you can either return from Main directly, or go ahead and instantiate your new class and pass it to Application.Run().

static class Program
{
  [STAThread]
  static void Main()
  {
    var form1 = new Form1();
    var result = from1.ShowDialog(); // Form1 can set DialogResult, or another property to indicate success

    if (result != DialogResult.OK) return; // either this
    if (!form1.ValidationSuccessful) return; // or this

    Application.Run(new Form2());
  }
}

I like this because you are not referencing Form2 from Form1, all of the code to deal with showing Form1 and exiting the application is consolidated in one place, and can easily be commented out for development or testing.


Try this to hide Form1:

this.Hide();

then in your FormClosing event of Form2:

Form2_FormClosing(object sender, EventArgs e)
{
    Application.Exit();
}


Try this

//Form1 code
if (responseFromServer == "OK")
{
this.Hide();
Form2 frm = new Form2();
frm.Show();
}

And you can exit from the application using Application.Exit() method in the Form2's form closing event


//Login Form Load Events or Constructor
this.Close(); //first Close Login From               
Application.Run(new Main());//second Run Main Form
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜