Mr_Green
Mr_Green

Reputation: 41832

close a windows form after another form has been opened

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.

Upvotes: 2

Views: 14335

Answers (3)

varg
varg

Reputation: 3636

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.

Upvotes: 1

Sathish Raja
Sathish Raja

Reputation: 131

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); 
        }
    }

Upvotes: 2

Rajesh Subramanian
Rajesh Subramanian

Reputation: 6490

Try the Following

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

Upvotes: 1

Related Questions