2016-08-02 99 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你的“登录”的逻辑,检查用户输入列表中的