0

I am using Visual studio 2012. I created two forms, form1 with a button to open form2 and the form2 has a button "exit" which will take me back to form1.

this is my code from form 1:

private void btnRecords_Click(object sender, EventArgs e)
{
    Form2 frm2 = new Form2();
    frm2.ShowDialog();
    this.Hide();
}

and from form 2:

 private void btnExit_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

I know I can use frm2.Show(); this.Hide(); instead of frm2.showdialog();. But, I need my form 1's state to be unaltered. My form1 contains a login form, which only enables the buttons (such as the new form button) if the login is correct. So if I hide form1 and just show it again, the login resets;

wruckie
  • 1,717
  • 1
  • 23
  • 34
Shrumms Dorke
  • 109
  • 1
  • 2
  • 9
  • because my login screen and my main menu buttons are on the same page @GrantWinney https://fbcdn-sphotos-f-a.akamaihd.net/hphotos-ak-frc1/t1/1003385_10203194962101399_1825981410_n.jpg – Shrumms Dorke Jan 23 '14 at 02:24
  • You must create a main-form contain all your works, and sub-form contain login form. So you can call the sub-form from main-from anywhere, anytime. – Louis.CLast Jan 23 '14 at 02:33
  • @Louis.CLast sorry, i'm kind of blurry at the concept. I just have different forms, form 1,form2,form3. Do i have to set one as main and others as sub? Would it be different than just having multiple forms? – Shrumms Dorke Jan 23 '14 at 02:41

2 Answers2

1

On Form2 class add a property to store the reference to the parent form:

public Form ParentForm { get; set; }

Then on Form1, you can show Form2 this way, hiding Form1 at the same time:

private void btnRecords_Click(object sender, EventArgs e)
{
    Form2 frm2 = new Form2();
    frm2.ParentForm = this;
    this.Hide();
    frm2.ShowDialog();
}

When closing Form2, you can show Form1 again:

private void btnExit_Click(object sender, EventArgs e)
{
    this.ParentForm.Show();
    this.Close();
}

Or even better, close Form2 this way:

private void btnExit_Click(object sender, EventArgs e)
{
    this.Close();
}

private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
    if (this.ParentForm != null)
        this.ParentForm.Show();
}

This will also show Form1 back if we the user closes Form2 using the cross button in the title bar.

Szymon
  • 42,577
  • 16
  • 96
  • 114
0

Why don't you handle the login in the form_load event of the main form. The form_load does not run every time the form regains focus. Is it appropriate to close the form on them if they do not login on load? In my case, I send an email with the username of windows user to Domain administrators and close the program. They have to reopen the program to give it another go.

    private void frmMain_Load(object sender, EventArgs e)
    {
        //Check login
        Form frmLogin = new Form();
        frmLogin.ShowDialog();
        if (frmLogin.LoginSucessful == true)
        {
            btnRecords.Enabled = true;
            lblWarning.Visible = false;
        }
        else
        {
            btnRecords.Enabled = false;
            lblWarning.Visible = true;
            lblWarning.Text = "You must first Login";
        }
        //other setup code here
    }
wruckie
  • 1,717
  • 1
  • 23
  • 34