0

I have two Winforms open at present.Out of the two one is Login form.Now as per my requirement ,if the user entered right credentials then these two opened forms needs to be closed and new form should be opened. Means I have to close opened winforms and open new Winform on button click event of Login form. Here i am not knowing exactly which windows are open because login form window is coming from menu button click event which is present on every form Please help me.Thanks in advance..

Ram
  • 545
  • 6
  • 16
  • 28
  • @GrantWinney i am not knowing exactly which windows are open because login form window is coming from menu button click event which is present on every form – Ram Mar 05 '14 at 12:30

4 Answers4

0

You can iterate over the Application.OpenForms collection and close that you need.

mnieto
  • 3,744
  • 4
  • 21
  • 37
0

Try this:

foreach (Form f in Application.OpenForms)
{
    f.Close();
}

OR using for loop:

for (int i = Application.OpenForms.Count - 1; i >= 0; i--)
{
     Application.OpenForms[i].Close();
}

OR create a list of forms:

List<Form> openForms = new List<Form>();
foreach (Form f in Application.OpenForms)
    openForms.Add(f);
foreach (Form f in openForms)
{
   f.Close();
}

As per you requirement, close all other forms except login form and then show that form.

foreach (Form f in Application.OpenForms)
    {
        if(f.Name!="frmLogin")   //Closing all other
            f.Close();           //forms
    }

Now activate the login form.

frmLogin.Show();
frmLogin.Focus();

Application.OpenForms gets a collection of open forms owned by the application. Read more about Application.OpenForms.

Raging Bull
  • 18,593
  • 13
  • 50
  • 55
  • after this loop finishes execution.Can i write codes to open next forms.Will it be ok – Ram Mar 05 '14 at 12:42
  • After execution of the list of forms Codes i have written code to open new form i.e Form1 frm=new Form1(); frm.show(); Now my Form1 flashes for a second and closes immedialtely..How to resolve this.I want My Form1 to be opened – Ram Mar 05 '14 at 12:45
  • Do you have `MdiParent` for your application? – Raging Bull Mar 05 '14 at 12:49
  • No Sir, its simple Form – Ram Mar 05 '14 at 12:50
  • You can close all forms except the login form. I think that's what you are after. – Raging Bull Mar 05 '14 at 12:55
  • You can check the name like `f.Name!="frmLogin"` while closing the forms. BTW, are you trying to restart the application? – Raging Bull Mar 05 '14 at 13:09
  • If you are doing this for security, I would suggest you to call the `login` form with `ShowDialog()` from the button click. User will not be able to bypass the form unless he gives the right username and password OR he click the cancel button. For restarting the application Read [this](http://stackoverflow.com/questions/779405/how-do-i-restart-my-c-sharp-winform-application) – Raging Bull Mar 05 '14 at 13:17
  • Yes i am trying to restart but with my windows name as the starting windows .Is this possible.How and where can i put conditions for that? – Ram Mar 05 '14 at 13:17
  • I am not getting why my required form flashes for a second and closes automatically..How can i stop and hold the windows? – Ram Mar 05 '14 at 13:39
0

you can't use foreach to close the forms as suggested in earlier replies. This is because foreach cannot be used to alter the enumerated forms list (when you Close() them, you will get a run-time error). Even if you use a for loop, you'll have to check that the main form is also not closed by mistake...

for(int i=0; i< Application.OpenForms.Count; i++)
{
    Form f = Application.OpenForms[i];
    if(f != this)
        f.Close();
}

Instead, you can try out the below logic. Where are these two forms getting loaded from? is it from a main form? I am assuming that both are being displayed using Form.Show() method.

In the login form login button handler, I'd accept a reference to the main form. when validation succeeds, I'd call a function LoginSuccessful() in the parent form, which would iterate through the open forms and close them.

public partial class FormMain : Form
{
    LoginForm loginForm;
    OtherForm otherForm;

    public FormMain()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        loginForm = new LoginForm(this);
        otherForm = new OtherForm();

        loginForm.Show();
        otherForm.Show();
    }

    public void LoginSuccessful()
    {
        loginForm.Close();
        otherForm.Close();  
        OtherForm thirdForm = new OtherForm();
        thirdForm.Show();
    }
}

LoginForm code as below:

public partial class LoginForm : Form
{
    FormMain parent;
    bool bLoginSuccessful = false;

    public LoginForm(FormMain parent)
    {
        InitializeComponent();
        this.parent = parent;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        bLoginSuccessful = true;
        Thread.Sleep(5000);
        if (bLoginSuccessful == true)
            parent.LoginSuccessful();
    }
}

This should help you in solving your problem... Granted, it isn't the best way... It all depends upon your approach. If you are more detailed in your requirement, I can probably think out a better way.

Tolga Evcimen
  • 7,112
  • 11
  • 58
  • 91
Bharat Mallapur
  • 674
  • 9
  • 17
  • `This is because foreach cannot be used to alter the enumerated forms list (when you Close() them, you will get a run-time error)`?? Try my code. – Raging Bull Mar 05 '14 at 12:53
  • @Raging Bull: I didn't say it can't be done... Just that the code written earlier wouldn't work.. It will give an InvalidOperation exception as the "collection was modified, enumeration operation may not execute". your code is more complex. You're adding all the forms into a list and then closing them. Instead, just do a for loop and close them in one operation. – Bharat Mallapur Mar 05 '14 at 12:59
  • @Dmitry: this is example code... In his code, he'd be writing the login code, which would set the bool variable to true... I'm just writing sample code here, not meant to be copy-pasted blindly... – Bharat Mallapur Mar 05 '14 at 13:00
0

In your login form, make default constructor private and add a new constructor and a private member like this:

private Form _callerform;

private LoginForm()
        {
            InitializeComponent();
        }

public LoginForm(Form caller)
        {
            InitializeComponent();
        }

Now, in the click event of button on LoginForm, try something like this:

        Form SomeOtherForm = new Form();
        SomeOtherForm.Show();
        // Hide login and caller form
        Hide();
        _callerForm.Hide();

Now, you have hidden a couple of forms and opened a new one. When the user closes the application, you need to close other forms too. So,

    void Application_ApplicationExit(object sender, EventArgs e)
    {
        foreach (Form form in Application.OpenForms)
        {
            form.Close();
        }
    }
danish
  • 5,550
  • 2
  • 25
  • 28