close a windows form after another form has been opened

14,149

Solution 1

Once you close the main form, your application's message loop terminates, which causes the entire application to exit. The Windows message loop is tied to your main form because that's the one you started Application.Run(new mainform()).

Try some other approach in your Program.cs

    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        MainForm mf  = new MainForm();
        if (mf.ShowDialog() == DialogResult.OK) 
        {  
           subForm newSubForm = new subForm();   
           newSubForm.RegisterMainForm(this);                        
           Application.Run(newSubForm); 
        }
    }

Solution 2

Try the Following

        subForm newSubForm = new subForm();
        newSubForm.Parent = this;
        newSubForm.ShowDialog();
        this.Hide();

Solution 3

I don't know why you wanted to close your mainForm. In this case, the mainForm is the application executable form. If you close the mainForm, all other forms will be also closed.

What do you really want to do? I think the naming of your forms is a little bit vexing. mainForm means to me, that the main part of your application will be executed in this form, eh?

Why don't you build a LoginForm that will be shown up after you application has been started and the user is currently not verified? I think this shouldn't avoid your purpose and will be a clean solution.

Some Code Examples:

public partial class MainForm : Form
{
    private bool isVerified = false;

    public MainForm()
    {
        InitializeComponent();
        InitializeLogin();
    }

    private void MainForm_Load(object sender, EventArgs e)
    {

    }

    private void InitializeLogin()
    {
        if (!isVerified)
        {
            using (LoginForm login = new LoginForm())
            {
                if (login.ShowDialog() == DialogResult.OK)
                {
                    MessageBox.Show("Login successful!");
                    isVerified = true;
                }
            }
        }
        else
        { }
    }

This is the LoginForm, that will be called after the Mainform is initialized and there is currently no verified user. Note that this is only demo code, but could be one possible implementation.

public partial class LoginForm : Form
{

    public LoginForm()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (IsUser())
        {
            // the DialogResult of the Form must be set 
            this.DialogResult = System.Windows.Forms.DialogResult.OK;    
        }
    }

    private bool IsUser()
    {
        return true;
    }

Another idea is to build a bootstrapper that managing your Application.Run() method before any form is loaded like Sathish Raja S solution.

Some Additional Thoughts:

  • Avoid Users from clicking the Login Button before Username/Password Textboxes are filled with credentials.
  • You can build this more flexible and extend the LoginForm to verify Users for special password secured sections of your application, but this first "version" can be reused application wide.
Share:
14,149
Mr_Green
Author by

Mr_Green

If you do not know or know - There won't be any conflicts If you pretend to know, there will be conflicts -- Shri Sadhguru Forget about your house of cards And I'll do mine Fall off the table And get swept under Denial, denial -- Radiohead - House of cards I'll paint it on the walls 'Cause I'm the one at fault I'll never fight again And this is how it ends -- Linkin Park - Breaking the habit

Updated on June 04, 2022

Comments

  • Mr_Green
    Mr_Green almost 2 years

    In my application, I am using two forms mainform and subForm. The mainForm has simple login application. On click of the button (btnClick) in mainForm(if the login credentials matches), it should get directed to the subForm. This I can do easily. but the problem is that the mainForm still visible and whenever I close the mainForm it is also closing the subForm. Which I dont want to happen. and If I give this.Close() after the function same problem is happening.
    " How to close the mainForm after opening the subForm. "

    I have tried the below code:

    btnClick Event:

                subForm newSubForm = new subForm();
                newSubForm.Show();
                newSubForm.RegisterMainForm(this);
                this.Close();
    

    RegisterMainForm is just a internal method to consider the actual subForm.

  • Mr_Green
    Mr_Green over 11 years
    Thanks for your response. For this maybe i should use MessageBox before opening the subForm right?
  • Sathish Raja
    Sathish Raja over 11 years
    why you need MessageBox before opening the subform?
  • Mr_Green
    Mr_Green over 11 years
    and also I am trying to open two different forms on different conditions.. like if --> subForm, else if-->subForm1 else --> show error dialog. In this situation, can I use this?
  • Sathish Raja
    Sathish Raja over 11 years
    ShowDialog is going to show your mainform as modal dialog, in your btnClick event just set DialogResult, ` private void button1_Click(object sender, EventArgs e) { if (LoginVerified) { this.DialogResult = DialogResult.OK; } else { this.DialogResult = DialogResult.Cancel; } } `
  • Sathish Raja
    Sathish Raja over 11 years
    set some property in your mainform and access them to switch between your subforms
  • Mr_Green
    Mr_Green over 11 years
    the problem with hide() is that the application is still running in back even after closing the subForm . Is there any way to avoid it?
  • Mr_Green
    Mr_Green over 11 years
    I think I have not followed you. please see my code once. program.cs , mainForm.cs
  • Mr_Green
    Mr_Green over 11 years
    well Thanks I used your code in combination with @RajeshSubramanian code and it worked. I did some own research also.