【问题标题】:Check to ensure both textboxes match & display a label saying if they do检查以确保两个文本框匹配并显示一个标签,说明它们是否匹配
【发布时间】:2015-01-18 05:43:10
【问题描述】:

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

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

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

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

所以我通过执行以下操作将文本框 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;
}

我不确定我只是在代码中重复自己还是什么,但我无法弄清楚。它可能不是最好的代码,但我正在尽可能多地学习它!

感谢大家的帮助

【问题讨论】:

    标签: c# winforms validation textbox


    【解决方案1】:

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

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

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

    【讨论】:

    • 它又在做同样的事情,这次密码文本框下的字符数保持不变,但这很容易解决。当文本框中没有任何内容时,它仍然会显示“密码匹配”,因为从技术上讲,它们确实如此!不确定我是否在代码中冗余并将其放在另一个地方并导致错误,我已经查看但我没有看到任何内容
    • 如果您调试条件,哪个条件的计算结果为真?当密码为空时,您似乎不会更新标签的文本,因此如果文本之前是“密码匹配”,它将在之后。查看我编辑的帖子。
    • 我不知道如何调试条件以确定哪个条件为真。但是,您刚刚显示的代码也不起作用...
    • 在您的解决方案中,按 F5 开始调试。使用 F9 在一行代码上设置断点。在每个“if”语句中放置一个断点,看看哪个被命中。
    • 我没有看到它在哪里告诉你它是否评估为真。在“Locals”窗口的底部,它说的是:this {PassLock.frmAddSite, Text: PassLock - Add Site} PassLock.frmAddSite sender {Text = "a} 注意:这是我输入到 TEXTBOX 对象中的文本 { System.Windows.Forms.TextBox} e {System.Event Args} System.EventArgs pw "a" string pwa "" string
    【解决方案2】:

    您真的需要在 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 标签
    要匹配,它只是更直观,它与 txtPasswordtxtPasswordAgain 匹配。
    然而,由于它们要么显示剩余字符数,要么显示有关密码匹配的信息,因此它们可能应该具有完全不同的名称。
    另外(但这可能只是个人喜好问题)我会更改前景色而不是背景色:我发现带有红色背景色的文本框有点难以阅读。

    【讨论】:

      猜你喜欢
      • 2012-11-16
      • 2018-05-14
      • 2021-11-01
      • 1970-01-01
      • 2021-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多