繁体   English   中英

自定义密码文本框C#Windows应用程序

[英]Custom Password textbox C# Windows Application

我想在C#应用程序中实现自定义密码文本框。 默认情况下,.NET为我提供了这些属性。

 - PasswordChar
 - UseSystemPasswordChar

然而,上述选项对我不起作用,因为我想实现一个自定义密码文本框,这将允许我输入字母数字字符,但行为方式类似于Android文本框控件。 我必须在使用“ asterix ”或任何其他密码字符进行屏蔽之前显示输入的字符几毫秒

为了实现这一点,我现在正在处理这种方式,但我认为这不是最佳解决方案。

private void txtPassword_TextChanged(object sender, EventArgs e)
    {

        if (timer == null)
        {
            timer = new System.Threading.Timer(timerCallback, null, 500, 150);
        }
        int num = this.txtPassword.Text.Count();
        if (num > 1)
        {
            StringBuilder s = new StringBuilder(this.txtPassword.Text);
            s[num - 2] = '*';
            this.txtPassword.Text = s.ToString();
            this.txtPassword.SelectionStart = num;
        }
    }

    public void Do(object state)
    {
        int num = this.txtPassword.Text.Count();
        if (num > 0)
        {
            StringBuilder s = new StringBuilder(this.txtPassword.Text);

            s[num - 1] = '*';
            this.Invoke(new Action(() =>
            {
                this.txtPassword.Text = s.ToString();
                this.txtPassword.SelectionStart = this.txtPassword.Text.Count();
                if (timer != null)
                {
                    timer.Dispose();
                }
                timer = null;
            }));
        }
    }

在构造函数中调用此片段

timerCallback = new TimerCallback(Do);

非常确定这可以更容易或更有效地完成。 任何投入高度赞赏。

谢谢VATSAG

首先,实现自定义安全控件通常不是一个好主意,除非你100%知道你在做什么。 如果您使用的是WPF,那么您可以通过PasswordBox控件的密码显示按钮预览密码。

您现有的代码可以通过多种方式进行改进,最明显的是:

  1. 更换

    if(str.Length == 1)...

((TextBox)sender).Text=new string('*', str.Length);
  1. 使用Environment.NewLine而不是'\\ n'。 (为什么你需要将它存储在密码数组中?)

  2. 在事件处理程序中使用boolean变量来禁用它的逻辑,而不是每次都附加\\ detaching事件处理程序。

  3. 使用Timer而不是使用Thread.Sleep()阻止UI线程

尝试这个。

第一个字符出现问题,在你输入下一个字符之前没有改变,但是我会留给你调试:);)

我没有使用System.Threading.Timer,而是使用了Windows.Forms.Timer

更简单:)

public partial class Form1 : Form
{
    Timer timer = null;

    public Form1()
    {
        InitializeComponent();
        timer = new Timer();
        timer.Interval = 250;
        timer.Tick += Timer_Tick;
    }

    private void Timer_Tick(object sender, EventArgs e)
    {
        timer.Enabled = false;

        int num = this.txtPassword.Text.Count();
        if (num > 0)
        {
            StringBuilder s = new StringBuilder(this.txtPassword.Text);

            s[num - 1] = '*';
            this.Invoke(new Action(() =>
            {
                this.txtPassword.Text = s.ToString();
                this.txtPassword.SelectionStart = this.txtPassword.Text.Count();
            }));
        }
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        int num = this.txtPassword.Text.Count();
        if (num > 1)
        {
            StringBuilder s = new StringBuilder(this.txtPassword.Text);
            s[num - 2] = '*';
            this.txtPassword.Text = s.ToString();
            this.txtPassword.SelectionStart = num;
            timer.Enabled = true;
        }
    }
}

暂无
暂无

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

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