-1

I am trying to simulate a login to a application. I want to open from the Login Form, the main form(when the Login is successful). I have a button on the main form that when clicked must freeze the main form open the Login form and after loging from the Login form unfreeze the same main form

9
  • 1
    So you want the login prompt to be a model dialog? Dec 3, 2021 at 0:21
  • Sorry if I am missing something, however, I have to ask if you have thought this through. What do you mean by “freeze the main form”? And why is closing and reopening the form undesirable?
    – JohnG
    Dec 3, 2021 at 0:26
  • The following may be helpful - although it's written in VB.NET: stackoverflow.com/questions/69711574/… Dec 3, 2021 at 0:27
  • 1
    Instead of calling yourForm.Show call yourForm.ShowDialog(this)
    – Flydog57
    Dec 3, 2021 at 0:34
  • Sounds a bit chicken and egg. You said you want the logon form to open the main form which opens the login form.. think you need to hammer that one out a bit more
    – Caius Jard
    Dec 3, 2021 at 0:35

1 Answer 1

0

I will give you an example:

Simple login and verification.

Important code:

Login_in f2 = new Login_in(); //Create login window
this.Hide(); //Hide the main window
f2.ShowDialog(); //Show login window
this.Show(); //Show main window

Main window:

private void Button1_Click(object sender, EventArgs e) 
{
    Login_in f2 = new Login_in();

    while (true)
    {
        this.Hide();
        f2.ShowDialog();

        if (f2.DialogResult == DialogResult.OK)
        {
            this.Show();
            MessageBox.Show("Verification Success!");
            break;
        } 
        else if (f2.DialogResult == DialogResult.Cancel)
        {
            this.Show();
            MessageBox.Show("Verification Failed");
            break;
        }

        f2.Close();
    }
}

enter image description here

Login Window:

private void Button1_Click(object sender, EventArgs e) 
{
    //Personal test database
    string myconn = @"Data Source = (localdb)\MSSQLLocalDB; Initial Catalog = Test; Integrated Security = True";
    SqlConnection conn = new SqlConnection(myconn);
    string sql= $"select * from Test.dbo.demoAccount where userid=@UserId and password=@PassWord";
        try
        {
        conn.Open();
        SqlCommand sqlCommand = new SqlCommand(sql, conn);
        //sqlCommand.Parameters.AddWithValue("@UserId", AccountTb.Text.Trim())
        //sqlCommand.Parameters.AddWithValue("@PassWord", PassTb.Text.Trim());
        sqlCommand.Parameters.Add("@UserId", SqlDbType.VarChar, 8).Value = AccountTb.Text.Trim();
        sqlCommand.Parameters.Add("@PassWord", SqlDbType.Char, 8).Value = PassTb.Text.Trim();      
        SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
        if (sqlDataReader.HasRows) //Satisfy the user name and password are consistent, enter the next interface
        {
            this.DialogResult = DialogResult.OK;
        } 
        else
        {
            this.DialogResult = DialogResult.Cancel;
        }

        conn.Close();
    } 
    catch (Exception o)
    {
        MessageBox.Show(o.ToString());
    }
}

enter image description here

OutPut:

enter image description here

If you have questions about my code, please comment below. Update the problem, I will continue to improve.

Updated:

  1. Add a delegate and event in the child window.The similar code is as follows.Because it needs to pass a string, the delegate needs to take a string parameter.
 public delegate void MyAccountDelegate(string account);
 public event MyAccountDelegate MyAccountEvent;
  1. Set the event of the Login button in the child window to call the above event when it is clicked, and pass the string that needs to be passed to the event.
if (sqlDataReader.HasRows)//Satisfy the user name and password are consistent, enter the next interface
{
    this.DialogResult = DialogResult.OK;
    MyAccountEvent(AccountTb.Text);//Add account information
} else
{
    this.DialogResult = DialogResult.Cancel;
}
  1. Add a method to display the returned information in the main window, which is the same as participating in the MyAccountDelegate delegation.
private void DisplayMyAccount(string account) {
    listBox1.Items.Add(account);
}
  1. Add the following code to open the child window in the Login button of the parent window:
 f2.MyAccountEvent += new Login_in.MyAccountDelegate(DisplayMyAccount);
 //f2.MyAccountEvent += DisplayMyAccount;

Tip:The (closed) main window used here is a hidden method, it is not really closed, so it will not affect the main window information:

this.Hide();
this.show();

Output:

enter image description here

Change it yourself according to your needs, please check if I understand what you mean.

3
  • I'd also take a read of dbdelta.com/addwithvalue-is-evil - it's not so bad as sql injection but it's also worth avoiding AddWithValue on a SqlCommand (other DB don't necessarily have a problem with it, but SQLS does)
    – Caius Jard
    Dec 3, 2021 at 7:10
  • @JialeXue-MSFT I cant close the main form because I want the information saved on the listbox to stay after I login with a different account Dec 3, 2021 at 10:19
  • How cant i save the information from the listbox without using sql or text files Dec 5, 2021 at 0:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.