简体   繁体   English

c#按钮上的密码

[英]Password on c# Button

I have a windows form i'm working on.我有一个正在处理的 Windows 窗体。 I have a button on a windows form if a user clicks the button another window appears with a textbox and a button asking you to put a password in. If the user types the passord correctly it will open a new form called AdminPage, however if the user types in the incorrect password a messagebox appears asking to try again.如果用户单击该按钮,我在 Windows 窗体上有一个按钮,另一个窗口会出现一个文本框和一个按钮,要求您输入密码。如果用户正确键入密码,它将打开一个名为 AdminPage 的新窗体,但是如果用户输入的密码不正确,会出现一个消息框,要求重试。 The problem is that i cant seem to get the AdminPage to open even if I type in the correct password.问题是,即使我输入了正确的密码,我似乎也无法打开 AdminPage。 If someone could guide me in my errors that would be great thanks如果有人能指导我解决我的错误,那就太好了,谢谢

 private void EnterBtn_Click(object sender, EventArgs e)
    {
        if (PsswdTxt.Text == ("BuildStore"))
        {
            AdminPage m = new AdminPage();
            m.Show();
            this.Close();
        }
          else

                MessageBox.Show("Please Try again");

        }
    }
}

Could it be that you're typing Buildstore or buildStore?可能是您正在输入 Buildstore 或 buildStore? The comparison is case sensitive - if you want to match the word irrespective of case you can use String.Compare or you could convert both values to upper/lower:比较区分大小写 - 如果您想匹配单词而不考虑大小写,您可以使用String.Compare或者您可以将两个值都转换为大/小:

if (string.Compare(PsswdTxt.Text,"BuildStore" ,true) == 0) // true signifies to ignore case
{
   AdminPage m = new AdminPage();
   m.Show();
   this.Visible = false;
}

or...或者...

   if (PsswdTxt.Text.ToUpper() == "BuildStore".ToUpper())
    {
       AdminPage m = new AdminPage();
       m.Show();
       this.Visible = false;
    }

I would create a Login" form for that. This; to control certain funtions for each user account. But "we" don't know what you are programming in there. 为此,我将创建一个“登录”表单。用于控制每个用户帐户的某些功能。但是“我们”不知道您在其中编程什么。

You can use a Login Form either when App Starts or when that button is clicked. 您可以在启动应用程序或单击该按钮时使用登录表单。 (Even by many other ways...) (甚至通过许多其他方式...)

Although if your password is always the same; 尽管您的密码始终相同; you can try this: 1) Create a "Login" form; 您可以尝试以下操作:1)创建一个“登录”表单; add 2 textBoxes and a button. 添加2个文本框和一个按钮。 2) Code it like this: 2)像这样编码:

public partial class Form1 : Form
{
    // Variable to create a new Form2
    Form2 f2 = new Form2();

    public Form1()
    {
        InitializeComponent();
    }

    // On Login Form Load...
    private void Form1_Load(object sender, EventArgs e)
    {
        btn_Login.Enabled = false;
    }

    // Login Button CLick
    private void btn_Login_Click(object sender, EventArgs e)
    {
        // replace "myUserName" with your User Name and "myPassword" with your password

            if (txtb_UserName.Text == "myUserName" && txtb_Password.Text == "myPassword")
            {
                // Shows the protected form
                f2.Show();
            }

                // If the Username or Password is not correct; A messageBox will be shows (or 2 if both are incorrect)
                // This can be perfected. But im just showing the basic examples.
            else if (txtb_UserName.Text != "myUserName")
            {
                // Sends an error message
                MessageBox.Show("Incorrect UserName");
            }

            else if (txtb_Password.Text != "myPassword")
            {
                // Sends an error message
                MessageBox.Show("Incorrect Password");
            }
        }

    // Add this code to enable or disable the login button. 
    // this will happen in both textBoxes (on TextChange Event).
    private void check_Text_Content()
    {
        if(txtb_UserName.Text != string.Empty && txtb_Password.Text != string.Empty)
        {
            btn_Login.Enabled = true;
        }

        else if (txtb_UserName.Text == string.Empty)
        {
            btn_Login.Enabled = false;
        }

        else if (txtb_Password.Text == string.Empty)
        {
            btn_Login.Enabled = false;
        }
    }

    // Now as you can see both textBoxes will check if their text is empty or not.
    // If it's empty; The login Buttton will be disabled. Else; will be Enabled.

    private void txtb_UserName_TextChanged(object sender, EventArgs e)
    {
        check_Text_Content();
    }

    private void txtb_Password_TextChanged(object sender, EventArgs e)
    {
        check_Text_Content();
    }
}

} }

If you want to customize your password once in a while i would use MS Access or SQL DataBase. 如果您想偶尔自定义密码,我会使用MS Access或SQL数据库。 (And a form to customize them). (以及用于自定义它们的表单)。 Not trying to discouraging you or something; 不劝阻您或其他事情; but if you don't know how to create and manage SQL / MS Access Databases; 但是,如果您不知道如何创建和管理SQL / MS Access数据库,则可以; i recommend you to search Google. 我建议您搜索Google。 There are tons of tutorials around the web. 网上有很多教程。 I'm not showing you how to do it with SQL or Access DB; 我没有向您展示如何使用SQL或Access DB进行操作。 because it's would be a huge step by step tutorial. 因为这将是一个巨大的分步教程。 Hope it works for you. 希望对你有效。

try this code..试试这个代码..

 private void EnterBtn_Click(object sender, EventArgs e)
    {
        if (string.Equals(PsswdTxt.Text,"BuildStore"))
        {
            AdminPage m = new AdminPage();
            m.Show();
            this.Visible = false;
        }
        else
        {
          MessageBox.Show("Please Try again");
        }        
    }

You should try ShowDialog instead of Show.您应该尝试 ShowDialog 而不是 Show。 It might be that it is shown behind the other form.可能是它显示在另一种形式的后面。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM