3

I have 2 Forms, Startform is a Login Form (Form1) and a Form that opens after Login, Form2.

when Login is successful the form2 shows.

f2.Show(); //form2 show
this.Hide(); //login(f1) hide

This works.

Now i want that if i press the red X Button (right top) that Form2 close and the Login page shows again.

I tried this in Form2:

Form1 f1 = new Form1();
....
...
private void Main_FormClosing(object sender, FormClosingEventArgs e)
    {
      f1.show();
    }

But this just close the Form2 and not open the From1 and the Program is still running in the Background

steffen1994
  • 247
  • 2
  • 12
  • Do you need to show the existing login form (the one you called `.Hide()` on) or a new one? – EpicKip Jun 06 '18 at 10:52
  • @EpicKip I want to show the one I hided – steffen1994 Jun 06 '18 at 10:53
  • Possible duplicate of [How to show a form again after hiding it?](https://stackoverflow.com/questions/13233451/how-to-show-a-form-again-after-hiding-it) – Thomas Flinkow Jun 06 '18 at 10:53
  • 1
    @steffen1994 then alter your form2 constructor to take a `Form1` parameter (so change `public Form2()` to `public Form2(Form1 form)`). Then in the constructor assign it to a global value and call `.Show()` on that – EpicKip Jun 06 '18 at 10:55
  • @EpicKip ok thank you, but I am really new to c#, maybe you can help me if you can give me an example for this :) – steffen1994 Jun 06 '18 at 10:57
  • 1
    Application.OpenForms[0].Show(); Do *strongly* consider to not ask for login credentials, your user already provided them when he logged in to Windows. Any extra one you put on top is quite likely to have severe security issues, not limited to exposing passwords. A winforms app is just not anything like a web page, you are not dealing with a complete stranger. – Hans Passant Jun 06 '18 at 10:58
  • 1
    @steffen1994 in the duplicate Thomas linked, this answer: https://stackoverflow.com/a/43879608/2885376 is what I mean – EpicKip Jun 06 '18 at 10:59
  • OFFTOP: seeing questions like this, I know why WPF is go-to framework now. In all comparisons (maybe besides learning curve) it beats Winforms hands down. @steffen1994, if you are just starting to learn GUI dev in C#, switch to WPF if it makes sense. – Krzysztof Skowronek Jun 06 '18 at 12:09

1 Answers1

1

In my example Form1 does role of your LoginForm

problem is what you are killing a Form2 which actually have created instance of Form1 (here your login form). so when instance of Form2 will be gone with it all its local instance will be gone too.

you can do one thing, while creating an object of Form2 from you Form1 pass object of Form1 to Form2.

so you will not required to create an instace of Form1 in Form2 and while closing it you can simply call Form1's show method.

like below.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        //passing current class' object
        Form2 form2 = new Form2(this);

        form2.Show();
        this.Hide();
    }
}

and Form 2 :

public partial class Form2 : Form
{
    Form1 m_form1;
    public Form2(Form1 form1)
    {
        InitializeComponent();
        m_form1 = form1;
    }

    private void Form2_FormClosing(object sender, FormClosingEventArgs e)
    {
        m_form1.Show();
    }
}
Amit
  • 1,821
  • 1
  • 17
  • 30
  • Hello @Amit, I added this now exactly in my code, but its the same problem again, the form2 close when I press X but form1 does not show with this code – steffen1994 Jun 06 '18 at 11:17
  • @steffen1994 are you sure that you are not at doing `new Form1()` anywhere in your Form2 ? – Amit Jun 06 '18 at 11:19
  • yes, I searched through the code, no new From1() in Form2 – steffen1994 Jun 06 '18 at 11:22
  • How are you passing the password and userName? I did a tutorial on this last year on my YouTube channel and I can post the answer with the tutorial link. – Halonic Jun 06 '18 at 11:36
  • @Amit, I got it, you Solution is fine, the problem is, it don't works with `private void Form1_FormClosing(object sender, FormClosingEventArgs e) {.... }` but with `protected override void OnFormClosing(FormClosingEventArgs e) { m_form1.Show(); }` – steffen1994 Jun 06 '18 at 11:36
  • @Halonic what? I don't understand what you mean. – steffen1994 Jun 06 '18 at 11:42
  • I posted my answer – Halonic Jun 06 '18 at 11:55