0

So, I'm making a payroll management system as a hobby project to help my resume and general knowledge of c#. So, I'm making a UI and I can open a new window just fine with this code:

private void button1_Click(object sender, EventArgs e)
{
            CreateAdminAcct createAcct = new CreateAdminAcct();
            createAcct.StartPosition = FormStartPosition.CenterScreen;
            createAcct.Show();

            this.Hide();
}

however, I don't know the event to check when the little red "x" button is clicked, because when that button is clicked, I want to go back to the main screen because I hide the main screen when that button is clicked, and when i click the red "x" on the screen that just opened, it closes, but the application continues to run in the background.

If there is some better way to manage multiple menus, I'm open to suggestions, however, this is what I've found easiest.

Thanks in advance

  • 1
    There are a number of possible approaches. The easiest way is to open your new form as a dialog, which makes it "modal." When you close it, control returns to the original form. – Robert Harvey Feb 19 '22 at 15:13
  • Is this Winforms, WPF, Xamarin .. ? – Robert Harvey Feb 19 '22 at 15:15
  • Protip: don't create new instances of new forms inside other form methods - instead design a "form manager" class which keeps track of all open forms - and share the manager between your form instances, that way every instantiated form (even multiple instances of a single `Form` class) can communicate with each other. – Dai Feb 19 '22 at 15:18
  • https://stackoverflow.com/questions/8750602/detect-when-a-form-has-been-closed-c-sharp – user18253515 Feb 19 '22 at 15:39

1 Answers1

0

I second Robert Harvey's suggestion; this gives the user the reassurance tha tht emain window is still open/ nothing got lost, but it's unreachably "behind" the CreateAdminAcct form while the CreateAdminAcct form is open

private void button1_Click(object sender, EventArgs e)
{
        CreateAdminAcct createAcct = new CreateAdminAcct();
        createAcct.StartPosition = FormStartPosition.CenterScreen;
        createAcct.ShowDialog();

        //do any code here that needs to access createAcct before it's lost
        MessageBox.Show(createAcct.NewAdmin.Name);
}

If you really do want to hide your main form, pass the main form itself to createAcct, and make it createAcct's job to re-open the main form when it is closing

private void button1_Click(object sender, EventArgs e)
{
        CreateAdminAcct createAcct = new CreateAdminAcct(this); //note passing this form to constructor
        createAcct.StartPosition = FormStartPosition.CenterScreen;
        createAcct.Show();
}

class CreateAcctForm : Form{
  private Form _showWhenClosing;
  CreateAcctForm(Form revertTo){
    InitializeComponent();
   
    _showWhenClosing = revertTo;
  }
}

void Form_Closing(object sender, ...){  //event
  _showWhenClosing.Show();
}

Side note: please rename your controls after you drop them ona form. code that's stuffed with label57, textbox25 is effectively obfuscated and really wearisome to follow

Caius Jard
  • 72,509
  • 5
  • 49
  • 80