Cristian Muscalu
Cristian Muscalu

Reputation: 9925

Hide Form1, show Form2 on Form1_Load

The Form1 of my App is a login page that i want to:
- show on some conditions
- hide and show Form2 on some conditions

I can hide/show a form by the button click event like so,

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

but the same technique does not work for Form1_Load.

I have tried the first example in this thread,

Program.cs

    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run();
    }

Form1

    private void Form1_Load(object sender, EventArgs e)
    {

        Form2 f2= new Form2();
        f2.Show();
        this.Hide();
    }

but it's not showing neither Form1 or Form2, and i don't see how it could. The second example i can't understand how i can implement, and the next google results are even more confusing.
Please help i'm stuck on this for 2 hours.

Upvotes: 0

Views: 3251

Answers (3)

Little Sylvanas
Little Sylvanas

Reputation: 21

Hello You Can Use This

private void button1_Click(object sender, EventArgs e)
{
    Form2 f2= new Form2();
    if(condition==true)
    {
        this.Hide();
        f2.ShowDialog();
        this.Close();
    }
}

Upvotes: 1

Morteza.M
Morteza.M

Reputation: 177

In the last line in program.cs you must type new Form1() between the parenthesis. So, your program.cs code is as follow:

static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
}

C# can not hide form in form_load evant Apparently. To resolve Hide problem, you can use of a timer and hide the form in tick event. i.e.:

Timer timer = new Timer();

private void timerTick(object sender, EventArgs e)
{
    timer.Enabled = false;
    this.Hide();
}

private void Form1_Load(object sender, EventArgs e)
{
   timer.Tick += new EventHandler(timerTick);
   timer.Interval = 10;
   Form2 frm = new Form2();
   frm.Show();
   timer.Enabled = true;
 }

This works. I tested it.

I hope this will be useful.

Upvotes: 3

Steve
Steve

Reputation: 216343

Why don't you reverse the order of your forms? Start with the main form in the main method.

Application.Run(new Form2());

Now in the constructor of Form2 call the login form with ShowDialog and set the result of the login in a global variable inside the Form2

public class Form2:Form
{
     private bool _isValidated = false;
     public Form2()
     {
         InitializeComponent();

         // Add here the conditions to check if you don't want to 
         // run the login process...
         // if(loginNotRequired) 
         //    _isValidated = true;
         // else 

         using(Form1 fLogin = new Form1())
         {
             // This blocks until the user clicks cancel or ok buttons
             DialogResult dr = fLogin.ShowDialog();
             if(dr == DialogResult.OK)
                _isValidated  = true;
         }
     }

Now in the Form2.Load event check the status of your login and close the Form2 if the login is not successful

    private void Form2_Load(object sender, EventArgs args)
    {
         if(!_isValidated)
            this.Close();
         else
            .....
    }

Upvotes: 0

Related Questions