繁体   English   中英

如何打开一个新表单然后从第二个表单 go 回到第一个表单而不关闭并重新打开第一个表单

[英]How to open a new form then from the second form go back to the first form without closing and reopening the first form

我正在尝试模拟登录到应用程序。 我想从登录表单打开,主表单(登录成功时)。 我在主窗体上有一个按钮,单击时必须冻结主窗体打开登录窗体,从登录窗体登录后解冻相同的主窗体

我给你举个例子:

简单的登录和验证。

重要代码:

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

主要 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();
    }
    }

在此处输入图像描述

登录 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='{ AccountTb.Text}' and password='{PassTb.Text}'";
try
    {
conn.Open();
SqlCommand sqlCommand = new SqlCommand(sql, conn);
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());
    }
    }

在此处输入图像描述

OutPut:

在此处输入图像描述

如果您对我的代码有任何疑问,请在下面发表评论。 更新问题,我会继续改进。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM