2015-11-13 45 views
-1

我正在完成一项任务并需要该流程下一步的帮助。我了解如何创建用户对象,但我不完全理解这些问题。我只是在寻求指导,所以我可以随时学习。一旦表单成功提交,他们要求我创建一个用户对象。这是否意味着我需要创建一个新的用户类并将所有数据成员,属性和方法放在那里?还是我留在静态的主要void类中并在那里编码?以下是我到目前为止的问题和代码。我将发布整个项目,但突出显示我所处的区域。 5号和6号是我坚持的地方。提交表单后,如何创建用户对象

  1. 创建用户注册系统。
  2. 它将需要包含屏幕截图中显示的字段。代码将与其交互的表单控件需要将其名称更改为更有意义的内容。不要将textbox1保留为textbox1。
  3. 密码和确认密码字段需要相互匹配,无论是页面提交时还是确认密码字段失去焦点时。如果表单无效,则页面将不会按照以下说明处理。密码字段需要屏蔽密码。
  4. 主要是一个组合框(DropDownList风格),将包含您选择的专业(至少5)的列表。根据选择的主要内容,您将在专业下方的专业下拉列表中填写项目,供用户从这些项目中进行选择。
  5. **一旦表单提交成功,我们将使用在表单中的值来创建用户对象将由私有数据成员加属性的用户对象
  6. ,所有的字段将是字符串,用主要例外,这将是一个枚举。**
  7. 清除按钮将重置窗体到其初始空状态。 负载最后一个按钮将重新填充与该创建的最后一个用户对象的形式,如果没有用户对象已经创建,显示出与指示有错误消息的消息框没有数据加载
namespace Forms 
{ 
public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 
    private void passwordTxt_TextChanged(object sender, EventArgs e) 
    { 
     passwordTxt.PasswordChar = 'x'; 
     passwordTxt.MaxLength = 11; 
    } 

    private void confPassTxt_TextChanged(object sender, EventArgs e) 
    { 
     confPassTxt.PasswordChar = 'x'; 
     confPassTxt.MaxLength = 11; 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     majorBox.Items.Add(""); 
     majorBox.Items.Add("Math"); 
     majorBox.Items.Add("Science"); 
     majorBox.Items.Add("English"); 
     majorBox.Items.Add("Philosophy"); 
     majorBox.Items.Add("History"); 
    } 

    private void submitBtn_Click(object sender, EventArgs e) 
    { 
     if (string.IsNullOrEmpty(firstNameTxt.Text) || string.IsNullOrEmpty(lastNameTxt.Text) 
      || string.IsNullOrEmpty(userNameTxt.Text) || 
      string.IsNullOrEmpty(passwordTxt.Text) || string.IsNullOrEmpty(confPassTxt.Text) 
      || string.IsNullOrEmpty(majorBox.Text) || string.IsNullOrEmpty(specialtyBox.Text)) 
     { 
      MessageBox.Show("You must enter in all fields before moving forward"); 
     } 
    } 

    private void majorBox_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     if (majorBox.SelectedItem.ToString() == "Math") 
     { 
      specialtyBox.Items.Clear(); 
      specialtyBox.Items.Add("Calculus"); 
      specialtyBox.Items.Add("Statistics"); 
     } 
     else if (majorBox.SelectedItem.ToString() == "Science") 
     { 
      specialtyBox.Items.Clear(); 
      specialtyBox.Items.Add("Biology"); 
      specialtyBox.Items.Add("Chemestry"); 
     } 
     else if (majorBox.SelectedItem.ToString() == "English") 
     { 
      specialtyBox.Items.Clear(); 
      specialtyBox.Items.Add("18th Centruy"); 
      specialtyBox.Items.Add("Teacher"); 
     } 
     else if (majorBox.SelectedItem.ToString() == "Philosophy") 
     { 
      specialtyBox.Items.Clear(); 
      specialtyBox.Items.Add("Aristotal"); 
      specialtyBox.Items.Add("Socrates"); 
     } 
     else 
     { 
      specialtyBox.Items.Clear(); 
      specialtyBox.Items.Add("Peace"); 
      specialtyBox.Items.Add("War"); 
     } 
    } 

    private void confPassTxt_Validating(object sender, CancelEventArgs e) 
    { 
     if (confPassTxt.Text != passwordTxt.Text) 
     { 
      wrongPass.SetError(confPassTxt, "Worng password"); 
     } 
     else 
     { 
      wrongPass.SetError(confPassTxt, ""); 
     } 
    } 
} 
} 
+3

您应该一次只询问一个问题,添加您尝试的内容,以及为什么它不起作用。 –

+0

我只是不清楚我是否需​​要创建另一个班级?如果我能得到这个问题的答案,我可以继续前进,完成任务,并发布我所做的工作,如果失败了。你的帮助表示赞赏。 @ B.ClayShannon –

+0

你应该创建一个注册类,还有'屏幕截图'我们在这里看不到它...... – MethodMan

回答

0

你只需要定义一个用户类:

class User { 
    private int id; 
    private string name; 
    private string job; 
    ... 
} 

,然后在submitBtn_Click方法后,一些验证刚刚创建User类的新实例:

private void submitBtn_Click(object sender, EventArgs e) 
    { 
     if (string.IsNullOrEmpty(firstNameTxt.Text) || string.IsNullOrEmpty(lastNameTxt.Text) 
      || string.IsNullOrEmpty(userNameTxt.Text) || 
      string.IsNullOrEmpty(passwordTxt.Text) || string.IsNullOrEmpty(confPassTxt.Text) 
      || string.IsNullOrEmpty(majorBox.Text) || string.IsNullOrEmpty(specialtyBox.Text)) 
     { 
      MessageBox.Show("You must enter in all fields before moving forward"); 

     } else { 
      User userObj = new User(); 
     } 
} 

你也应该在你的用户类中放入一个构造函数来填充User类成员。

编辑:

好吧,你应该删除你的类定义private string confPassword;线,因为它只是一个验证领域。此外,主要字段应该是数字,因为主要的id应该存储在数据库中。

class Members 
{ 
    private string firstName; 
    private string lastName; 
    private string userName; 
    private string password; 
    private int major; 
    private int specialty; 

    public Members(string firstName, string lastName, string userName,string password, int major, int specialty) 
    { 
     this.firstName = firstName; 
     this.lastName = lastName; 
     this.userName = userName; 
     this.password = password; 
     this.major = major; 
     this.specialty = specialty; 
    } 
} 

那么你应该在你的Form类中的Submit_Click方法下有这样的东西。

else 
     { 
      Members user = new Members(firstNameStrVal, lastNameStrVal, userNameStrVal, passwordStrVal, majorIntVal, specialtyIntVal); 
      .... 
      your database insert will be here 
     } 

现在用户对象包含表单数据并准备好进行数据库事务。