1

I'm new to c#. Got a log-in system with registration project. Having trouble with the forms.

I have 2 forms, form1 and form2. Form1 is my main form, my log-in form. Form2 is the registration form.

So this is how the application runs: Before form1 shows, there is a condition first to be met. If the condition is met, message box will appear then form2 will show. However, when the condition is not met form1 will show.

So the problem is, form1 keeps showing although the condition is met. Form2 appears but form1 appears too.

private void Form1_Load(object sender, EventArgs e)
{
    if (condition)
    {
        MessageBox.Show("Message");
        this.Hide();
        Form2 frm = new Form2();
        frm.Show();
    }
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Rom
  • 63
  • 1
  • 9
  • 3
    Something like this maybe: http://stackoverflow.com/a/4210040/426894 – asawyer Jun 29 '15 at 18:43
  • 3
    where is `condition` being set? – D Stanley Jun 29 '15 at 18:45
  • possible duplicate of [Hiding a form and showing another when a button is clicked in a Windows Forms application](http://stackoverflow.com/questions/4345666/hiding-a-form-and-showing-another-when-a-button-is-clicked-in-a-windows-forms-ap) – M.kazem Akhgary Jun 29 '15 at 18:49
  • 1
    @M.kazemAkhgary that won't fix anything. Look at the MDSN. `this.Hide()` is equivalent to setting `this.Visible = false;` – Broots Waymb Jun 29 '15 at 18:54

3 Answers3

2

You shouldn't do this sort of thing in Form_Load of Form1 to begin with. Logically, it makes the most sense to test your condition first and then never instantiate Form1 if you're not going to use it.

Inside Program.cs, if you haven't changed it, you should have the following line of code:

Application.Run(new Form1());

You should replace that line with some code to test your condition and determine which form to start up with:

    if (condition)
    {
        MessageBox.Show("Message");
        Application.Run(new Form2());
    }
    else
    {
        Application.Run(new Form1());
    }
Erik
  • 5,355
  • 25
  • 39
1

Your issue seems to be the fact that the load event fires before the form is visible. So technically speaking, it doesn't have anything visible to hide at this point.

Referred to this post: why isn't this.Hide() working in Form1_load event?

Community
  • 1
  • 1
Broots Waymb
  • 4,713
  • 3
  • 28
  • 51
  • and `condition` is false by defect – Alan Jun 29 '15 at 19:01
  • @Zero, well, if we go by OP's actual code, we can assume `condition` really is true as form2 still shows. form1 will also continue to be visible regardless of what `condition` is. – Broots Waymb Jun 29 '15 at 19:03
  • I agree with you. Only clarified that because it will not work if it solves what you indicate. – Alan Jun 29 '15 at 19:07
1

There are many ways to do it but my preferred approach to implement login forms is this:

[STAThread]
static void Main()
{
    if (new FormLogin().ShowDialog() == DialogResult.OK)
    {
        Application.Run(new FormMain());
    }
}

And the typical handler method in login form for login button:

private void btnLogin_Click(object sender, EventArgs e)
{
    if (CheckUserPassword())
    {
        this.DialogResult = System.Windows.Forms.DialogResult.OK;
    }
    else MessageBox.Show("Login failed!");
}
VahidNaderi
  • 2,448
  • 1
  • 23
  • 35