2017-02-18 43 views
0

喜evryone你可以幫我請..我有使用的Visual Studio 2015年社區我的textBox的名字是我的Windows窗體應用程序代碼用戶名另一種是密碼,最後是登錄如果用戶想要登錄,它會得到一棵樹次錯誤在這裏,我想它會自動顯示在忘記密碼的問題,如果用戶如何找回密碼當他或她登錄表單時出現樹時錯誤。之前我會去我的數據庫表格,我不知道你能幫忙請解釋一下。如何顯示的密碼恢復表單用戶輸入憑據無效後三次

這裏是我的代碼

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace SQLSERVER_VISUALSTUDIO_COMMUNITY 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
      txt_Password.PasswordChar = '*'; 
     } 

     private void txt_login_Click(object sender, EventArgs e) 
     { 
      if (txt_USername.Text == "" && txt_Password.Text == "") 
      { 
       MessageBox.Show("Please enter your password and user_name"); 
       txt_USername.Clear(); 
       txt_Password.Clear(); 
      } 
      else if (txt_USername.Text == "jondygonzales" && txt_Password.Text == "sharkwebcaster") 
      { 
       MessageBox.Show("successfully log_in"); 
       Form1 f = new Form1(); 
       f.Show(); 
       Form2 main = new Form2(); 
       main.Show(); 
       this.Hide(); 
      } 
     } 
    } 
} 
+0

你得到了什麼確切的錯誤代碼? –

+0

@AidenGrossman謝謝..我沒有錯誤,但我的觀點是我想顯示忘記的密碼,如果用戶在登錄窗體的樹時出錯。 – Sharkweb

回答

0

你需要做的是保持一個計數器,你會被一個時的用戶名和密碼是無效的增加。

您檢查此變量值,當它達到3時,顯示用戶密碼恢復表單。

public partial class Form1 : Form 
{ 
    int loginAttemps = 0; 
    public Form1() 
    { 
     InitializeComponent(); 
     txt_Password.PasswordChar = '*'; 
    } 

    private void txt_login_Click(object sender, EventArgs e) 
    { 
     if (txt_USername.Text == "" && txt_Password.Text == "") 
     { 
      MessageBox.Show("Please enter your password and user_name"); 
      txt_USername.Clear(); 
      txt_Password.Clear(); 
     } 
     else if (txt_USername.Text == "jondygonzales" && txt_Password.Text == "sharkwebcaster") 
     { 
      loginAttempts = 0; 
      MessageBox.Show("successfully log_in"); 
      Form1 f = new Form1(); 
      f.Show(); 
      Form2 main = new Form2(); 
      main.Show(); 
      this.Hide(); 
     } 
     else 
     { 
      loginAttempts += 1; 

      if(loginAttemps == 3) 
      { 
       RecoveryForm recForm = new RecoveryForm(); // You need to use correct Form here. 
       recForm.Show(); 
       this.Hide(); 
      } 
     } 
    } 
} 
+0

謝謝你的答案。所以只需要做的就是把一個條件,如果用戶名和密碼將屬於其他的 – Sharkweb

+0

我糾正了代碼的答案,使其更符合邏輯。除了引入新變量'loginAttempts'外,最後一個'else'塊是您需要添加的塊。 –

+0

loginAttempts + = 1的用法; – Sharkweb

相關問題