检查以确保两个文本框匹配&显示标签,如果他们这样做

时间:2015-01-18 05:43:11

标签: c# winforms validation textbox

所以我试图检查两个文本框以确保它们彼此匹配,然后在标签中我想说“密码匹配”或“密码不匹配”。

好吧,我的大部分工作都在工作,但如果两个文本框中都没有任何内容,我希望标签可见。无论我尝试什么,当两个文本框都为空时,我会继续获得“密码匹配”。

总而言之,用户将密码堆栈输入到两个文本框中,标签应显示为“密码匹配”,但如果用户从文本框中删除了两个密码,我希望标签消失。我想要消失的标签叫做'lblPWCountAgain',文本框叫'txtPassword'和'txtPasswordAgain'

但是在密码框下我设置了它,告诉用户他们在文本框中输入了多少字符。此标签仅显示用户何时关注文本框,以便在他们聚焦之前不可见。 “密码匹配”和“密码不匹配”标签的设置方式相同。如果用户在两个文本框中输入相同的密码,背景颜色将变为绿色,如果输入的密码不匹配,则会变为红色。

所以我通过执行以下操作将textboxes TextChanged事件设置为'textbox_TextChangedCompare':

txtPassword.TextChanged += textbox_TextChangedCompare;
txtPasswordAgain.TextChanged += textbox_TextChangedCompare;

在textbox_TextChangedCompare中我有:

string pw = txtPassword.Text;
string pwa = txtPasswordAgain.Text;

if (pw == pwa)
{
  lblPWCountAgain.Visible=true;
  lblPasswordCount.Text = "Passwords Match";
  lblPWCountAgain.Text = "Passwords Match";
}

else if (string.IsNullOrEmpty(pw) && string.IsNullOrEmpty(pwa))
{
  lblPWCountAgain.Visible=false;
}

else
{
  lblPWCountAgain.Visible = true;
  lblPWCountAgain.text = "Passwords do not match!";
  var passw = txtPassword.MaxLength - txtPassword.Text.Length;
  lblPasswordCount.Text = passw.ToString();
}

// I also just tried to use this as well
if (pw == "" && pwa == "")
{
  lblPWCountAgain.Visible = false;

  var passw = txtPassword.MaxLength - txtPassword.Text.Length;
  lblPasswordCount.Text = passw.ToString();
}

这是聚焦的代码:

var password = txtPassword.MaxLength - txtPassword.Text.Length;

if (txtPassword.Focused)
{
  lblPasswordCount.Visible = true;
  lblPasswordCount.Text = password.ToString() + " Characters remaining";
}

else
{
  lblPasswordCount.Visibe = false;
}

因此,对于背景颜色变化,我这样做了:

// I set the KeyUp event to textbox_Compare:
txtPassword.KeyUp += textbox_Compare;
txtPasswordAgain.KeyUp += textbox_Compare;

private void textbox_Compare(object sender, KeyEventArgs e)
{
  Color bgColor = new Color();

  if (txtPassword.Text != txtPasswordAgain.Text)
  {
  bgColor = Color.Red;
  }
  else
  {
  lblPWCountAgain.Visible = true;
  bgColor = Color.LightGreen;
  }

  if (txtPassword.Text == String.Empty && txtPasswordAgain.Text == String.Empty)
  {
    bgColor = SystemColors.ControlLightLight // This is the background color of the textbox by default
  }

  txtPassword.BackColor = bgColor;
  txtPasswordAgain.BackColor = bgColor;
}

我不确定我是否只是在代码中重复自己或者是什么,但我无法弄明白。它可能不是最好的代码,但我想尽可能多地学习它!

感谢帮助人员

2 个答案:

答案 0 :(得分:0)

  

好吧,我的大部分都在工作,但如果两个文本框都没有   其中的任何东西我都希望标签可见。无论我尝试什么   我一直在"密码匹配"当两个文本框都为空时。

我相信这是你发言的顺序。第一个条件是在语句检查空字符串之前评估为true。尝试下面的解决方案,我在检查文本框是否相等之前检查空字符串。

    string pw = txtPassword.Text;
    string pwa = txtPasswordAgain.Text;

    if (string.IsNullOrEmpty(pw) && string.IsNullOrEmpty(pwa))
    {
      lblPWCountAgain.Visible=false;
//whicheverLabelIsShowing.Text = "Whatever text you want showing";
    }    
    else if (pw == pwa)
    {
      lblPWCountAgain.Visible=true;
      lblPasswordCount.Text = "Passwords Match";
      lblPWCountAgain.Text = "Passwords Match";
    } 
    else
    {
      lblPWCountAgain.Visible = true;
      lblPWCountAgain.text = "Passwords do not match!";
      var passw = txtPassword.MaxLength - txtPassword.Text.Length;
      lblPasswordCount.Text = passw.ToString();
    }

// I also just tried to use this as well
if (pw == "" && pwa == "")
{
  lblPWCountAgain.Visible = false;

  var passw = txtPassword.MaxLength - txtPassword.Text.Length;
  lblPasswordCount.Text = passw.ToString();
}

答案 1 :(得分:0)

您真的需要在 KeyUp 事件接收器中设置背景颜色吗? 除非您有理由这样做,否则请将其包含在 TextChanged 事件接收器中调用的代码中。 它使代码简单(即不易出错) 试试这个:

private void ComparePasswordsAndSetControlsAccordingly() {

        Color backgroundColor = SystemColors.ControlLightLight;

        if (txtPassword.Text.Length == 0 && txtPasswordAgain.Text.Length == 0) {
            // both are empty

            lblPWCountAgain.Visible = false;

            // background color is default

            lblPasswordCount.Text = "???"; // set the text to be displayed
            lblPWCountAgain.Text = "???";  // set the text to be displayed ( or leave it out : it's not visible anyway )
        }
        else if (txtPassword.Text == txtPasswordAgain.Text) {
            // they match

            lblPWCountAgain.Visible = true;
            lblPasswordCount.Text = "Passwords Match";
            lblPWCountAgain.Text = "Passwords Match";

            backgroundColor = Color.LightGreen;
        }
        else {
            // they do not match ( one can be empty, but not both )

            lblPWCountAgain.Visible = true;
            lblPWCountAgain.Text = "Passwords do not match!";
            lblPasswordCount.Text = "???"; // set the text to be displayed
            DisplayCharactersRemaining();

            backgroundColor = Color.Red;
        }

        txtPassword.BackColor = backgroundColor;
        txtPasswordAgain.BackColor = backgroundColor;
    }  

只需在 textbox_TextChangedCompare 方法中调用它即可 您可能也希望在表单的构造函数中调用它。

这是 DisplayCharactersRemaining 方法(在聚焦代码中调用它)

private void DisplayCharactersRemaining() {
        lblPasswordCount.Text =
            (txtPassword.MaxLength - txtPassword.Text.Length).ToString() + " Characters remaining";
    }  

我删除了临时变量并重命名了 bgColor 变量 IMO,如果你有一个 lblPasswordCount 标签,你应该有一个 lblPasswordCountAgain 标签
匹配,它更直观,它与 txtPassword txtPasswordAgain 匹配。
然而,由于它们显示剩余字符的数量或有关密码匹配的信息,因此它们可能应该具有不同的名称。
此外(但它可能只是个人品味的问题)我会改变前景色而不是背景色:我发现红色背景颜色的文本框有点难以阅读。