【发布时间】:2014-05-28 15:57:10
【问题描述】:
我有一个作业,我应该使用用户登录来制作一个会计应用程序。我的问题是,我必须使用 {get,set} 但我不知道如何正确使用它们。有一个名为“UserLogin”的类.cs' 和一种形式 'UserLoginForm.cs'。在 userLoginForm 中有两个 maskedTextBoxes : maskedTextBoxID 和 maskedTextBoxPass。
当我点击 userLoginForm 上的登录按钮时,它会从 userloginclass 中调用一个函数来检查 id 和 pass 是否正确?
问题是当我单击登录按钮时,来自 maskedTextBoxes 的 id 和 pass 不是我在单击登录之前输入的,它们始终是 maskedTextBoxes 的第一个值:空...
如果我输入 123 作为 id 并输入 123 作为 pass 那么如果我在函数中检查它们是否 id 为“123”并且 pass 为“123”,则会出现错误的 id&pass 消息。
这一次:如果我输入 123 作为 id 和 123 作为 pass ,那么如果在函数中检查它们是否 id 是 " " 并且 pass 是 " " (这是他们的第一个值)登录成功。
问题出在哪里?
在用户登录表单中
public partial class UserLoginForm : Form
{
public UserLoginForm()
{
InitializeComponent();
}
public string userID
{
get
{
return maskedTextBoxID.Text;
}
}
public string userPASS
{
get
{
return maskedTextBoxPass.Text;
}
}
private void button_enter_Click(object sender, EventArgs e)
{
UserLogin ulogcs = new UserLogin();
ulogcs.checklogin();
在用户登录类中:
public void checklogin()
{
UserLoginForm uform = new UserLoginForm();
if (uform.userID == "123" && uform.userPASS == "123")
{
MessageBox.Show("Welcome to the bank");
AccountForm aform = new AccountForm();
aform.Show();
}
else
{
MessageBox.Show("Error");
}
}
对不起语法(我不是母语)
【问题讨论】:
-
创建登录类的实例并将信息传递给它。或者使 checklogin 方法(它应该接受两个字符串参数)静态并直接调用它。
-
在
checklogin(),UserLoginForm uform = new UserLoginForm();中创建一个新表单。我不认为那是你想要的。 -
我向 checklogin() 函数发送字符串,正如您和其他 cmets 所说,我的问题已解决。感谢您提供有关创建新表单的帮助和额外信息。