为什么我的 Windows 表单应用程序上的按钮不起作用?
Why isn't the button on my Windows Form Application working?
所以我正在制作一台 ATM,我必须编程的第一件事就是登录屏幕。为了定义用户,我创建了一个用户 class,它由 ID、用户名、密码、储蓄账户和支票账户组成。在我的 windows 表单中,我创建了两个按钮,一个执行登录,另一个关闭程序。这是我的 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 ATM_Project
{
public partial class Form1 : Form
{
List<User> users = new List<User>();
int attempts = 3;
public Form1()
{
new User() { id = 0, userName = "user1", password = "password123", savingsAcct = 2000, checkAcct = 2500 };
new User() { id = 1, userName = "user2", password = "password234", savingsAcct = 3000, checkAcct = 4500 };
new User() { id = 2, userName = "user3", password = "pass345", savingsAcct = 3000, checkAcct = 5000 };
InitializeComponent();
}
private void exitbtn_Click(object sender, EventArgs e)
{
this.Close();// this button is used to close the application
}
private void loginbtn_Click(object sender, EventArgs e)
{
verification();
}
public void verification()
{
for (int i = 0; i < users.Count; i++)
{
while (attempts != 0)
{
if (textBox1.Text == users[i].userName && textBox2.Text == users[i].password) //checks that the username and the password match.
{
MessageBox.Show("Your password is correct!");
break;
}
else
{
MessageBox.Show("Error. Your username or password are incorrect!");
attempts = attempts - 1;
}
}
}
}
}
}
我将我的对象放在一个列表中,并使用 for 循环遍历该列表,然后比较用户在第一个文本框中输入的内容与第 ith 个位置的用户名,并比较用户在第二个文本框中输入的内容文本框到第 i 个位置的密码。如果它们匹配,它应该弹出一条消息告诉我它是正确的。如果它错了,它应该告诉我它错了,并且在尝试三次后它应该停止工作。我创建了一个名为 verification 的 public void,我在其中进行了所有测试,我只是在登录按钮中调用它。但是,这是行不通的。当我在文本框中输入内容并单击登录按钮时,它什么也没做。但是退出按钮确实有效。关于为什么会发生这种情况的任何见解?有什么我可能会忘记的吗?
您似乎在定义新用户,但并未将其添加到列表中。
所以你循环了 0 个用户。
例如,您可以将 Form1() 更改为
public Form1()
{
users.add(new User() { id = 0, userName = "user1", password = "password123", savingsAcct = 2000, checkAcct = 2500 });
users.add(new User() { id = 1, userName = "user2", password = "password234", savingsAcct = 3000, checkAcct = 4500 });
users.add(new User() { id = 2, userName = "user3", password = "pass345", savingsAcct = 3000, checkAcct = 5000 });
InitializeComponent();
}
另请注意,this.Close() 只会关闭 windows,不会关闭应用程序。如 Winforms: Application.Exit vs Enviroment.Exit vs Form.Close
中所述
我也认为这个版本的验证可能更有意义:
public void verification()
{
if (textBox1.Text == users[i].userName && textBox2.Text == users[i].password) //checks that the username and the password match.
{
MessageBox.Show("Your password is correct!");
}
else
{
MessageBox.Show("Error. Your username or password are incorrect!");
attempts -= 1;
}
if(attempts == 0)
{
Environment.Exit();
}
}
看起来您没有向用户列表变量中添加任何内容...而不是:
public Form1()
{
new User() { id = 0, userName = "user1", password = "password123", savingsAcct = 2000, checkAcct = 2500 };
new User() { id = 1, userName = "user2", password = "password234", savingsAcct = 3000, checkAcct = 4500 };
new User() { id = 2, userName = "user3", password = "pass345", savingsAcct = 3000, checkAcct = 5000 };
InitializeComponent();
}
尝试
public Form1()
{
users.Add (new User() { id = 0, userName = "user1", password = "password123", savingsAcct = 2000, checkAcct = 2500 });
users.Add (new User() { id = 1, userName = "user2", password = "password234", savingsAcct = 3000, checkAcct = 4500 });
users.Add (new User() { id = 2, userName = "user3", password = "pass345", savingsAcct = 3000, checkAcct = 5000 });
InitializeComponent();
}
然后当您遍历用户列表时 - users.Count 将有一个值。
所以我正在制作一台 ATM,我必须编程的第一件事就是登录屏幕。为了定义用户,我创建了一个用户 class,它由 ID、用户名、密码、储蓄账户和支票账户组成。在我的 windows 表单中,我创建了两个按钮,一个执行登录,另一个关闭程序。这是我的 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 ATM_Project
{
public partial class Form1 : Form
{
List<User> users = new List<User>();
int attempts = 3;
public Form1()
{
new User() { id = 0, userName = "user1", password = "password123", savingsAcct = 2000, checkAcct = 2500 };
new User() { id = 1, userName = "user2", password = "password234", savingsAcct = 3000, checkAcct = 4500 };
new User() { id = 2, userName = "user3", password = "pass345", savingsAcct = 3000, checkAcct = 5000 };
InitializeComponent();
}
private void exitbtn_Click(object sender, EventArgs e)
{
this.Close();// this button is used to close the application
}
private void loginbtn_Click(object sender, EventArgs e)
{
verification();
}
public void verification()
{
for (int i = 0; i < users.Count; i++)
{
while (attempts != 0)
{
if (textBox1.Text == users[i].userName && textBox2.Text == users[i].password) //checks that the username and the password match.
{
MessageBox.Show("Your password is correct!");
break;
}
else
{
MessageBox.Show("Error. Your username or password are incorrect!");
attempts = attempts - 1;
}
}
}
}
}
}
我将我的对象放在一个列表中,并使用 for 循环遍历该列表,然后比较用户在第一个文本框中输入的内容与第 ith 个位置的用户名,并比较用户在第二个文本框中输入的内容文本框到第 i 个位置的密码。如果它们匹配,它应该弹出一条消息告诉我它是正确的。如果它错了,它应该告诉我它错了,并且在尝试三次后它应该停止工作。我创建了一个名为 verification 的 public void,我在其中进行了所有测试,我只是在登录按钮中调用它。但是,这是行不通的。当我在文本框中输入内容并单击登录按钮时,它什么也没做。但是退出按钮确实有效。关于为什么会发生这种情况的任何见解?有什么我可能会忘记的吗?
您似乎在定义新用户,但并未将其添加到列表中。 所以你循环了 0 个用户。
例如,您可以将 Form1() 更改为
public Form1()
{
users.add(new User() { id = 0, userName = "user1", password = "password123", savingsAcct = 2000, checkAcct = 2500 });
users.add(new User() { id = 1, userName = "user2", password = "password234", savingsAcct = 3000, checkAcct = 4500 });
users.add(new User() { id = 2, userName = "user3", password = "pass345", savingsAcct = 3000, checkAcct = 5000 });
InitializeComponent();
}
另请注意,this.Close() 只会关闭 windows,不会关闭应用程序。如 Winforms: Application.Exit vs Enviroment.Exit vs Form.Close
中所述我也认为这个版本的验证可能更有意义:
public void verification()
{
if (textBox1.Text == users[i].userName && textBox2.Text == users[i].password) //checks that the username and the password match.
{
MessageBox.Show("Your password is correct!");
}
else
{
MessageBox.Show("Error. Your username or password are incorrect!");
attempts -= 1;
}
if(attempts == 0)
{
Environment.Exit();
}
}
看起来您没有向用户列表变量中添加任何内容...而不是:
public Form1()
{
new User() { id = 0, userName = "user1", password = "password123", savingsAcct = 2000, checkAcct = 2500 };
new User() { id = 1, userName = "user2", password = "password234", savingsAcct = 3000, checkAcct = 4500 };
new User() { id = 2, userName = "user3", password = "pass345", savingsAcct = 3000, checkAcct = 5000 };
InitializeComponent();
}
尝试
public Form1()
{
users.Add (new User() { id = 0, userName = "user1", password = "password123", savingsAcct = 2000, checkAcct = 2500 });
users.Add (new User() { id = 1, userName = "user2", password = "password234", savingsAcct = 3000, checkAcct = 4500 });
users.Add (new User() { id = 2, userName = "user3", password = "pass345", savingsAcct = 3000, checkAcct = 5000 });
InitializeComponent();
}
然后当您遍历用户列表时 - users.Count 将有一个值。