• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 233
  • Last Modified:

C# -- close "Login.cs", keep "Main.cs" open

How can I get the below Login.cs
method to "dispose" without
killing "MainForm.cs" ?

this.Dispose(); and this.Close();
both cause "MainForm.cs" to also close.
------------------------------------------------------------------
        private void btnLogin_Click(object sender, EventArgs e)
        {
            GetUserIDTableAdapter AdapterUID = new GetUserIDTableAdapter();
            DataSet1.GetUserIDDataTable dtUserId = AdapterUID.GetData(vLogin.Text, vPassword.Text);
            if (dtUserId.Rows.Count > 0)
            {
                MainForm frm = new MainForm();
                frm.Show();
                // this.Dispose();
            }
            else
            {
                MessageBox.Show("Login Failed");
            }
        }
0
finance_teacher
Asked:
finance_teacher
  • 4
1 Solution
 
Meir RivkinFull stack Software EngineerCommented:
u can't.
what i suggest u to do is to use the following code:
 private void btnLogin_Click(object sender, EventArgs e)
        {
            GetUserIDTableAdapter AdapterUID = new GetUserIDTableAdapter();
            DataSet1.GetUserIDDataTable dtUserId = AdapterUID.GetData(vLogin.Text, vPassword.Text);
            if (dtUserId.Rows.Count > 0)
            {
this.Hide();
                MainForm frm = new MainForm();
                frm.ShowDialog();
this.Close();
            }
            else
            {
                MessageBox.Show("Login Failed");
            }
        }
0
 
Meir RivkinFull stack Software EngineerCommented:
when u close/dispose the login form it disposes all "internal" instances and one of them is your main form.
0
 
Meir RivkinFull stack Software EngineerCommented:
besides, it would be more UI to allow the user to go back from main form once logged in, and maybe use the login form again with different user/password.

for instance:
private void btnLogin_Click(object sender, EventArgs e)
        {
            GetUserIDTableAdapter AdapterUID = new GetUserIDTableAdapter();
            DataSet1.GetUserIDDataTable dtUserId = AdapterUID.GetData(vLogin.Text, vPassword.Text);
            if (dtUserId.Rows.Count > 0)
            {
this.Hide();
                MainForm frm = new MainForm();
                if(frm.ShowDialog() == DialogResult.OK){
this.Show();
}
else{
this.Close();
}
            }
            else
            {
                MessageBox.Show("Login Failed");
            }
        }
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
Meir RivkinFull stack Software EngineerCommented:
just set the this.DialogResult in the main form when leaving the form.
assume that if the user click OK, the login form is displayed again to allow him to login with different user/password, and if cancel then exit applciation.
0
 
finance_teacherAuthor Commented:
this.Hide(); works good
0
 
magicdlfCommented:
Not sure if this will help. Just take a look. Good luck
http://bytes.com/topic/c-sharp/answers/449265-self-restarting-application
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now