2个文本框对应一个密码字段 [英] 2 textbox for one password field

查看:59
本文介绍了2个文本框对应一个密码字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows窗体中有两个文本框.我可以检查密码是否相同吗?无需单击提交"按钮或弹出窗口.
我可以在regex

i have two textbox in my windows form. can i check the password that they are same? without clicking on the submit button or getting a popup.?
can i do that thing in regex

推荐答案

中执行此操作吗?假设您有一个带有两个TextBox,TextBox1和TextBox2的窗体:您希望两个TextBox在其中显示初始消息纯文本,例如第一个显示:输入密码",第二个显示:重新输入密码".

当用户使任一文本框具有焦点"并开始键入时,您希望初始的纯文本消息消失,然后用户键入的任何内容都将显示为带掩码"的密码字符,例如: *"

当用户在任一文本框中单击"Enter"键时,您要比较其内容:如果匹配,则完成;如果它们不匹配,则要将两个文本框重置为其初始状态.

注意:这两个TextBoxes都有两种获取焦点的方法:一种是通过用户在其中一个鼠标内部单击鼠标(MouseDown),另一种是通过用户单击TabKey使其成为活动控件,拥有焦点.

现在,让我们看一些代码:
Assuming you have a Form with two TextBoxes on it, textBox1, and textBox2: you want the two TextBoxes to display initial messages in plain-text, like the first one to display: "Enter Password," and the second one to display: "Re-Enter Password."

When the user causes either one of the TextBoxes to have ''Focus, and begins typing, you wish the initial plain-text message to disappear, and then whatever the user types will appear as your "masked" password-character, like: "*"

When the user hits the "Enter" key in either TextBox, you want to compare their contents: if they match, you are done; if they don''t match, you want to reset the two TextBoxes to their initial state.

Note: there are two ways for either of the TextBoxes to get ''Focus: one is by the user clicking the mouse inside one of them (MouseDown), and the other is by the user hitting the TabKey to make them the active control that has ''Focus.

Now, let''s look at some code:
// for convenience
private const char EnterChar = (char) Keys.Enter;
private const char EmptyChar = '\0';
private const char MaskChar = '*';

// the default plain-text content
private string text1 = "Enter Password";
private string text2 = "Re-Enter Password";

// keep track of whether each TextBox
// is showing plain-text, or is showing
// each key-stroke entered as the "mask"
// these 'flags' are used to make sure we don't
// clear the TextBoxes more than once in
// "password-entry mode"
private bool tb1Virgin = true;
private bool tb2Virgin = true;

private void Form1Load(object sender, EventArgs e)
{
    // not really necessary, just a habit to do this
    textBox1.Text = text1;
    textBox2.Text = text2;
    textBox1.PasswordChar = EmptyChar;
    textBox2.PasswordChar = EmptyChar;
}

// this will work if you remove these MouseDown EventHandlers
// but I like the idea of auto-clearing the plain-text when the
// user clicks inside the TextBoxes: rather than letting them
// click anywhere in the plain-text and start typing: then clear

private void TextBox1MouseDown(object sender, MouseEventArgs e)
{
    if (textBox1.PasswordChar == EmptyChar)
    {
        textBox1.Clear();
        textBox1.PasswordChar = MaskChar;
        tb1Virgin = false;
    }
}

private void TextBox2MouseDown(object sender, MouseEventArgs e)
{
    if (textBox2.PasswordChar == EmptyChar)
    {
        textBox2.Clear();
        textBox2.PasswordChar = MaskChar;
        tb2Virgin = false;
    }
}

// clean-up and reset if there's no match
private void PasswordTextBoxesReset()
{
    MessageBox.Show("No Match: try again");
    textBox1.PasswordChar = EmptyChar;
    textBox1.Text = text1;
    tb1Virgin = true;
    textBox2.PasswordChar = EmptyChar;
    textBox2.Text = text2;
    tb2Virgin = true;
}

// get an Enter/Return character: check for a match
private void TextBox1KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == EnterChar)
    {
        if (textBox1.Text != textBox2.Text)
        {
            PasswordTextBoxesReset();
        }
        else
        {
            MessageBox.Show("Password Match = " + textBox1.Text);
        }
    }
    else
    {
        // do we need to clear the plain-text ?
        if (tb1Virgin)
        {
            textBox1.Clear();
            textBox1.PasswordChar = MaskChar;
            tb1Virgin = false;
        }
    }
}

