简体   繁体   中英

Hiding Form 1 and Show form 2 on Windows Form 1 Load

Am a newbie when it come to C# Programming. Here's my trouble: I want to Show a new Form 2 and Hide Form 1 on the Windows Form 1 Load.

Here's my current codes;

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

My en-counted Error with the current Code: When Form 1 load its loading Form 2 but it's not hiding itself. this.Hide Statement not working, I've try this.Close but this will Close the entire software as it's closing the main form.

Can anyone kindly help me with this error.

You can use Visible property to hide your form.

private void Form1_Load(object sender, EventArgs e)
{
    var Form2 = new Form2();
    Form2.Show();
    this.Visible=false;
}

The Error i thought is that you are showing the form2 before hiding the previous form.

private void Form1_Load(object sender, EventArgs e)
{
     var Form2 = new Form2();
     form1.Visible=false;
     Form2.Show();
}

You can also make form2 as modal by using show dailog method() by which focus is given to the form2 and form1 becomes inactive though it will be shown.

modal and modeless forms https://msdn.microsoft.com/en-IN/library/aa984358%28v=vs.71%29.aspx

Here's how I manage to make this work.

New Codes: Form 1

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

    private void Timer_Tick(object sender, EventArgs e)
    {
        if (Properties.Settings.Default.Status == "Form2Visible")
        {
            this.Hide();
        }

    }

Explanation: On the Windows Form 1 Load am showing the the Form 2, then using a timer to verify a Properties.Settings Value. If the Properties.Settings = Form2Visible, Form 1 will Hide. Once am done on Form 2 I simply need to change the Properties.Settings to something else and stop the Timer on Form 1.

If their is a simplest way let me know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM