简体   繁体   English

为什么Windows窗体应用程序上的按钮不起作用?

[英]Why isn't the button on my Windows Form Application working?

So I'm making an ATM and the first thing I have to program is the login screen. 因此,我正在制作ATM机,首先要编程的是登录屏幕。 To define the users I created a User class which is formed by an id, username, password, savings account and checks account. 为了定义用户,我创建了一个User类,该类由ID,用户名,密码,储蓄帐户和支票帐户组成。 In my windows form I created two buttons, one executes the log in and the other one closes the program. 在Windows窗体中,我创建了两个按钮,一个按钮执行登录,另一个按钮关闭程序。 This is the code for my Windows Form: 这是我的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;
                    }

                }
            }
        }
    }
}

I put my objects inside a list and I use a for loop to traverse the list and I compare whatever the user inputs on the first text box to the username in the ith position and compare whatever the user inputs in the second textbox to the password in the ith position. 我将对象放在列表中,并使用for循环遍历该列表,并将用户在第一个文本框中输入的内容与第i个位置的用户名进行比较,并将用户在第二个文本框中输入的内容与中的密码进行比较。第i个位置。 If they match it should pop a message telling me it's correct. 如果它们匹配,则会弹出一条消息,告诉我它是正确的。 And if it's wrong it should tell me it's wrong and after three attempts it should stop working. 如果错了,它应该告诉我这是错的,并且在尝试了三遍之后,它应该停止工作。 I created a public void called verification where I do all that testing and I just call it inside the log in button. 我创建了一个称为验证的公共空白,我在其中进行所有测试,并且只在“登录”按钮内调用它。 However it's no working. 但是,这没有用。 When I type something into the text boxes and click the log in button it does nothing. 当我在文本框中输入内容并单击“登录”按钮时,它什么也没做。 However the exit button does work. 但是退出按钮确实起作用。 Any insight as to why this might be happening? 为什么会发生这种情况的任何见解? Is there something I could be forgetting? 有什么我可能会忘记的东西吗?

Looks like you're not adding anything to your users List variable... instead of: 似乎您没有向用户列表变量添加任何内容,而不是:

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();
}

try 尝试

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();
}

Then when you loop through your users List - users.Count will have a value. 然后,当您遍历用户列表时-users.Count将具有一个值。

It looks like you are defining new users but not adding them to the list. 您似乎正在定义新用户,但未将其添加到列表中。 So you are looping over 0 user. 因此,您正在遍历0个用户。

You could for example change Form1() to 例如,您可以将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();
        }

Also note that this.Close() only closes the windows, not the application. 还要注意,this.Close()仅关闭窗口,不关闭应用程序。 As explained in Winforms: Application.Exit vs Enviroment.Exit vs Form.Close Winforms中所述:Application.Exit与Enviroment.Exit与Form.Close

I think also the this version of verification might make more sense: 我认为此版本的验证也可能更有意义:

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();
            }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的Picturebox动画不能按预期方式工作(C#Windows Form应用程序)? - Why isn't my picturebox animation working as expected (C# Windows Form Application)? 我的Windows表单上的按钮不起作用。 Visual Studio在我的代码中未显示任何错误 - The button on my Windows form isn't working. Visual studio is not showing any errors in my code ASP.NET Web应用程序:为什么“复制到剪贴板”按钮不起作用? - ASP.NET Web Application: Why isn't my “Copy To Clipboard” button working? 为什么更改按钮内容不起作用 - Why isn't change button content working 为什么我的应用程序没有绘图? - Why isn't my application drawing anything? 为什么修改后的.NET Windows窗体在再次运行程序后没有显示新的更改? - Why isn't my revised .NET Windows Form not showing new changes after running the program again? 为什么我的NHibernate更新无法正常工作? - Why isn't my NHibernate update working? 为什么我的DoubleAnimation无法正常工作? - Why isn't my DoubleAnimation working? 为什么我的数据绑定不起作用? - Why isn't my databinding working? 为什么我的ReportViewer不能正常工作? - Why isn't my ReportViewer working?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM