Home > Mobile >  Password Text entered should appear for half a second and then replaced by * (Masking)
Password Text entered should appear for half a second and then replaced by * (Masking)

Time:09-07

I have a question in WPF, We have a text box. Where the user enters a password or some confidential data.

At this point, we have overridden System.Windows.Controls.TextBox's OnTextInput event handler. We have overridden as the textbox should work normally and based on some conditions, the same textbox should work as a password text box. and I am replacing the character entered with * asterisk.

Now, we have this requirement where, the text entered should be visible for half a second and then replaced by *.

Can you kindly help me here, Thanks in Advance.

CodePudding user response:

First you need to add TextBox_cahnged function to the textbox for ex:

<TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="52" Margin="285,150,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="179" TextChanged="TextBox_Change"/>

Seacond add the header using System.Threading; third add to the main wpf file:

static string password = "";
    static int len = 0;
    private void TextBox_Change(object sender, TextChangedEventArgs e)
    {
        int ind = this.textBox1.Text.Length - 1;
        if (ind 1<len)
        {
            password = password.Substring(0, password.Length - 1);
            len -= 1;
        }
        if (this.textBox1.Text.Length > 0 && this.textBox1.Text[ind]!='*')
        {
            len  = 1;
            this.textBox1.Text = textBox1.Text;
            password = password   this.textBox1.Text[ind];
            Thread thread = new Thread(threadFunc);
            thread.Start((object)ind);

            textBox1.CaretIndex = textBox1.Text.Length;
        }
        
    }
    public void threadFunc(object ind2)
    {
        int ind = (int)ind2;
        Thread.Sleep(1500);
        Dispatcher.BeginInvoke(new Action(delegate
        {
            this.textBox1.Text = this.textBox1.Text.Substring(0, ind)   '*';
            if (this.textBox1.Text.Length - 1 > ind)
            {
                this.textBox1.Text = this.textBox1.Text   this.textBox1.Text.Substring(ind   1);
            }
            textBox1.CaretIndex = textBox1.Text.Length;
        }));
    }

the thread is important so the textbox will update and show the letters

CodePudding user response:

You have to update the Text after 0.5 seconds, I'd do it like this..

At the end of OnTextInput, move the character conversion to RunAfter

Utils.RunAfter(.5, () =>
{
    // convert to stars *
    Text = new string('*', Text.Length);
    // make sure the caret at the end
    CaretIndex = Text.Length;
});

Where RunAfter is a utility function

public static void RunAfter(double seconds, Action action)
{
    if (Application.Current.Dispatcher == null)
        return;

    var myTimer = new DispatcherTimer(
        DispatcherPriority.Normal,
        Application.Current.Dispatcher)
    {
        Interval = TimeSpan.FromSeconds(seconds)
    };
    myTimer.Tick  = (_, _) =>
    {
        action?.Invoke();
        myTimer.Stop();
    };
    myTimer.Start();
}
  • Related