根据文本框长度条件启用按钮 c#

enabling button upon textbox length condition c#

我正在尝试根据 TxtPswrd 的长度大于 8 来启用 BtnSubmit。我正在使用 Textfield Validating 函数,我很困惑,我应该使用 Validating、Validated 还是 TextChanged 函数?

 private void BtnSignin_Click(object sender, EventArgs e)
    {
        // SqlConnection con = StartCon();
        foreach (Control c in this.Controls)
        {
            if (c is TextBox)
            {
                TextBox textBox = c as TextBox;
                if (textBox.Text != string.Empty)
                {
                    DataInserted(sender, e);
                }
                else
                {
                    MessageBox.Show("Fill All Fields", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    TxtID.Focus();
                    break;
                }
            }
        }

DataInserted 函数:

private void DataInserted(object sener, EventArgs e)
    {
        try
        {
            SqlConnection sqlConnection = StartCon();
            string AdminLookup = "Select count(*) from signin WHERE memid = '" + TxtID.Text + "' and adminpass='"+TxtPswrd.Text+"'";
            SqlCommand command = new SqlCommand(AdminLookup, sqlConnection);
            command.Parameters.Clear();
            command.Parameters.AddWithValue("@usr", TxtUsername.Text);
            command.Parameters.AddWithValue("@pwd", TxtPswrd.Text);
            
            if (command.ExecuteScalar().ToString() =="1")
            {
                SetValueForText1 =  TxtUsername.Text;
                new Thread(() => new PSP_Manager().ShowDialog()).Start();
                this.Close();
            }
            else
            {

                MessageBox.Show("Error Signing in", "Check Credentials", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        catch (SqlException sqlexception)
        {
            MessageBox.Show("Error", sqlexception.Message);
        }
        finally
        {
            StartCon().Close();
        }
    }

我试图在 TxtPswrd.length >= 8

时启用 BtnSubmit

求助。 pic

我会使用 TextChanged:

private void PasswordTextbox_TextChanged(object sender, EventArgs e){
  submitButton.Enabled = passwordTextbox.Text.Length >= 8;
}

因为我希望在他们仍然专注于框中时启用提交按钮,只要他们输入 8 个字符

在其他新闻中,不要这样写SQL命令;参见 http://Bobby-tables.com 了解原因..

..也不要在数据库中以明文形式存储密码;这样做是 http://haveibeenpwned.com 必须存在的原因