I want to close my login form and open Main form!
i do that like this!

private void btnLoginOk_Click(object sender, EventArgs e)
            {
            if (textBox2.Text == "acer")
                {
               
                Main frmMain = new Main();
                frmMain.Show();
                this.Close();
                }
            else
                {
                MessageBox.Show("Invalid Username or Password", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                
                }
            
            }

But My mainForm also close in this code.what i do now?

I guess your mainform is in fact your login form. Line 8 of your code closes it, together with frmMain. Instead of closing your main form, use the Visible property.

For forms which are opened for some small amout of time, like Login form, best way to close them (because they have to close sooner or later) is to use the "using" keyword and a ShowDialog() method:

//on your class from where you call login form:
using(LoginForm login = new LoginForm())
{
    if(login.ShowDialog() == DialogResult.OK)
    {
        //when login form will close the code will come in here
        //and login form will close it self!
    }
}

//on login form:
//when login validation is checked and validated (and validation is OK), you do:
this.DialogResult = DialogResult.OK;
//then the login form will close automatically when it will execute that code to the end!

This is how you do it.

or you could just put this.close() before line 6 therefore it wont include frmMain

You also could just hide the form using this.Hide.

MainForm main = new MainForm();
main.Show();
this.Hide();

that would still hide the main form because it is being shown before this.hide

that would still hide the main form because it is being shown before this.hide

I just tested this and no, it doesn't hide the Main form. Also putting this.Close() before a form.Show() doesn't help. It still closes the main form. I suggest you to just hide it. And perform a Application.Exit() at closing the main form (to shutdown the program completly, when intented offcourse).

fair play
im a noob :)

Main frmMain = new Main();
                frmMain.Show();
                this.Close();

try...

this.Close();
                Main frmMain = new Main();
                frmMain.Show();

my app uses the same type of format, and using this.Close(); before calling another form, closes the current form, before opening a new form.

how can i close a form that is open but with an event of a separate form?

try this.. worked for me... not sure if its what you need tho...

formToCloseName fTCN = (formToCloseName )Application.OpenForms["formToCloseName "];
                    fTCN .Close();

//
//use this in a button handler...
//

you could maybe initially do loginform.hide();

then in your new form, use this code, to close the login form.
you could also not use form.hide(), and just this code in form 2, which will close any other form which is currently open...

thankyou so much walid86! worked perfectly.

no worries :), don't forget to mark it as solved if it's working as needed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.