簡體   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