Hie guys!I am developing an application(has 4 forms so far) and I want the Login form to be closed when one is on the Main Form.The application has an option of exiting the system on the Main Form but when I do that the Login Form will still be there..I need your help guys.Here is part of my code

if (dr["UserID"].ToString()==txtID.Text&& dr["UserName"].ToString() == txtUser.Text && dr["Password"].ToString() == txtPass.Text && dr["Station"].ToString() == cmbStation.Text)
                {
                    Login lg = new Login();
                    lg.Close();
                    MainForm mfrm = new MainForm();
                    mfrm.Show();                   
                    
                }

Write following code in the constructor method of MainForm,

...
 public MainForm() {
       Login lg = new Login();
       lg.ShowDialog();
       InitializeComponent();
 }
...
if (dr["UserID"].ToString()==txtID.Text&& dr["UserName"].ToString() == txtUser.Text && dr["Password"].ToString() == txtPass.Text && dr["Station"].ToString() == cmbStation.Text)
                {
                    this.Close();
                }
...

what if you want to show your log in form again and close the main form?

If you want to exit your application you ca use :

Application.Exit();

Also if you goes from login from to main form,
when you are in login form you must use :

this.Close();

so your login form will be closed and you go to main form
then you can use this.close() or application.exit()

first open main form and then close login form
try it

//MainForm mfrm = new MainForm();

//mfrm.Show(); 
this.Close();

;

try this code...
if u use Application.Exit(), ur complete application will be closed. its something like you are using Alt + F4...

sry, leave the previous post...

Login lg = new Login();
lg.Close();
//MainForm mfrm = new MainForm();
//mfrm.Show(); 
this.Close();

;

try this....
if u use Application.Exit(), ur complete application will be closed. its something like you are using Alt + F4...

arunkumars, what purpose does Login lg = new Login(); lg.Close(); serve? You create the form, run its constructor then dispose of it? If you were trying to obtain a reference to the existign Login form and close that one, this is not the way to do it.

Since it seems the OP got the answer they needed and AngelicOne has tacked their question on an old thread i'll do my best to answer Angelic's question.
It sounds like you want the flow of execution to be something like:

Start application
   |
Show Login Form--------        
   |                   |
If Login        When Main Form Closed
Successful      return to Login Form
   |                   |  
Show Main Form---------

If thats the case then make the login form the main application form, when the user logs in use something like:

private void LogIn()
{
    if(LoggedIn) //check credentials here
    {
         MainForm frm = new MainForm(); //create main form
         frm.FormClosed += new FormClosedEventHandler(frm_FormClosed); //hook event to catch when form is closed
         frm.Show(); //show main form
         this.Hide(); //hide login form
    }
}
        
void frm_FormClosed(object sender, FormClosedEventArgs e)
{
    this.Show(); //reshow login form
}

thats what i am trying to say is design by Ryshad

//Login lg = new Login();
      //lg.Close();
      MainForm mfrm = new MainForm();
      mfrm.Show();
      this.Close();

;
sorry, this was what I was trying to express..

That makes a little more sense, but it is still not the ebst course of action.
Its a mistake i see far too often...you are creating an instance of the child form within the parent form then closing the parent. I personally think this should close the child too, but due to a quirk in the .net framework the child keeps the parent in memory because it holds a reference to it.
You should HIDE the current form and if you dont plan on showing it again later then change this.Show() to this.Close in the frm_FormClosed event handler in my code above. This change will mean that the parent form closes when the child form does.

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.