// get an Enter/Return character: check for a match
private void TextBox2KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == EnterChar)
    {
        if (textBox1.Text != textBox2.Text)
        {
            PasswordTextBoxesReset();
        }
        else
        {
            MessageBox.Show("Password Match = " + textBox1.Text);
        }
    }
    else
    {
        // do we need to clear the plain-text ?
        if (tb2Virgin)
        {
            textBox2.Clear();
            textBox2.PasswordChar = MaskChar;
            tb2Virgin = false;
        }
    }
}

注意:

0.请注意,将TextBox的PasswordChar属性设置为"\ 0"具有使TextBox中键入的所有内容均为纯文本的效果.

1.两个文本框的"MultiLine"属性设置为"false".请记住,TextBoxes的"AcceptsReturn"属性的值在这里无关紧要,因为它们不是"MultiLine TextBoxes".并且请注意,.NET中的"Enter"和"Return"是相同的.

2.您当然可以重构该代码,并使textBox1和textBox2共享相同的''MouseDown和''KeyPress EventHandlers,但是,出于教育目的,每个TextBox都有自己的EventHandlers.而且,对于一个简单的问题:为什么要麻烦重构?

3.显然,当存在匹配项"时,您可能想要隐藏TextBoxes,或切换到其他某种Form,等等.

4.当然,用标签修饰"两个文本框或使用工具提示来给用户信息(例如必须输入的最少字符数)是很容易的,无论是否必须输入至少一个数字,等等.其他不错的方法是:如果匹配失败,则在不符合您的验证标准的情况下,根据用户输入的内容分析,向用户提供适当的反馈,例如:您的密码必须至少包含一个数字,"等等.

Notes:

0. Note that setting the PasswordChar property of a TextBox to ''\0'' has the effect of making everything typed in the TextBox plain-text.

1. The two TextBoxes have the ''MultiLine property set to ''false. Remember that the value of the ''AcceptsReturn property of the TextBoxes doesn''t matter here, because they are not ''MultiLine TextBoxes. And, note, that "Enter" and "Return" are the same in .NET.

2. You could certainly re-factor this code, and make both textBox1 and textBox2 share the same ''MouseDown, and ''KeyPress, EventHandlers, but, for educational purposes here, each TextBox has its own EventHandlers. And, for something this simple: why bother to re-factor ?

3. Obviously, when there is a "match," you''ll probably want to hide the TextBoxes, or switch to some other Form, or whatever.

4. It would be easy, of course, to "decorate" the two TextBoxes with either Labels, or use a Tool-Tip, to give the user information like the minimum number of characters to enter, whether they must enter at least one number, etc. Other nice touches are: if a match fails, give the user some appropriate feedback based on analysis of what they entered if it doesn''t meet your validation criteria, like: "your password must contain at least one number," etc.


我可以检查密码是否相同
是的

我可以在正则表达式中做这件事
是的.但是,您不需要这样做,因为可以进行直接比较,并且可以进行比较.您要的是Winforms,没有客户端服务器在其他地方进行检查.可以在后面的代码中引发事件以进行比较.

试试:
1.在第二个文本框上放置一个onlostfocus事件
2.在这种情况下,直接比较密码本身(不需要正则表达式)
3.根据比较结果显示消息框或消息文本

参考: MSDN:Control.OnLostFocus方法 [ ^ ]
示例: MSDN:Control.LostFocus事件 [ ^ ]
can i check the password that they are same
Yes

can i do that thing in regex
Yes. But, you don''t need to, since a direct comparison is possible and will do. You are asking for Winforms, there is no client-server different place to check for it. Event can be raised in code behind for the comparison.

Try:
1. Put a onlostfocus event on the second textbox
2. In the event, compare the passwords directly itself (regex not required)
3. Based on the comparison result display messagebox or a message text

Refer: MSDN: Control.OnLostFocus Method[^]
Example: MSDN: Control.LostFocus Event[^]


这篇关于2个文本框对应一个密码字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