Home > OS >  How to open a new form then from the second form go back to the first form without closing and reope
How to open a new form then from the second form go back to the first form without closing and reope

Time:12-11

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

CodePudding user response:

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.

  • Related