2016-08-02 185 views
0

我的教師給了我和我的同學三項活動,這些活動是使用註冊表單做一個簡單的登錄表單而不使用數據庫(很明顯,我們需要做這個活動與數據庫)繼續.....之前不使用數據庫的登錄表單和註冊表格

這裏是代碼: Form1中:

public partial class Form1 : Form 
    { 
     string Username; 
     string Password; 
     string NAME; 
     string Age; 

     Form2 Frm = new Form2(); 
//Here is where you get the value of the String from Form2 
     public void PassValue(string strValue) 
     { 
      Username = strValue; 
     } 
     public void PassAnotherValue(string strValue2) 
     { 
      Password = strValue2; 
     } 
     public void PassAnotherValueAgain(string strValue3) 
     { 
      NAME = strValue3; 
     } 
     public void PassAnotherValueAgainAndAgain(string strvalue4) 
     { 
      Age = strvalue4; 
     } 
//------------------------------------------------------------------ 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void LoginBtn_Click(object sender, EventArgs e) 
     { 
      if (string.IsNullOrWhiteSpace(LoginUserNameTB.Text))  
      {  
       MessageBox.Show("Please input proper Username...!"); 
      } 
      if (string.IsNullOrWhiteSpace(LoginPasswordTB.Text))  
      {  
       MessageBox.Show("Please input proper Password...!"); 
      }  
      else if ((LoginUserNameTB.Text != Username) && (LoginPasswordTB.Text != Password)) 
      { 
       MessageBox.Show("Welcome" + NAME + "!");  
      } 
      else if ((LoginUserNameTB.Text == Username) && (LoginPasswordTB.Text == Password)) 
      { 
       MessageBox.Show("Please input proper Username and/or Password...!"); 
      } 
     } 

     private void RegisterBtn1_Click(object sender, EventArgs e) 
     { 
      Frm.Show(); 
     } 
    } 
} 

窗體2:

//Form2 has four textboxes, four labels, and a button 
      private void RegisterBtn2_Click(object sender, EventArgs e) 
      { 
       Form1 obj1 = new Form1(); 
       Form1 obj2 = new Form1(); 
       Form1 obj3 = new Form1(); 
       Form1 obj4 = new Form1(); 
       Form1 obj5 = new Form1(); 

     //This is where you pass the String value back to Form1 
        obj1.PassValue(RegUserNameTB.Text); 
        obj2.PassAnotherValue(RegPasswordTB.Text); 
        obj3.PassAnotherValueAgain(NTB.Text);  
        obj4.PassAnotherValueAgainAndAgain(ATB.Text); 


       if (string.IsNullOrWhiteSpace(NTB.Text) && string.IsNullOrWhiteSpace(ATB.Text) && string.IsNullOrWhiteSpace(RegUserNameTB.Text) && string.IsNullOrWhiteSpace(RegPasswordTB.Text)) 
        { 
         MessageBox.Show("Please enter the following:" + "\n" + "Name" + "\n" + "Age" + "\n" + "\n" + "UserName" + "\n" + "Password"); 
        } 
       Close(); 
      } 
     } 
    } 

現在到這個程序的問題..... 該程序工作得很好,每次我輸入用戶名和密碼它的工作,但'名稱'的價值是缺失的,每次我點擊註冊按鈕它只會執行一次動作,而不會再次執行(可能需要一個例外)....並且總結起來,我們的教師告訴我們用戶將有3個輸入用戶名和密碼的限制,然後程序將關閉....任何想法?

+3

你爲什麼要創建5'Form1中()'對象的用戶相匹配? – FrankerZ

回答

2

我可以看到,您正試圖在每次調用註冊表單(Form2)時存儲與用戶相關的一些信息。

這些屬性:

string Username; 
    string Password; 
    string NAME; 
    string Age; 

應存放在一個類不能在窗體(Form1在你的代碼)。 我建議做一個簡單的類有以下字段:

class User 
{ 
    // make those fields public for accessibility 
    public string Username; 
    public string Password; 
    public string NAME; 
    public string Age; 

    public User(string Username, string Password, string NAME, string Age) 
    { 
     // assign each fields with arguments from constructor 
     this.Username = Username; 
     this.Password = Password; 
     this.NAME = NAME; 
     this.Age = Age; 
    } 
} 

沒有數據庫,讓我們創建一個表來存儲每個「用戶」

class User 
{ 
    // Dont forget to add // using System.Collections.Generic; 
    // on top of the file otherwise List<> would not be available 
    public static List<User> UserList = new List<User>(); 

    // make those fields public for accessibility 
    public string Username; 
    public string Password; 
    public string NAME; 
    public string Age; 

    public User(string Username, string Password, string NAME, string Age) 
    { 
     // assign each fields with arguments from constructor 
     this.Username = Username; 
     this.Password = Password; 
     this.NAME = NAME; 
     this.Age = Age; 
    } 
} 

爲新用戶添加到UserList的,作出新的方法來處理它了我們的用戶類中

class User 
{ 
    // Dont forget to add // using System.Collections.Generic; 
    // on top of the file otherwise List<> would not be available 
    public static List<User> UserList = new List<User>(); 

    // make those fields public for accessibility 
    public string Username; 
    public string Password; 
    public string NAME; 
    public string Age; 

    public User(string Username, string Password, string NAME, string Age) 
    { 
     // assign each fields with arguments from constructor 
     this.Username = Username; 
     this.Password = Password; 
     this.NAME = NAME; 
     this.Age = Age; 
    } 

    public static void AddUserToList(User user) 
    { 
     UserList.Add(user); 
    } 
} 

每當一個用戶要註冊,您可以處理按鈕點擊事件是這樣的:

private void RegisterBtn2_Click(object sender, EventArgs e) 
    { 

     // check for valid input first 
     if (string.IsNullOrWhiteSpace(NTB.Text) && string.IsNullOrWhiteSpace(ATB.Text) && string.IsNullOrWhiteSpace(RegUserNameTB.Text) && string.IsNullOrWhiteSpace(RegPasswordTB.Text)) 
     { 
      MessageBox.Show("Please enter the following:" + "\n" + "Name" + "\n" + "Age" + "\n" + "\n" + "UserName" + "\n" + "Password"); 
     } 
     else 
     { 
      User user = new User(RegUserNameTB.Text, RegPasswordTB.Text, NTB.Text, ATB.Text); 
      User.AddUserToList(user); 
     } 
     Close(); 
    } 

現在,你可以通過實現通過迭代的UserList你的「登錄」的邏輯,檢查用戶輸入列表中的